Save as XML with VBA?

Can I save an Excel spreadsheet in XML format? I have an XML Schema file ... and some data in a table ... and I have an Excel option Save as XML file, but can I save the file as XML from VBA? I want to automate the process, and I did not find anything like that. Thanks!

+6
source share
2 answers

this time I managed to save the good ol "macro recorder)) (Why didn’t I use it before I posted here?) So ... To load the xml schema, follow these steps:

ActiveWorkbook.XmlMaps.Add("Book2.xml", "raport").Name _ = "raport_Map" 

And save it as xml:

 ActiveWorkbook.SaveAsXMLData Filename:="Book3.xml", _ Map:=ActiveWorkbook.XmlMaps("raport_Map") 

Who would have thought so easy?

+5
source

This link helped me the most -> http://curiousmind.jlion.com/exceltotextfile

Script at the link:

 Sub MakeXML(iCaptionRow As Integer, iDataStartRow As Integer, sOutputFileName As String) Dim Q As String Q = Chr$(34) Dim sXML As String sXML = "<?xml version=" & Q & "1.0" & Q & " encoding=" & Q & "UTF-8" & Q & "?>" sXML = sXML & "<rows>" ''--determine count of columns Dim iColCount As Integer iColCount = 1 While Trim$(Cells(iCaptionRow, iColCount)) > "" iColCount = iColCount + 1 Wend Dim iRow As Integer iRow = iDataStartRow While Cells(iRow, 1) > "" sXML = sXML & "<row id=" & Q & iRow & Q & ">" For icol = 1 To iColCount - 1 sXML = sXML & "<" & Trim$(Cells(iCaptionRow, icol)) & ">" sXML = sXML & Trim$(Cells(iRow, icol)) sXML = sXML & "</" & Trim$(Cells(iCaptionRow, icol)) & ">" Next sXML = sXML & "</row>" iRow = iRow + 1 Wend sXML = sXML & "</rows>" Dim nDestFile As Integer, sText As String ''Close any open text files Close ''Get the number of the next free text file nDestFile = FreeFile ''Write the entire file to sText Open sOutputFileName For Output As #nDestFile Print #nDestFile, sXML Close End Sub Sub test() MakeXML 1, 2, "C:\Users\jlynds\output2.xml" End Sub 
0
source

Source: https://habr.com/ru/post/909950/


All Articles