简体   繁体   中英

Vba-Excel: Export Excel Sheet to XML?

I have requirement to export excel spreadsheet data to XML. To do this i added few lines of code in WorkSheet_Activate ie,

Private Sub Worksheet_Activate()

 Dim oMyconnection As Connection
 Dim oMyrecordset As Recordset
 Dim oMyXML As DOMDocument
 Dim oMyWorkbook As String

 Set oMyconnection = New Connection
 Set oMyrecordset = New Recordset
 Set oMyXML = New DOMDocument

 oMyWorkbook = Application.ThisWorkbook.FullName

 oMyconnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                  "Data Source=" & oMyWorkbook & ";" & _
                  "Extended Properties=excel 8.0;" & _
                  "Persist Security Info=False"

 oMyrecordset.Open "Select * from [Sheet1$A1:C100]", oMyconnection, adOpenStatic

 oMyrecordset.Save oMyXML, adPersistXML

 oMyXML.Save (ThisWorkbook.Path & "\Output.xml")

 oMyrecordset.Close
Set oMyconnection = Nothing
Set oMyrecordset = Nothing
Set oMyXML = Nothing

But when ever i try to execute it i am getting an error like User-Defined Datatype not found . Actually i am getting this error because of the line Dim oMyXML As DOMDocument . Am i missing any reference? Any help would be appreciated greatly.

I know this thread is a few months old, but you'll need to add a reference to " Microsoft XML, v6.0 " (or whatever version you have/need) to use the DOMDocument datatype.

Dim oMyXML As MSXML.DOMDocument
Set oMyXML = New MSXML.DOMDocument

he problem isn't "XML output" per se, but weirdness with Microsoft VBA and referencing the right ActiveX components.

Look at either/both of these links and see if they help:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM