excel - 从 VBS 运行 VBA 宏时出现问题

标签 excel vbscript vba

我正在 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/

相关文章:

sql-server - 启动订阅者同步的脚本

vba - 以编程方式保存 Excel 加载项

java - 如何在没有外部jar的情况下使用Java读取excel文件

vbscript - 如何清除vbscript中数组的内容?

vbscript - 在什么条件下 Wscript.Echo 显示为弹出窗口而不是控制台

excel - 如何在 VBA 中格式化特定的邮件值

vba - Excel VBA : If value not found in range, 转到

excel - 使用 AppleScript 在 Excel 2016 中打开文件

mysql - Excel 到一个数据库输出中的多个表 - PDI

excel - 在 Excel 中编写 ANN : VBA Type Mismatch Error