Using VB to generate XML files for Word documents (or other binary data) and convert them to each other

Source: Internet
Author: User
Tags end integer processing instruction string version access
word|xml| binary | Generate xml| Data | conversion using VB to generate XML files for Word documents (or other binary data) and convert them to each other1. Establish a new VB Project 2.    Reference Microsoft XML, version 2.0 or above 3. Create a button on a form Form1 CmdcreatexmlAnd cmdgetbinaryCode: Option Explicit
Dim odoc as DOMDocument
Dim Docinpath as String
Dim Xmloutpath as String
Dim Docoutpath as String

Private Sub Cmdcreatexml_click ()

Dim Oele as IXMLDOMElement
Dim Oroot as IXMLDOMElement
Dim Onode as IXMLDOMNode

Docinpath = App.Path & "Docinput.doc"
Xmloutpath = App.Path & "Xmlouput.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 ' 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 ' 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 ' 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 to you."

End Sub

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-1)
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 are nothing) Then

' Get the data
Set Onode = ODoc.documentElement.selectSingleNode ("/root/data")

' Make sure your 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 Cmdgetbinary_click ()

Docoutpath = App.Path & "Docoutput.doc"

Set odoc = New DOMDocument

If odoc.load (Xmloutpath) = True Then
' Save the Doc as another file
Writebindata Docoutpath

MsgBox Docoutpath & "is created to you."
Else
MsgBox ODoc.parseError.reason
End If
End Sub

Private Sub form_unload (Cancel as Integer)
Releaseobjects
End Sub

Private Sub releaseobjects ()
Set odoc = Nothing
End Sub 4. Create a Word document Docinput.doc.
5. Save the document in the engineering directory 6.Run Program click Cmdcreatexmlbutton. An XML file Xmlouput.xml is established.
Click cmdgetbinarybutton to generate a Word document DOCOUTPUT.DOC.
As in the above method, you can also save any binary data as XML, and then regenerate the binary data that can be used for web transmissions, and so on, where you can use XMLHTTP.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.