M
Mike Collard
I want to refresh an Excel spreadsheet and then save it
from an MS Access module. I have tried the following,
based on the MS Help for the GetObject function, which
opens Excel and the appropriate workbook but fails to
refresh the data.
Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final
release.
' Test to see if there is a copy of Microsoft Excel
already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument
returns a
' reference to an instance of the application. If the
application isn't
' running, an error occurs.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.
' Set the object variable to reference the file you want
to see.
Set MyXL = GetObject("c:\myfile.XLS")
' Show Microsoft Excel through its Application property.
Then
' show the actual window containing the file using the
Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows("myfile.XLS").Visible = True
MyXL.ActiveWorkbook.RefreshAll
MyXL.ActiveWorkbook.Save
MyXL.ActiveWorkbook.Close
' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit
method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If
Set MyXL = Nothing ' Release reference to the
' application and
spreadsheet.
End Sub
Any help appreciated.
Thanks
Mike Collard
from an MS Access module. I have tried the following,
based on the MS Help for the GetObject function, which
opens Excel and the appropriate workbook but fails to
refresh the data.
Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final
release.
' Test to see if there is a copy of Microsoft Excel
already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument
returns a
' reference to an instance of the application. If the
application isn't
' running, an error occurs.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.
' Set the object variable to reference the file you want
to see.
Set MyXL = GetObject("c:\myfile.XLS")
' Show Microsoft Excel through its Application property.
Then
' show the actual window containing the file using the
Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows("myfile.XLS").Visible = True
MyXL.ActiveWorkbook.RefreshAll
MyXL.ActiveWorkbook.Save
MyXL.ActiveWorkbook.Close
' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit
method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If
Set MyXL = Nothing ' Release reference to the
' application and
spreadsheet.
End Sub
Any help appreciated.
Thanks
Mike Collard