将表格从Word复制到Excel而不拆分?

时间:2018-06-27 13:15:06

标签: excel vba ms-word

我使用的代码是:

Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel

wdFileName = Application.GetOpenFilename("Word files ,*.doc;*.docx;*.docm", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
    TableNo = wdDoc.TAbles.Count
    If TableNo = 0 Then
        MsgBox "This document contains no tables", _
        vbExclamation, "Import Word Table"
    ElseIf TableNo > 1 Then
        TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
        "Enter table number of table to import", "Import Word Table", "1")
    End If
    With .TAbles(TableNo).Range.Copy
Range("A1").Activate
Application.CommandBars.ExecuteMso "PasteSourceFormatting"
    End With
End With

Set wdDoc = Nothing

End Sub

问题是,当表粘贴到excel中时,列A中的值被拆分为多个单元格,而我一生都无法弄清楚如何在不拆分单元格的情况下粘贴并保持源格式(即,粗体文本,普通文本与单词表中的文本相同)

谢谢

0 个答案:

没有答案