0

我需要将 excel 电子表格数据导出到 XML。为此,我在 WorkSheet_Activate 中添加了几行代码,即

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

但是,当我尝试执行它时,我会收到类似User-Defined Datatype not found. 实际上我因为这条线而收到这个错误Dim oMyXML As DOMDocument。我缺少任何参考吗?任何帮助将不胜感激。

4

2 回答 2

1

我知道这个线程已有几个月的历史,但您需要添加对“ Microsoft XML, v6.0 ”(或您拥有/需要的任何版本)的引用才能使用 DOMDocument 数据类型。

Dim oMyXML As MSXML.DOMDocument
Set oMyXML = New MSXML.DOMDocument
于 2013-06-27T14:27:40.940 回答
0

他的问题不是“XML 输出”本身,而是 Microsoft VBA 和引用正确的 ActiveX 组件的怪异之处。

查看这些链接中的一个/两个,看看它们是否有帮助:

于 2013-02-02T06:26:19.847 回答