我正在从XP中的任务调度程序转移到Windows7,我试图更好地理解它。
我的vbs脚本位于此位置N:\Folder1\subFolder1\subFolder2\subFolder3\subFolder4\script.vbs
这个script.vbs
基本上在同一个目录中打开一个excel文件(.xlsm)并在该excel文件中运行一个宏,然后关闭excel文件。
该脚本使用此代码strPath = WshShell.CurrentDirectory
获取其所在的目录。
宏基本上从csv文件(位于其他地方)复制数据,并可能做一些其他的数据操作。
我在上面的各种文件夹/子文件夹中这样做了很多次,所以基本上每个excel文件都有一个文件夹,它也有自己的script.vbs
文件。
然后我安排这些脚本在任务调度程序中运行。
在XP中,这是可以的,因为脚本已经安排并按要求运行。
但现在我使用的是Windows7,希望能再次安排这些内容。
事实证明,我必须将每个script.vbs的位置放在每个计划的Start in: (optional)
选项卡的Action
parmaeter中。
但我想知道的是,为什么我必须为每个时间表执行此操作? 我以为我在脚本中获取了script.vbs的当前目录,或者我遗漏了一些内容。 任何其他评论/改进欢迎这种方法。
注意:
我不记得必须在Windows XP中执行此操作,但也许Windows XP为您完成此操作。
我不需要运行最高的权限,因为我的密码每3个月更改一次
'need to update WBName & MacroName here as this is fairly generic
dim WshShell
set WshShell = CreateObject("Wscript.Shell")
dim strPath
strPath = WshShell.CurrentDirectory
'WScript.echo("For Debugging: strPath")
'WScript.echo(strPath)
'msgbox strPath
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
'myExcelWorker.Visible = True ' this makes excel visible
dim oWorkBook
dim WBName
WBName = "\ExcelWorkBook.xlsm" 'WB to be opened
dim MacroName
MacroName = "'" & strpath & WBName & "'!macroName" 'Macro Name to be run
'Write Start+strPath to log file
Call WriteLog("Start_XXX",strPath,"var3")
'Write Mid+strPath+WBName to log file
Call WriteLog("Mid___XXX",strpath & WBName,"var3")
'open WB for running macro
'set oWorkBook = myExcelWorker.Workbooks.open(strpath & WBName) 'for WB WITHOUT password
Set oWorkBook = myExcelWorker.Workbooks.Open(strpath & WBName,,,,"","Password") 'for WB with password
'Write MacroName to log file
Call WriteLog("Mid___XXX",MacroName,"var3")
myExcelWorker.Run MacroName
myExcelWorker.DisplayAlerts = False 'this is required so the WB will save without being prompted
oWorkBook.Save
oWorkBook.Close
myExcelWorker.DisplayAlerts = True ' set it back to true again as it is good practice
myExcelWorker.Quit
'Write End to log file
Call WriteLog("End___XXX","t2","t3")
set oWorkBook = Nothing
set myExcelWorker = Nothing
set WshShell = Nothing
'sub to write to log file
Sub WriteLog(var1, var2, var3)
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
'Wscript.Echo "VBSStart.vbs is running"
Dim ObjFso
Dim StrFileName
Dim ObjFile
Dim FlName
'WScript.Echo var1 & ",,,," & var2
FlName = "TestFile.txt"
StrFileName = objShell.CurrentDirectory & "\" & FlName
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
set ObjFile = ObjFso.OpenTextFile(StrFileName, 8, True)
'Writing a string into the file
ObjFile.WriteLine(var1 & "," & var2 & "," & var3 & "," & now)
'Closing the file
ObjFile.Close
'msgbox "near the end..."
' Using Set is mandatory
Set objShell = Nothing
End Sub