我正在 VBS 中编写一个脚本来在一个目录中的许多 excel 文件上运行一个宏。我是 VBS 和宏的新手。
根目录中有许多文件夹。每个文件夹内有许多子文件夹。我正在寻找这个级别的文件夹,称为“检查”。在这些文件夹中,我正在寻找与 .050 模式匹配的文件。或 .120.. 如果找到这些文件,我想在它们上运行 Excel 宏来修改页脚。
我已经设法设置了所有逻辑来搜索文件,这似乎是有效的。我的 PERSONAL.XLSB 文件中记录了一个宏,我可以单独打开文件并成功运行宏。
问题:当我尝试从代码中调用宏时,出现以下错误:
Cannot run the macro
The macro may not be available in this workbook or all macros may be disabled.
我在 Excel 中启用了宏。我已经尝试了多种运行宏的方法,但都无法正常工作。
我的 VBS 脚本:
DIM FSO, rootFolder, subFolders, subFolder, inspectionFolders, inspectionFolder, inspectionFiles, inspectionFile, wb
Set FSO = CreateObject("Scripting.FileSystemObject")
Set rootFolder = FSO.GetFolder("N:\ENGINEERING-Test")
Set subFolders = rootFolder.SubFolders
For Each subFolder in subFolders
WScript.Echo "in " + rootFolder
WScript.Echo "found folder " + subFolder.Name
Set inspectionFolders = subFolder.SubFolders
For Each inspectionFolder in inspectionFolders
WScript.Echo "found folder " + inspectionFolder.name
If InStr(1, inspectionFolder.Name, "Inspection", vbTextCompare) Then
WScript.Echo "In inspection Folder"
Set inspectionFiles = inspectionFolder.files
For Each inspectionFile in inspectionFiles
WScript.Echo "Checking File " + inspectionFile.name
If InStr(1, inspectionFile.Name, ".050.", vbTextCompare) > 0 Or InStr(1, inspectionFile.Name, ".120.", vbTextCompare) > 0 Then
WScript.Echo "Found file " + inspectionFile.name
Set xlApp = CreateObject("Excel.application")
Set xlBook = xlApp.Workbooks.Open(inspectionFolder & "\" & inspectionFile.name, 0, False)
xlApp.Application.Visible = False
xlApp.Application.Run "C:\Users\Nick\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB!Module1.ModifyHeaderFooter"
xlApp.ActiveWindow.close
xlApp.Quit
Else
End If
Next
Else
End If
Next
Next
我的宏:
Sub ModifyHeaderFooter()
'
' ModifyHeaderFooter Macro
'
'
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$3"
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Company, LLC"
.RightFooter = "Page &P of &N&8" & Chr(10) & ""
.LeftMargin = Application.InchesToPoints(0.45)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0.58)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 300
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
End Sub
在这一点上,我能够找到我正在寻找的文件,并且脚本尝试执行宏,但我得到一个错误。谁能看到我做错了什么?
最佳答案
试试xlApp.Run("PERSONAL.XLSB!ModifyHeaderFooter")
关于excel - 从 VBS 运行 VBA 宏时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47640233/