copy-paste tables from word to excel

后端 未结 1 1769
栀梦
栀梦 2020-12-11 08:08

I have a word document which is updated periodically. I can go into that Word document, select the contents of an entire table and copy, then go into an Excel spreadsheet a

相关标签:
1条回答
  • 2020-12-11 08:20

    Something like this:

    Sub read_word_document()
    
    Const DOC_PATH As String = "Z:\mydir\myfile1.DOC"
    
    Dim sht As Worksheet
    Dim WordDoc As Word.Document
    Dim WordApp As Word.Application
    Dim i As Long, r As Long, c As Long
    Dim rng As Range, t As Word.Table
    
        Set WordApp = CreateObject("Word.Application")
        WordApp.Visible = False
        Set WordDoc = WordApp.Documents.Open(DOC_PATH, ReadOnly:=True)
    
        Set sht = Sheets("Temp")
        Set rng = sht.Range("A1")
        sht.Activate
    
        For Each t In WordDoc.Tables
            t.Range.Copy
            rng.Select
            rng.Parent.PasteSpecial Format:="Text", Link:=False, _
                        DisplayAsIcon:=False
            With rng.Resize(t.Rows.Count, t.Columns.Count)
                .Cells.UnMerge
                .Cells.ColumnWidth = 14
                .Cells.RowHeight = 14
                .Cells.Font.Size = 10
            End With
    
            Set rng = rng.Offset(t.Rows.Count + 2, 0)
        Next t
        WordDoc.Close
        WordApp.Quit
    End Sub
    
    0 讨论(0)
提交回复
热议问题