Home » excel » exiting/restarting a word macro that controls excel

exiting/restarting a word macro that controls excel

Posted by: admin May 14, 2020 Leave a comment


I’m running a word macro that

  1. initializes Excel
  2. creates a temporary workbook in excel
  3. when exiting the word userform, terminates excel

However, it seems there is some residual Excel instances/workbook that is not fully closed because when I start the word macro again, I get error: 462, remote server machine …

initialize userform in a word document:

private sub commandbutton1_click()
    dim exc as excel.application
    set exc as new excel.application
    dim wb as excel.workbook

    set wb = exc.workbook.saveas filename:="wordexc"      'creates temp workbook
end sub

run excel processing via a userform1 commandbutton:

private sub commandbutton2_click()

    excel.workbook("wordexc").close   'closes temp workbook

    dim wb as excel.workbook
    set wb = excel.workbook.saveas filename:="wordexc"     'this wb is for actual use
    'i do not delete this wb after running cuz it has stored data that will be used
    'if user cliks commandbutton2 again, it will be deleted and new wbook with new data  
    'is created

    'processing code

end sub

private sub queryclose (etc...)
    'Close Excel
    Dim sKillExcel As String

     sKillExcel = "TASKKILL /F /IM Excel.exe"
     Shell sKillExcel, vbHide

end sub

btw i think the problem is the last part:

 Dim sKillExcel As String

 sKillExcel = "TASKKILL /F /IM Excel.exe"
 Shell sKillExcel, vbHide

Cuz if I stop the macro and terminate EXCEL in task manager, it doesn’t give me this problem if I run the macro again…
I also tried other methods, like calling a excel workbook i saved in a directory instead of a temporary one via createobject(“excel.application”) in one sub, exc.quit, but I have to use that termination code above cuz otherwise EXCEL still shows in task manager.

How to&Answers:

Besides not declaring your objects correctly, you are not (if I may say so) flushing the toilet correctly.

See this example (UNTESTED)

I am using Latebinding instead of Early Binding so that this code is compatible with any version of Excel

'~~> Define your Excel objects so that they are available 
'~~> in other userform sub procedures as well.
Dim oXLApp As Object, oXLwb As Object

Private Sub CommandButton1_Click()
    Set oXLApp = CreateObject("Excel.Application")

    '~~> Show Excel
    oXLApp.Visible = True

    '~~> Add a new workbook
    Set oXLwb = oXLApp.Workbooks.Add

    '~~> Do some stuff

    '~~> Save the file
    oXLwb.SaveAs "C:\wordexc.xlsx", FileFormat:=51

    oXLwb.Close SaveChanges:=False
End Sub

'~~> Close and Quit Excel (Flush Toilet)
Private Sub CommandButton2_Click()
    Set oXLwb = Nothing
    Set oXLApp = Nothing
End Sub


Why not do something like this? Basically, you define your excel object globally, and then you can call it’s “Quit” method later.

Private objExcel As Excel.Application

Private Sub CommandButton1_Click()
    Set objExcel = New Excel.Application
    objExcel.Visible = True
End Sub

Private Sub CommandButton2_Click()

End Sub