VBA读取word中的内容到Excel中

VBA读取word中的内容到Excel中,这类代码的思路都是一致的,包括ADO、SQL、DELPHI、VB……
先声明、创建一个对象 → 用此对象打开需要操作的文件、数据库 → 从头到尾循环一遍做某事 → 关闭文件 → 关闭对象 → 释放对象。
Public Sub Duqu()
    Dim myFile As String
    Dim docApp As Word.Application
    Dim docRange As Word.Range
   
    myFile = ThisWorkbook.Path & "\Word文档的名字"    '指定Word文档
    Set docApp = New Word.Application
    docApp.Documents.Open myFile
   
    For i = 1 To docApp.ActiveDocument.Paragraphs.Count
    With docApp.ActiveDocument
        'If .Paragraphs.Count >= 4 Then
            Set docRange = .Paragraphs(i).Range
            cp = cp & docRange
        'End If
    End With
    Next i
   
    Range("a1") = cp
   
    docApp.Quit
   
    Set docRange = Nothing
    Set docApp = Nothing
    Set ws = Nothing
    
End Sub
 
另一类方法:
Sub MS_Word()
    Dim wd As Object
    Dim doc As Object
    
    Set wd = CreateObject("Word.Application")       ' 建立Word会话
    wd.Visible = False                              ' 设定Word应用程序为不可见状态

    Set doc = wd.Documents.Open(ThisWorkbook.Path & Application.PathSeparator & "001 安全管理程序.Doc")    ' 打开当前目录下指定Word文件
    doc.Tables(1).Cell(1, 2).Range = Cells(3, 1).Value                                                  ' Word文件第一个表的1行2列内容改为当前工作表的A3的内容
    doc.Save    ' 保存Word文件
    doc.Close   ' 关闭文件
    wd.Quit     ' 退出Word会话
    Set doc = Nothing
    Set wd = Nothing
End Sub
posted @ 2019-06-28 15:53  inocalli  阅读(8498)  评论(0编辑  收藏  举报