Import XML as text on Mac version

jlduerr

New Member
Joined
Sep 3, 2021
Messages
6
Office Version
  1. 365
Platform
  1. MacOS
Hey all,

My team has an Excel report that we generate from a macro that is importing an XML file. However, my team also works on Macs and the macro does not run properly without opening the file in a virtual emulator with Windows. They have been creating reports via this method for years..

Anyways, I've been searching for a better way and noticed the Mac version of Excel does not have the Import XML functionality. I did, however, notice that the Mac version of Excel will treat an XML file as a text file and the data could still be imported accordingly. I've created a brand new macro to attempt this method (code below). This newly created macro runs properly on my Mac and virtual Windows and the others' virtual Windows versions. However, this macro does not run properly on the others' Mac versions. It will come up with an error and the debug highlights the .Refresh BackgroundQuery:=False line.

Does anyone know what might be causing this? It would be great have this working on everyone's Macs.


Code:
Sub ImportText()

Dim FilePath As String
Dim FolderPath As String
FolderPath = ThisWorkbook.Path
FilePath = FolderPath & "/ReportInfo.xml"

Worksheets("Data").Activate
 Range("B1").Select
    Application.CutCopyMode = False
    With Sheets("Data").QueryTables.Add(Connection:= _
        "TEXT;" & FilePath, Destination:=Range( _
        "$B$1"))
        .Name = "PressReportInfo_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = False
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 10000
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1)
        .TextFileFixedColumnWidths = Array(100)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Range("C1").Select
End Sub
 
Last edited by a moderator:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Any chance you can provide a download link to an example data set .xml file?
 
Upvote 0
Which versions of macOS and Office are on the Macs where it doesn't work?
 
Upvote 0
Josh D
Test
Pilot
AE10001_001_Test Pilot
37.5
100
GC-2 POLYLITH
CMYK
AE10001

Test Pilot
Tag
Pantone 186 C, Pantone Black C
1
0
1
0
1
0
2000_S_F
2000_S_B
Hi

Thanks for providing the end result. I was hoping for an actual .xml file to test with. You may have to put it onto a sharing service such as OneDrive or DropBox and provide the share link in a reply here. Thank you,
 
Upvote 0
Hi again,

I am able to confirm the error is generated at the RefreshBackground command.

When adding the querytable on the Mac, this works:

VBA Code:
Sheets("Data").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;/Users/JBG/Desktop/ReportInfo.xml", Destination:=Range("$B$1"))
        .Name = "ReportInfo_2"
 
Upvote 0
Try changing the way you add the querytable, and adjust the Fixed Column Widths accordingly.

This example works:

VBA Code:
Sub Macro3()


    Sheets("Data").Select
    Range("B1").Select
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;/Users/JBG/Desktop/ReportInfo.xml", Destination:=Range("$B$1"))
        .Name = "ReportInfo_4"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = False
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 10000
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1)
        .TextFileFixedColumnWidths = Array(33)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
 
Upvote 0
Try changing the way you add the querytable, and adjust the Fixed Column Widths accordingly.

This example works:

VBA Code:
Sub Macro3()


    Sheets("Data").Select
    Range("B1").Select
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;/Users/JBG/Desktop/ReportInfo.xml", Destination:=Range("$B$1"))
        .Name = "ReportInfo_4"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = False
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 10000
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1)
        .TextFileFixedColumnWidths = Array(33)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
This works, however, the issue that I am having is that I would need the folder path to be dynamic rather than static. I feel like the issue is related to that. Once I change the folder path to "ThisWorkbook.Path" & "/ReportInfo.xml" is where I have the issue. And the thing is I don't even have the issue. It's when another user other than me that attempts the macro.
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

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