Why I am losing font attributes when exporting(line by line) to an xml file

MPW

Well-known Member
Joined
Oct 7, 2009
Messages
571
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am creating an xml from scratch taking information from sheets in a workbook.
The workbook 3rd cell looks correct:
1676072244413.png

The individual line is added like this:
VBA Code:
        hsy = Sheets("Directive Lang").Cells(7, 3).Value2
        strXML = lvl04 & "<heSaidText>" & hsy & "</heSaidText>"
            Print #intFileNum, strXML
This is how it displays in the Watch window:
1676072010817.png


Here is a sample of my output:
Rich (BB code):
  <ReferenceTexts>
    <ReferenceTextInfo level="0">
      <Language>
        <iso></iso>
        <name>RUSSIAN</name>
        <ldml>ru</ldml>
        <fontFamily>Charis SIL</fontFamily>
        <fontSizeInPoints>10</fontSizeInPoints>
        <heSaidText>o? ??????.</heSaidText>
      </Language>
    </ReferenceTextInfo>
    <ReferenceTextInfo level="1" name="WEB (WEB)">
      <Language>
        <iso>eng</iso>
        <name>English</name>
        <ldml>en</ldml>
        <fontFamily>Charis SIL</fontFamily>
        <fontSizeInPoints>10</fontSizeInPoints>
        <heSaidText>he said.</heSaidText>
Notice the bold lines. The English text displays fine, but the Russian version displays "?" for most characters.

Most of the languages I have worked with display correctly, however some turn out like this.

I already open the final result in NotePad++ to convert it from Ansi to UTF-8, but the text is incorrect before this stage, in the VBA code.

Part 2 of this request is "How do I convert it automatically on the export without the extra step of using NotePad++?

If you have any insight on either of these issues I will be very grateful.

Thanks in advance.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try using the Stream object from the Microsoft ActiveX Data Objects library, which supports UTF-8. Maybe something like this...

VBA Code:
    Dim oStream As Object 'ADODB.Stream
    Set oStream = CreateObject("ADODB.Stream") 'New ADODB.Stream
   
    With oStream
        .Charset = "UTF-8"
        .Mode = 3 'adModeReadWrite
        .Type = 2 'adTypeText
        .Open
       
        hsy = Sheets("Directive Lang").Cells(7, 3).Value2
        strXML = lvl04 & "<heSaidText>" & hsy & "</heSaidText>"
       
        .WriteText strXML
        .SaveToFile "c:\path\filename.txt", 1 'adSaveCreateNotExist
        .Close
    End With

Hope this helps!
 
Upvote 0
Solution
Ok, I can try this, however I am relatively new to XML in general, I was using:
VBA Code:
    Dim xmlDom As MSXML2.DOMDocument60          
    Dim xmlVersion As MSXML2.IXMLDOMProcessingInstruction   

    Set xmlDom = New MSXML2.DOMDocument60

    Set xmlVersion = xmlDom.createProcessingInstruction("xml", "version=" & ChrW(34) & "1.0" & ChrW(34) & " encoding=" & ChrW(34) & "utf-8" & ChrW(34))
    xmlDom.appendChild xmlVersion
Would I use your suggestion with this, or instead of this? Will this require that I re write the whole routine?

When I took this on last year, I really did not know what I was doing. It all does work except the 2 issues I mentioned, but it took a lot of trial and error.
 
Upvote 0
Ok, awesome! I switched out the DOMDocument60 with the streamwriter and it worked. Only real adjustment was to add Chr(10) after each line. I do remember trying this method when I was investigating it last year. I think I failed because I had other forces at work. Thanks Domenic for the quick answer!
 
Upvote 0
One last comment. The old method I was using had to converted to UTF-8 in Notepad++. The new version that Domenic suggested automatically converted the output to UTF-8 BOM. Nice.

It seemed to load into the program that it is intended for without a problem. Thanks again!
 
Upvote 0
That's great, I'm glad I was able to help. And thanks for your feedback.

By the way, were you using Print #intFileNum, strXML as in your original post or the DomDocument object as in your subsequent post? If the latter, you should have been able to save the document as a UTF-8 file with xmlDoc.Save after you've created your XML code.

Cheers!
 
Upvote 0
I was actually using both.
As I stated, I didn't really have a handle on it. I never could get it to save as true xml, probably because I was mixing apples and oranges.

Made me really happy to get it consolidated with your answer.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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