我正在VBS中编写一个脚本,在目录中的许多excel文件上运行宏。我是VBS和宏的新手。
根目录中包含许多文件夹。每个文件夹里面都有许多子文件夹。我正在寻找这个级别的文件夹,称为“检查”。在这些文件夹中,我正在寻找符合 .050。或 .120。模式的文件。如果找到这些文件,我想在它们上运行Excel宏来修改页脚。
我设法设置所有逻辑来搜索文件,这似乎正在起作用。我在PERSONAL.XLSB文件中记录了一个宏,我可以单独打开文件并成功运行宏。
问题:当我尝试从代码中调用宏时,我收到以下错误:
无法运行宏
宏可能在此工作簿中不可用,或者可能禁用所有宏。
我在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
此时我能够找到我正在寻找的文件,并且脚本尝试执行宏,但是我收到错误。谁能看到我做错了什么?
答案 0 :(得分:0)
只试用xlApp.Run("PERSONAL.XLSB!ModifyHeaderFooter")
答案 1 :(得分:0)
最后我搬家了
Set xlApp = CreateObject("Excel.application")
在我的循环之外并改变了
xlApp.Application.Run "C:\Users\Nick\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB!Module1.ModifyHeaderFooter"
到
xlApp.Run("PERSONAL.XLSB!ModifyHeaderFooter")
我的错误已经解决了。我还添加了
xlApp.Workbooks.Open("C:\Users\Nick\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB")
在我调用宏之前的那一刻。我可能一直在做其他错误的事情,但是当我在循环之外打开它时它只能进行一次迭代。感谢Scott和Garbb的贡献,感谢Scott提供额外的效率提示。