我正在运行一个 word 宏
- 初始化 Excel
- 在 Excel 中创建一个临时工作簿
- 退出word userform时,终止excel
但是,似乎有一些剩余的 Excel 实例/工作簿没有完全关闭,因为当我再次启动 word 宏时,我收到错误:462,远程服务器机器...
在 Word 文档中初始化用户表单:
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
userform1.show
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
顺便说一句,我认为问题是最后一部分:
Dim sKillExcel As String
sKillExcel = "TASKKILL /F /IM Excel.exe"
Shell sKillExcel, vbHide
因为如果我在任务管理器中停止宏并终止 EXCEL,如果我再次运行宏,它不会给我这个问题......我还尝试了其他方法,比如调用我保存在目录而不是目录中的 excel 工作簿临时通过createobject(“excel.application”)在一个sub,exc.quit,但我必须使用上面的终止代码cuz否则EXCEL仍然显示在任务管理器中。