我正在尝试使用vbs调用我的excel宏。这是我的代码片段。
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
objExcel.Application.Visible = True
objExcel.Application.Run "C:\Folder\Test_PO.xls!Data_Analysis"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit
现在问题是我能够打开文件,但宏以某种方式在这里被禁用并向我显示'macro may not be present or may be disabled'
。我确定我正在调用正确的宏名称,但是一旦文件被打开,加载项选项卡我已经配置了宏来运行得到消失。如果我手动打开文件,这不会打开,我可以看到选项卡并从选项卡本身运行宏。有什么建议我如何克服这个问题并让宏运行?
答案 0 :(得分:7)
试试这个
Dim objExcel, objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
objExcel.Visible = True
objExcel.Run "Data_Analysis"
objWorkbook.Close
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Echo "Finished."
WScript.Quit
修改强>
如果宏在模块中,那么以上内容将有所帮助。如果宏在工作表中,请说Sheet1然后替换行
objExcel.Run "Data_Analysis"
与
objExcel.Run "sheet1.Data_Analysis"
<强>后续强>
试试这段代码。
Dim objExcel, objWorkbook, ad, FilePath
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
For Each ad In objExcel.AddIns
If ad.Name = "Converteam.xla" Then
FilePath = ad.Path & "\Converteam.xla"
Exit For
End If
Next
objExcel.Workbooks.Open (FilePath)
Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\Test_PO.xls")
objExcel.Run "Data_Analysis_Converteam"
objWorkbook.Close
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Echo "Finished."
WScript.Quit
<强>说明强>:
使用CreateObject
时,默认情况下不会安装加载项。请看这个链接。
主题:在Excel中使用CreateObject命令时无法加载加载项
链接:http://support.microsoft.com/kb/213489/
您必须加载加载项,然后调用相关的宏。此外,您的宏名称不是Data_Analysis
,而是Data_Analysis_Converteam
HTH
答案 1 :(得分:1)
要添加到Siddhart的答案 - 您可以在VBScript中加载所需的插件,如下所示:
objExcel.RegisterXLL("analys32.xll") 'For XLL addins
objExcel.Workbooks.Open(objExcel.LibraryPath & "\analysis\atpvbaen.xla") 'For standard XLA addins
objExcel.Workbooks.Open("C:\Program Files\MyAddins\MyAddin.xla") 'for custom XLA addins