Workbooks.OpenXML VBA Method when running Excel as a PowerShell ComObject. How to specify the LoadOption parameter?

PinPinPoola

New Member
Joined
Aug 17, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am trying to open an XML file with Excel running as a PowerShell ComObject, but I cannot work out how to correctly specify the LoadOption parameter?

I am using the Workbooks.OpenXML method as described here: Workbooks.OpenXML method (Excel)

I don't have a Stylesheet, so only want to supply the FileName and LoadOption parameters.

When I open the XML file via the Excel UI, I need to select the 'As an XML table' to get result I want.

Excel Open XML UI Popup.png


And the Worksheet looks like this:

Excel XML Workbook Working.png


This is the VBA that works natively in Excel (Recorded as a Macro):

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ChDir "D:\Temp\TempVisioStencils\HPE"
    Workbooks.OpenXML Filename:= _
        "D:\Temp\TempVisioStencils\HPE\HPE-ProLiant-DL-Logical_masters.xml", _
        LoadOption:=xlXmlLoadImportToList
    Windows("Book1").Activate
End Sub

Here is the PowerShell I am testing with:

Code:
Remove-Variable -Name * -ErrorAction SilentlyContinue

$xmlFile = "D:\Temp\TempVisioStencils\HPE\HPE-ProLiant-DL-Logical_masters.xml"
$xlsxFile = "D:\Temp\TempVisioStencils\HPE\HPE-ProLiant-DL-Logical.xlsx"

$xlsSpreadsheet = 51
$openXMLFilename = "Filename:=`"$($xmlFile)`""
$openXMLLoadOption = "LoadOption:=xlXmlLoadImportToList"
$openXMLParams = "$openXMLFilename, $openXMLLoadOption"

$Excel = New-Object -ComObject Excel.Application

$WorkBook = $Excel.Workbooks.OpenXML($openXMLParams)
$WorkBook.SaveAs($xlsxFile, $xlsSpreadsheet)

$Excel.Quit()

I get an error popup and when I select OK, the script exits and no .xlsx file is created.

Excel VBA OpenXML PowerShell Error Popup.png


If I specify the XML file name as the only OpenXML parameter:

Code:
$WorkBook = $Excel.Workbooks.OpenXML($xmlFile)

The .xlsx file is created, but not in the way I want:

Excel XML Workbook Not Working.png


I am stuck and would really appreciate any help or suggestions here.

Many Thanks
Pin
 
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Solved. ??

I feel a bit daft that I did not try this before posting here.

$WorkBook = $Excel.Workbooks.OpenXML(FileName,Stylesheets,LoadOption)

Where:

FileName = Full path to the XML file
Stylesheets = A value greater than 0.
LoadOption =
One of the values from XlXmlLoadOption enumeration (Excel)


Name​
Value​
Description​
xlXmlLoadImportToList2Places the contents of the XML data file in an XML table.
xlXmlLoadMapXml3Displays the schema of the XML data file in the XML Structure task pane.
xlXmlLoadOpenXml1Opens the XML data file. The contents of the file will be flattened.
xlXmlLoadPromptUser0Prompts the user to choose how to open the file.
DON'T USE THIS ONE AS EXCEL HANGS WAITING FOR INPUT

So in my case:

Code:
$WorkBook = $Excel.Workbooks.OpenXML($xmlFile,1,2)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,560
Messages
6,120,217
Members
448,951
Latest member
jennlynn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top