Code to open specific Excel sheet ...

  • Thread starter Thread starter m.cringle
  • Start date Start date
M

m.cringle

Hi

Can anybody tell me what code I need to just open an Excel spreadsheet?

I already have an event sorted but I'm not sure how to get the
spreadsheet to open afterwards. I'm new to the VBA 'stuff'!

Cheers guys!
 
First make sure you have a reference to the Excel object library in your
database (Tools|References on the VBA menu bar). Then add a module along
these lines to the database:

''''module starts''''
Option Compare Database
Option Explicit

Private Function GetExcelApp() As Object

' if Excel open return reference to it
' else establish reference to it
On Error Resume Next
Set GetExcelApp = GetObject(, "Excel.Application")
If Err.Number = 429 Then
Set GetExcelApp = CreateObject("Excel.Application")
End If

End Function


Public Sub OpenWorkbook(strWorkBook As String)

Dim appExcel As Excel.Application

Set appExcel = GetExcelApp()

appExcel.Workbooks.Open FileName:=strWorkBook
appExcel.Visible = True

End Sub
''''module ends''''

Call the OpenWorkbook procedure, passing the path to the Excel file to it,
e.g.

OpenWorkbook "F:\SomeFolder\SomeSubfolder\SomeWorkbook.xls"

Ken Sheridan
Stafford, England
 
Great stuff - thanks Ken, much appreciated

Ken said:
First make sure you have a reference to the Excel object library in your
database (Tools|References on the VBA menu bar). Then add a module along
these lines to the database:

''''module starts''''
Option Compare Database
Option Explicit

Private Function GetExcelApp() As Object

' if Excel open return reference to it
' else establish reference to it
On Error Resume Next
Set GetExcelApp = GetObject(, "Excel.Application")
If Err.Number = 429 Then
Set GetExcelApp = CreateObject("Excel.Application")
End If

End Function


Public Sub OpenWorkbook(strWorkBook As String)

Dim appExcel As Excel.Application

Set appExcel = GetExcelApp()

appExcel.Workbooks.Open FileName:=strWorkBook
appExcel.Visible = True

End Sub
''''module ends''''

Call the OpenWorkbook procedure, passing the path to the Excel file to it,
e.g.

OpenWorkbook "F:\SomeFolder\SomeSubfolder\SomeWorkbook.xls"

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top