2015-01-14 48 views
0

所以,我使用(修改後)這段代碼,從這裏:How to set recurring schedule for xlsm file using Windows Task SchedulerExcel將不會關閉過程

我的錯誤:運行時錯誤:未知的運行錯誤。

我已經搜索了很多,找到一種方法來關閉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 

回答

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 
+1

這是不正確的。退出(在應用程序對象上)和「關閉」(在工作簿上)在VBScript中工作得很好。此外,'Quit'不會關閉其他Excel實例,只會引用實例的所有窗口。 –

+0

對不起,沒有回覆,一直在度假。那麼退出和關閉不適合我,如果它沒有,我不會說我已經嘗試過,並在這裏問一個問題:-)只是說。 –