求助 用VB编写的XML与WORD文档转换中遇到的问题,请高手指点!!!

原创 2005年05月29日 09:25:00

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里面的内容。。。。。。。。。

使用Jacob批量转换word为txt、pdf、xps、html、xml等文档

Jacob全称位java com bridge,通过该插件,可以使用Java语言编写程序,调用COM、ActiveX组件来操作Windows本地程序。 参考一位网友的例子,我写了一个程序,用来将wo...

求助:Three20 使用TTURLXMLResponse 获取XML问题

求助:Three20 使用TTURLXMLResponse 获取XML问题    在写一个xml读取数据的程序。使用了 TTURLXMLResponse 这个 Three...
  • xbiii3s
  • xbiii3s
  • 2011年08月13日 10:49
  • 803

求助Spring mvc xml/json多视图返回json数据问题

各位IT界的兄弟姐妹你们好: 小弟最近做一个Spring mvc 支持返回xml/json多视图问题: 请求URL:http://localhost:8080/GraphicService/g...

VB控制Word文档实例精选二

1、返回Word文档的段落文字,控制分页,设置页眉和页脚 '先引用Microsoft Word 11.0 Object Library Option Explicit Dim WordApp...

VB.NET 创建WORD文档

    1、添加引用->COM->Microsoft Word 11.0 Object Library    2、Imports Word   ''' ''' 创建word文档 ...

06毕业设计 - VB导出word文档

Private Sub docout_Click()       '导出WORD按钮  If rs1.RecordCount   MsgBox "导出失败,当前列表中没有记录!"  outstate1...

使用VB对word文档进行格式调整

在word文档中,有时候会出现大量表格,在需要调整格式时,只能一个一个手动刷格式,这样不仅费时而且很费力。本人在写文档时,碰到这种情况,熬夜干活儿,太累。所以下定决心,抽点时间研究WORD中的宏编程。...

Spire.Doc 教程:从C#和VB.NET的word文档中删除形状

Spire.Doc 是一个MS Word 组件,使用户可以直接执行各种Word文档处理任务,本文介绍了如何从C#和VB.NET的word文档中删除形状。...

PDM转换成Word文档或者XML文档

1、打开数据库设计文档pdm,选择模板。 2、选择模板 3、选择好模板后,返回pdm文档 4、单击Generate Report,选择RTF格式 5、确定后即可保存为word文档 小注: 保...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:求助 用VB编写的XML与WORD文档转换中遇到的问题,请高手指点!!!
举报原因:
原因补充:

(最多只允许输入30个字)