在某些情况下,我们需要以编程方式将宏插入到Excel工作表中。我找到了一个VB脚本,它将特定的宏插入到上面提到的excel表中。
Set objExcel = CreateObject("Excel.Application")
strPath = WScript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
excelLocation = strFolder & "\GeneratedReport.xls"
macroVBSLocation = strFolder & "\sendMailModule.bas"
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open(excelLocation)
objWorkbook.VBProject.VBComponents.Import macroVBSLocation
objWorkbook.Save
objExcel.Quit
excelLocation指向excel将出现的位置。 macroVBSLocation指向宏将存在的位置。