MSAccess VBA code won't open Excel sheet if Excel is already running
When I open excel in MS Access through the following VBA code:
Set objApp = GetObject(, "Excel.Application")
Set objApp = CreateObject("Excel.Application")
objApp.Visible = True
Set wb = objApp.Workbooks.Open("\\bk开发者_Python百科00sql0002\D_Root\Pre-Manufacturing\Excel\CommitmentLetter.xls", True, False)
It will not open if the Excel Application is already running. I need to first close the already running Excel Application then I can run the above code and it will open Excel.
How do I get Excel to open even if it is already running? Thanks!
Something like this:
On Error Resume Next
Set objApp = GetObject(, "Excel.Application")
Do While Not objApp Is Nothing
objApp.Quit
Set objApp = GetObject(, "Excel.Application")
Loop
On Error GoTo 0
Set objApp = CreateObject("Excel.Application")
You probably also need to handle the case when Excel won't quit.
UPDATE
To use the existing running instance (rather than kill it):
On Error Resume Next
Set objApp = GetObject(, "Excel.Application")
On Error GoTo 0
If objApp Is Nothing Then
Set objApp = CreateObject("Excel.Application")
End if
精彩评论