Creating Excel Macro for Exporting XML to a certain folder

前端 未结 1 1702
刺人心
刺人心 2021-01-07 11:45

I need to create a macro (which I have never done before) and if you guys can guide me to a right path, it would be really appreciated.

What I\'m doing currently: I

1条回答
  •  攒了一身酷
    2021-01-07 12:06

    My co-worker actually helped me out with this. I thought I should share what I did

    Sub ExportXML()
    '
    ' Export XML Macro exports the data that is in Excel to XML.
    '
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    '
    newFileName = Application.GetSaveAsFilename("out.xml", "XML Files (*.xml), *.xmls")
    If newFileName = False Then
    Exit Sub
    End If
    If objFSO.FileExists(newFileName) Then
    objFSO.DeleteFile (newFileName)
    End If
    ActiveWorkbook.XmlMaps("Root_Map").Export URL:=newFileName
    
    
    Set objFile = objFSO.OpenTextFile(newFileName, ForReading)
    
    
    Dim count
    count = 0
    Do Until objFile.AtEndOfStream
     strLine = objFile.ReadLine
     If count = 0 Then
        strNewContents = strNewContents & "" & vbCrLf
    ElseIf count = 1 Then
        strNewContents = strNewContents & "" & vbCrLf
    Else
        strNewContents = strNewContents & strLine & vbCrLf
    End If
    count = count + 1
    
    Loop
    
    objFile.Close
    
    Set objFile = objFSO.OpenTextFile(newFileName, ForWriting)
     objFile.Write strNewContents
    
    objFile.Close
    End Sub
    

    0 讨论(0)
提交回复
热议问题