Use VB to generate a Word document (or other binary data) to transform each other

zhaozj2021-02-08  269

Use VB to generate a Word document (or other binary data) to transform each other

1. Create a new VB project

2. Reference Microsoft XML, version 2.0 or above

3. Create button cmdcreatexml and cmdgetbinary on the form form1

Code:

Option ExplicitDim oDoc As DOMDocumentDim DOCINPATH As StringDim XMLOUTPATH ​​As StringDim DOCOUTPATH ​​As StringPrivate 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.a ppendChild 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 Ole.DataType = "bin.hex" Ole.text = & hffccc set onode = odoc.createElement ("data") oot.Appendchild onode set Ole = 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 subFunction 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 = arrBytesEnd FunctionPrivate 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 = odoc.documentelement.selectsinglenode ("/ root / data")' make six 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 SubPrivate Sub cmdGetBinary_Click () DOCOUTPATH ​​= App.Path & "/DocOutput.doc" Set oDoc = New domdocument if odoc.load (xmloutpath) = true kilobox docoutpath & "IS created for you."

Else msgbox odoc.parseerror.reason end iFend Subprivate Sub Form_Unload (Cancel AS Integer) ReleaseObjectsend Subprivate Sub ReleaseObjects () set odoc = Nothingend Sub4. Create Word Document Docinput.doc.

5. Save the document in the project directory

6. Run the program Click the CMDCReatexml button. An XML file XMlouPut.xml is created. Click the CMDGetBinary button to generate a Word document Docoutput.doc.

According to the above method, any binary data can be saved as XML, and then regenerate binary data.

You can use XMLHTTP where web transmission, etc.

转载请注明原文地址:https://www.9cbs.com/read-3356.html

New Post(0)