开发者

MSAccess VBA code won't open Excel sheet if Excel is already running

开发者 https://www.devze.com 2023-04-07 06:40 出处:网络
When I open excel in MS Access through the following VBA code: Set objApp = GetObject(, \"Excel.Application\")

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
0

精彩评论

暂无评论...
验证码 换一张
取 消