bre*_*tdj 14
最好像你指示的那样使用vbs
vbs
,扩展名为.vbs的文本文件(参见下面的示例代码)vbs
vbs
打开workbook
,然后:
Private Sub Workbook_Open()
事件在ThisWorkbook
文件打开时运行代码Application.Run
在vbs
运行宏请参阅Windows任务计划程序上的" 运行Excel"中的后续方法示例
样本vbs
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
'vbs opens a file specified by the path below
Set ObjWB = ObjExcel.Workbooks.Open("C:\temp\rod.xlsm")
'either use the Workbook Open event (if macros are enabled), or Application.Run
ObjWB.Close False
ObjExcel.Quit
Set ObjExcel = Nothing
Run Code Online (Sandbox Code Playgroud)
小智 6
三个重要步骤 - 如何安排 excel.xls(m) 文件的任务
简单地说:
更详细...
`
' a .vbs file is just a text file containing visual basic code that has the extension renamed from .txt to .vbs
'Write Excel.xls Sheet's full path here
strPath = "C:\RodsData.xlsm"
'Write the macro name - could try including module name
strMacro = "Update" ' "Sheet1.Macro2"
'Create an Excel instance and set visibility of the instance
Set objApp = CreateObject("Excel.Application")
objApp.Visible = True ' or False
'Open workbook; Run Macro; Save Workbook with changes; Close; Quit Excel
Set wbToRun = objApp.Workbooks.Open(strPath)
objApp.Run strMacro ' wbToRun.Name & "!" & strMacro
wbToRun.Save
wbToRun.Close
objApp.Quit
'Leaves an onscreen message!
MsgBox strPath & " " & strMacro & " macro and .vbs successfully completed!", vbInformation
'
Run Code Online (Sandbox Code Playgroud)
`
设置程序/脚本:= C:\Windows\System32\cscript.exe
设置添加参数(可选):= C:\MyVbsFile.vbs
那应该工作。
让我知道!
罗德鲍文
我推荐了Kim撰写的博客,它对我来说很好用。看博客
宏的自动执行可以借助Windows Task Scheduler在指定时间调用的VB脚本文件来完成。
请记住,用要打开的工作簿的名称替换“ YourWorkbook”,并用要运行的宏的名称替换“ YourMacro”。
请参见VB脚本文件(仅将其命名为RunExcel.VBS):
' 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
' (otherwise it can't find the files)
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = WshShell.CurrentDirectory
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\YourWorkbook.xls"
Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB)
' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "'" & strPath & "\YourWorkbook" & "!Sheet1.YourMacro"
on error resume next
' Run the calculation macro
myExcelWorker.Run strMacroName
if err.number <> 0 Then
' Error occurred - just close it down.
End If
err.clear
on error goto 0
oWorkBook.Save
myExcelWorker.DefaultFilePath = strSaveDefaultPath
' Clean up and shut down
Set oWorkBook = Nothing
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will shut those down also
if myExcelWorker.Workbooks.Count = 0 Then
myExcelWorker.Quit
End If
Set myExcelWorker = Nothing
Set WshShell = Nothing
Run Code Online (Sandbox Code Playgroud)
您可以从命令提示符下测试此VB脚本:
>> cscript.exe RunExcel.VBS
Run Code Online (Sandbox Code Playgroud)
一旦对VB脚本文件和工作簿进行了测试,使其可以完成所需的工作,则可以使用Microsoft任务计划程序(“控制面板”->“管理工具”->“任务计划程序”)自动执行“ cscript.exe RunExcel.vbs”您。
请注意,宏的路径应采用正确的格式并在单引号内,例如:
strMacroName = "'" & strPath & "\YourWorkBook.xlsm'" &
"!ModuleName.MacroName"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
100529 次 |
最近记录: |