简体   繁体   中英

How to run from Powershell a VBA macro that does a SAPLogon

I have an Excel that uses the Analysis for Office Add-In. It has a VBA macro that, among other things, does this:

ThisWorkbook.Application.Run("SAPLogon", "DS_1", "100", "myuser", "mypassword")

When I run the macro form Excel it works, but I'm trying to automate it with Powershell, and when I try to call it form there, I get this error:

Run-time error '1004':
Cannot run the macro 'SAPLogon'. The macro may not be available in this 
workbook or all macros may be disabled.

As it tends to get disabled sometimes, I tried enabling the "Analysis" add-in first like this in the same macro:

For ndx = 1 To Application.COMAddIns.Count
    If Application.COMAddIns(ndx).Description = "Analysis" Then
        Application.COMAddIns(ndx).Connect = True
        Exit For
    End If
Next

But it still doesn't work. Running other macros from the same Powershel does work. What am I missing?

Here's a sample of my PowerShell code in which I'm opening an Excel file, running the macro CreateChart and saving then closing the file again.

$excel = new-object -comobject excel.application
$workbook = $excel.workbooks.open("C:\Excel\Temp\File.xlsx")
$worksheet = $workbook.worksheets.item(1)
$excel.Run("CreateChart")
$workbook.save()
$workbook.close()
$excel.quit()

I'd recommend adjusting this sample according to your code and check whether your way of running the macro was wrong perhaps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM