Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Visual Basic for Applications

Post new topic   Reply to topic
working with worksheets
View previous topic :: View next topic  
Author Message
pwdrvr
Newbie


Joined: 08 Aug 2005
Posts: 13

PostPosted: Aug 11th, 2005 07:27 AM    Post subject: working with worksheets Reply with quote

i have no problem writing a code that opens excel now i can't figure out how to open a certain workbook or worksheet in a workbook please help
Back to top
View user's profile Send private message
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Aug 12th, 2005 05:37 AM    Post subject: Reply with quote

Hi,

Here's the bit you know:

Code:

On Error Resume Next 
Set appExcel = GetObject(, "Excel.Application")   
If Err.Number <> 0 Then         
        Set appExcel = CreateObject("Excel.Application")
End If
Err.Clear   
On Error GoTo 0


Here's the bit you are asking about:

Code:

Dim wbWorkBook as Excel.Workbook
Dim shtSheet As Excel.Worksheet

Dim strWorkbook as string
Dim strSheet as string

strWorkbook ="fully qualified path to the Workbook"
strSheet = "sheet name of the sheet to open"

Set wbWorkBook = appExcel.Workbooks.Open(strWorkBook)
Set shtSheet = wbWorkBook.Sheets(strSheet)


When you've finished processing:

Code:

wbWorkBook.Save
wbWorkBook.Close
set shtSheet = Nothing
set wbWorkBook = Nothing


Good luck,

Regards
Doug
Back to top
View user's profile Send private message
pwdrvr
Newbie


Joined: 08 Aug 2005
Posts: 13

PostPosted: Aug 12th, 2005 09:12 AM    Post subject: Reply with quote

ok we know what i'm trying to do and everything i try isn't working
so maybe if i give you what i've got so far you can tell me where i went wrong

Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)

Private Sub Excel_Click()
Rem this will minimize all windows first
Call keybd_event(&H5B, 0, 0, 0)
Call keybd_event(&H4D, 0, 0, 0)
Call keybd_event(&H5B, 0, &H2, 0)

Rem this will open excel
Call Shell("C:\Program Files\Microsoft Office\OFFICE11\excel.exe", vbMaximizedFocus)


Dim wbWorkBook As Excel.Workbook
Dim shtSheet As Excel.Worksheet

Dim strWorkbook As String
Dim strSheet As String

strWorkbook = "C:\Documents and Settings\Josh\My Documents\Mrp Updates\Mrp update.xls"
strSheet = "PO"

Set wbWorkBook = appExcel.Workbooks.Open(strWorkbook)
Set shtSheet = wbWorkBook.Sheets(strSheet)
End Sub

where is the error?¿
Back to top
View user's profile Send private message
dougthomas
Moderator


Joined: 27 Jul 2005
Posts: 271
Location: Essex, UK

PostPosted: Aug 12th, 2005 12:06 PM    Post subject: Reply with quote

Hi again,

I can't see anything wrong with the code. Have you added a reference to the Excel Object Library to your Project ? (From the Project, References menu for the project)

Are you getting some sort of error message? If so what is it?

What are you expecting to see ?

Looking at your code, just before the "End Sub" you have opened Spreadsheet PO in
C:\Documents and Settings\Josh\My Documents\Mrp Updates\Mrp update.xls

I assume you want to do something with it now that it's open.

if you just want to display it, try putting

shtSheet.Visible = xlSheetVisible

after the set ShtSheet = wbWorkBook.Sheets(strSheet) statement.

What exactly are you trying to do ?


Regards
Doug
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Visual Basic for Applications All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic