• 网站地图|收藏本站|数学学习|学习方法|电脑学习|教学大全|生活常识|句子大全|管理资料下载|范文大全
  • 用vb将word文档生成xml文件并互相转换

    时间:10-14 10:27:50来源:http://www.laixuea.com 电脑技巧阅读:8527

    概要:1. 建立一个新的vb工程。2. 引用 Microsoft XML,版本 2.0 或以上。3. 在窗体form1上建立按钮 cmdCreateXML 和 cmdGetBinary代码:Option ExplicitDim oDoc As DOMDocumentDim DOCINPATH As StringDim XMLOUTPATH As StringDim DOCOUTPATH As StringPrivate Sub cmdCreateXML_Click()Dim oEle As IXMLDOMElementDim oRoot As IXMLDOMElementDim oNode As IXMLDOMNodeDOCINPATH = App.Path & “DocInput.doc”XMLOUTPATH = App.Path & “XmlOuput.xml”Call ReleaseObjectsSet oDoc = New DOMDocumentoDoc.resolveExternals = True注释

    用vb将word文档生成xml文件并互相转换,标签:电脑技巧大全,电脑技术,http://www.laixuea.com

    1. 建立一个新的vb工程。

    2. 引用 Microsoft XML,版本 2.0 或以上。

    3. 在窗体form1上建立按钮 cmdCreateXML 和 cmdGetBinary

    代码:

    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 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

    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 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 cmdGetBinary_Click()

    DOCOUTPATH = App.Path & “DocOutput.doc”

    [1] [2]  下一页


    Tag:电脑技巧电脑技巧大全,电脑技术电脑学习 - 电脑技巧