2

I am trying to export Excel files to PDF. Since there are a lot of Excel files to be exported I'm trying to automate this via PowerShell. In principle this works fine, but some Excel files contain formulas from an add-in. The corresponding cells only container "#NAME?" if the file is opened via PowerShell. If the file is opened manually the values are calculated correctly.

I set $objExcel.visible = $True to be able to see what is going on and introduced a pause of 120 seconds to check for any error and if the add-in are actually loaded. The add-ins seem to be all active but the formulas still display as "#NAME?".

Nonetheless I tried to add the add-ons manually. Interestingly this fails for .dll add-ins with the error message "Unable to get the Add property of the AddIns class". All other add-ins do not give this error message.

What am I missing?

This is the relevant part of my Powershell script:

# Initialize Excel object
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type]
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $True
$objExcel.AskToUpdateLinks = $False
$objExcel.DisplayAlerts = $False

# Open Excel file
$workbook = $objExcel.workbooks.open("path/to/document.xls, 3, 0, 1)
$workbook.checkCompatibility = $False
$workbook.Saved = $true

# Possible load add-ins, tried with and without
$workbook.Application.AddIns.Add("C:\Program Files (x86)\some\application\some_addin.dll").Installed = $True
$workbook.Application.AddIns.Add("C:\Program Files (x86)\Microsoft Office\root\Office16\Library\some_other_addin.xla").Installed = $True

# Tried refresh all - to no effect
$workbook.RefreshAll()

Start-Sleep -Seconds 120

# Save as PDF
$pdfPath = "path/to/document.pdf
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $pdfPath)

# Close Excel
$workbook.Close($False)
$objExcel.Workbooks.close()
$workbook = $null
$objExcel.Quit()
4
  • 1
    Is it an .xla or a .dll? The latter sounds like a COMAddin. Commented Nov 5 at 16:43
  • There are actually multiple add-ins, some are DLLs and some are XLAs. Commented Nov 6 at 7:01
  • 2
    If you can rely on the files having been calculated, turning off calculation prior to opening a file should avoid the #Name? errors from appearing. Commented Nov 6 at 9:07
  • You can't use AddIns to load a COMAddin. Commented Nov 6 at 16:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.