View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
patrick molloy patrick molloy is offline
external usenet poster
 
Posts: 391
Default opening diff. worksheets in opened workbook

from your main access vba call a sub that assigns the
workbook to a variable - assumes the workbook is open. If
its not then it would raise an error...that's trapped by
testing the err number , a non-zero value is an
error...in which case it then opes the book...either way,
it returns the workbook object assigned tot hat workbook
to the calling code


dim wb as workbook
Set WB = GetWorkbook("MyBook")
if WB Is Nothing then
msgbox "Cannot find " & "MyBook"
exit sub
end if
.... workbook assigned to wb, carry on...
.... your access code



Private Function GetWorkbook(sName as string) as workbook
on error resume next
dim wb as workbook
set wb = xlApp.Workbooks(sName)
if err.number<0 then
err.clear
set wb = xlApp.Workbooks.Open(sName)
end if
set GetWorkbook = wb
End Function


HTH

Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
How could I open another spreadsheet in the same

workbook
if the workbook has been already opened?
I'm doing it from MS Access.

If logWorkbookIsOpened = False Then
'it's working good
Set xlBook = xlApp.Workbooks.Open(strWorkbookName)
Set xlSheet = xlBook.Worksheets(strWorksheetName)
xlSheet.Activate

xlApp.Visible = True

Else
'here is a mistake #0
Set xlSheet = Workbooks(strWorkbookName).Worksheets
(strWorksheetName)
xlSheet.Activate
xlApp.Visible = True

End If

Thanks

.