【问题标题】:Trouble running a VBA macro from VBS从 VBS 运行 VBA 宏时出现问题
【发布时间】:2018-05-18 07:46:36
【问题描述】:

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

此时我能够找到我正在寻找的文件,并且脚本尝试执行宏,但我得到了一个错误。谁能看到我做错了什么?

【问题讨论】:

  • 您需要在 VBS 脚本创建的 excel 实例中打开您的 Personal.xlsb 工作簿。我还建议重构 vbscript 以在循环外打开 Excel 实例,并且只在循环中打开工作簿。这样,您就不会在每个循环中不必要地创建和销毁 excel 对象。然后我建议重构你的工作簿中的宏,以接受一个 wb 对象作为参数,而不是使用ActiveSheet
  • 谢谢。那么我应该将Set xlApp = CreateObject("Excel.application") 移到第一个循环上方吗?然后我可以使用xlApp.Workbooks.Open("C:\Users\Nick\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB") 打开我的Personal.xlsb 工作簿一次吗?
  • 听起来就像我会做的那样:)

标签: excel vbscript vba


【解决方案1】:

试试xlApp.Run("PERSONAL.XLSB!ModifyHeaderFooter")

【讨论】:

  • 如果该工作簿未在 vbscript 打开的 excel 实例中打开,则无法工作。看我上面的评论
  • 好吧,看来你是对的。我曾假设 PERSONAL.XLSB 工作簿会自动加载,因为它会在您正常运行 excel 时自动加载,但是当您通过 vbs 脚本运行它时它似乎不会自动加载。
  • 谢谢,一旦我应用了上述更改并在实例中打开 Personal.xlsb,您的建议就帮我完成了剩下的工作。
【解决方案2】:

最后我搬家了

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 提供的其他效率提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多