Excel不会关闭进程

时间:2015-01-14 10:03:45

标签: macros vbscript excel-2010

所以,我在这里使用(修改后)此代码:How to set recurring schedule for xlsm file using Windows Task Scheduler

我的错误:运行时错误:未知的运行时错误。

我已经远远地搜索了一个方法来关闭Excel进程,但几乎每个人都使用。但遗憾的是它给出了上述错误。我也试过。关闭,但这是不被认可的

' Create a WshShell to get the current directory
Dim WshShell
Set WshShell = CreateObject("WScript.Shell") 

' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application") 

' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone

' Tell Excel what the current working directory is 
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = "C:\Users\hviid00m\Desktop"
myExcelWorker.DefaultFilePath = strPath

' Open the Workbook specified on the command-line 
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\Status Report (Boxplots) TEST.xlsm"

Set oWorkBook = myExcelWorker.Workbooks.Open (strWorkerWB, , , , , , True)

' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "Refresh"
on error resume next 
myExcelWorker.Run strMacroName
if err.number <> 0 Then
WScript.Echo "Fejl i macro"
End If
err.clear
on error goto 0 
oWorkBook.Save 
' Clean up and shut down
' Don’t Quit() Excel if there are other Excel instances 
' running, Quit() will shut those down also
myExcelWorker.Quit <--- ERROR

Set oWorkBook = Nothing
Set myExcelWorker = Nothing
Set WshShell = Nothing

1 个答案:

答案 0 :(得分:0)

在另一方找到了一些代码。 之所以(据我所知)是.Quit和.Close适用于VBA而不是VBS。

' Clean up and shut down
' Don’t Quit() Excel if there are other Excel instances 
' running, Quit() will shut those down also
Dim objWMIService, objProcess, colProcess

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = "         & "'EXCEL.EXE'")

For Each objProcess in colProcess
objProcess.Terminate()
Next`

Set oWorkBook = Nothing
Set myExcelWorker = Nothing
Set WshShell = Nothing