Dim oDoc As DOMDocument
Dim DOCINPATH As String
Dim DOCOUTPATH As String
Dim XMLOUTPATH As String
Dim XMLINPATH As String
Function ReadBinData(ByVal strFileName As String) As Variant
Dim lLen As Long
Dim iFile As Integer
Dim arrBytes() As Byte
Dim lCount As Long
Dim strOut As String
'Read from disk
iFile = FreeFile()
Open strFileName For Binary Access Read As iFile
lLen = FileLen(strFileName)
ReDim arrBytes(lLen)
Get iFile, , arrBytes
Close iFile
ReadBinData = arrBytes
End Function
Private Sub WriteBinData(ByVal strFileName As String)
Dim iFile As Integer
Dim arrBuffer() As Byte
Dim oNode As IXMLDOMNode
If Not (oDoc Is Nothing) Then
' Get the data
Set oNode = oDoc.documentElement.selectSingleNode("/Root/Data")
' Make sure you use a byte array instead of variant
arrBuffer = oNode.nodeTypedValue
' Write to disk
iFile = FreeFile()
Open strFileName For Binary Access Write As iFile
Put iFile, , arrBuffer
Close iFile
End If
End Sub
Private Sub Command1_Click()
Dim oEle As IXMLDOMElement
Dim oRoot As IXMLDOMElement
Dim oNode As IXMLDOMNode
If Text1 = "" Then
MsgBox "您还没有选择路径"
Exit Sub
End If
If Text2 = "" Then
MsgBox "您还没有选择路径"
Exit Sub
End If
' DOCINPATH = Label1.Caption
' XMLOUTPATH = Text1.Text & ".xml"
Call ReleaseObjects
Set oDoc = New DOMDocument
oDoc.resolveExternals = True
' Create processing instruction and document root
Set oNode = oDoc.createProcessingInstruction("xml", "version='1.0'")
Set oNode = oDoc.insertBefore(oNode, oDoc.childNodes.Item(0))
' Create document root
Set oRoot = oDoc.createElement("Root")
Set oDoc.documentElement = oRoot
oRoot.setAttribute "xmlns:dt", "urn:schemas-microsoft-com:datatypes"
' Add a few simple nodes with different datatypes
Set oNode = oDoc.createElement("Document")
oNode.Text = "Demo"
oRoot.appendChild oNode
Set oNode = oDoc.createElement("CreateDate")
oRoot.appendChild oNode
Set oEle = oNode
' Use DataType so MSXML will validate the data type
oEle.dataType = "date"
oEle.nodeTypedValue = Now
Set oNode = oDoc.createElement("bgColor")
oRoot.appendChild oNode
Set oEle = oNode
' Use DataType so MSXML will validate the data type
oEle.dataType = "bin.hex"
oEle.Text = &HFFCCCC
Set oNode = oDoc.createElement("Data")
oRoot.appendChild oNode
Set oEle = oNode
' Use DataType so MSXML will validate the data type
oEle.dataType = "bin.base64"
' Read in the data
oEle.nodeTypedValue = ReadBinData(DOCINPATH)
' Save xml file
oDoc.save XMLOUTPATH
MsgBox XMLOUTPATH & " is created for you."
End Sub
Private Sub Command2_Click()
'DOCOUTPATH = Text1.Text & ".doc"
'XMLOUTPATH = Label1.Caption
If Text3 = "" Then
MsgBox "您还没有选择路径"
Exit Sub
End If
If Text4 = "" Then
MsgBox "您还没有选择路径"
Exit Sub
End If
Set oDoc = New DOMDocument
If oDoc.Load(XMLOUTPATH) = True Then
'Save the Doc as another file
WriteBinData DOCOUTPATH
MsgBox DOCOUTPATH & " is created for you."
Else
MsgBox oDoc.parseError.reason
End If
End Sub
Private Sub Command3_Click()
xmldlg.Filter = "xml文件(*.xml)|*.xml"
xmldlg.ShowOpen
XMLOUTPATH = xmldlg.FileName
Text3 = XMLOUTPATH
End Sub
Private Sub Command4_Click()
worddlg.Filter = "word文件(*.doc)|*.doc"
worddlg.ShowSave
DOCOUTPATH = worddlg.FileName
'MsgBox DOCOUTPATH
Text4 = DOCOUTPATH
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
Label1.Caption = CurDir & "/" & File1.FileName
End Sub
Private Sub Command5_Click()
worddlg1.Filter = "word文件(*.doc)|*.doc"
'worddlg1.Action = 1
worddlg1.ShowOpen
DOCINPATH = worddlg1.FileName
Text1 = DOCINPATH
End Sub
Private Sub Command6_Click()
xmldlg1.Filter = "xml文件(*.xml)|*.xml"
'xmldlg1.Action = 2
xmldlg1.ShowSave
XMLOUTPATH = xmldlg1.FileName
Text2 = XMLOUTPATH
End Sub
Private Sub Form_Unload(Cancel As Integer)
ReleaseObjects
'xmldlg.Action = 1
'worddlg.Action = 2
' worddlg.Filter = "word文件(*.doc)|*.doc"
End Sub
Private Sub ReleaseObjects()
Set oDoc = Nothing
End Sub
我编写后发现XML里面全是乱码,而不是WORD文档里的内容,小弟是在搞不清是怎么回事,请各位高手指点迷经,使XML里转换出WORD里面的内容。。。。。。。。。