Powershell - 从脚本打开文件时未加载Excel加载项(XLAM)

时间:2017-08-16 15:48:57

标签: excel-vba powershell excel-addins vba excel

我在Excel中启用了一个加载项文件“add-in.xlam”,以便所有Excel文件都可以使用一组自定义加载项功能。我在Module1中用一个名为'exportModule'的宏创建了一个新文件'Export VBA.xlsm'。当我手动打开新的excel文件并运行exportModule时,所有工作都按预期工作。我已经创建了一个PowerShell脚本来自动化该过程,如果我单步执行代码,我注意到在打开“Export VBA.xlsm”时没有加载add-in.xlam。

这是我的PowerShell脚本:

$excel = New-Object -comobject Excel.Application
$workbook = $excel.Workbooks.Open("C:\FilePath\Export VBA.xlsm")
$excel.Visible = $true
$excel.Run("'Export VBA.xlsm'!Module1.exportModule")
$workbook.close()
$excel.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)

这是我的VBA宏:

Sub exportModule()

    Dim codeMod As VBIDE.VBComponent
    Dim vbProj As VBProject
    Dim vbComp As VBComponent
    Dim destDir As String
    Dim outputFileBase As String
    Dim vbProjectName As String
    Dim index As Integer
    Dim subIndex As Integer
    Dim outputFileName As String
    Dim moduleName As String

    vbProjectName = "add-in.xlam"
    destDir = "C:\Git\dev"
    outputFileBase = "add-in-|.txt"
    index = 1
    subIndex = 1

    For Each vbProj In Excel.AddIns.Application.VBE.VBProjects
        If InStr(1, Excel.AddIns.Application.VBE.VBProjects(index).FileName, vbProjectName) > 0 Then
            For Each vbComp In Excel.AddIns.Application.VBE.VBProjects(index).VBComponents
                If Excel.AddIns.Application.VBE.VBProjects(index).VBComponents(subIndex).Type = 1 Then
                    Set codeMod = Excel.AddIns.Application.VBE.VBProjects(index).VBComponents(subIndex)
                    moduleName = Excel.AddIns.Application.VBE.VBProjects(index).VBComponents(subIndex).Name
                    outputFileName = Replace(outputFileBase, "|", moduleName)
                    Call ExportVBComponent(codeMod, destDir, outputFileName)
                End If
                subIndex = subIndex + 1
            Next vbComp
        End If
        index = index + 1
    Next vbProj

End Sub

有什么想法吗? 谢谢,

布赖恩

0 个答案:

没有答案