PinPinPoola
New Member
- Joined
- Aug 17, 2021
- Messages
- 9
- Office Version
- 365
- Platform
- 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.
And the Worksheet looks like this:
This is the VBA that works natively in Excel (Recorded as a Macro):
Here is the PowerShell I am testing with:
I get an error popup and when I select OK, the script exits and no .xlsx file is created.
If I specify the XML file name as the only OpenXML parameter:
The .xlsx file is created, but not in the way I want:
I am stuck and would really appreciate any help or suggestions here.
Many Thanks
Pin
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.
And the Worksheet looks like this:
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.
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:
I am stuck and would really appreciate any help or suggestions here.
Many Thanks
Pin
Last edited by a moderator: