如何将格式文本从Word传输到Excel?

时间:2019-05-08 11:39:35

标签: excel vba

以下代码将数据从MS Word(内容控件)复制到Excel。但是,当我将带有项目符号的文本复制并粘贴到Excel中时,它会删除项目符号并仅粘贴文本。

如何从内容控件复制项目符号?

Sub getWordFormData()
Dim wdApp As New Word.Application
Dim myDoc As Word.Document
Dim CCtl As Word.ContentControl
Dim myFolder As String, strFile As String
Dim myWkSht As Worksheet, i As Long, j As Long

myFolder = "enter folder path" '<< enter you folder path for the word document

If Dir(myFolder & "\" & "*.*") = "" Then
    Application.ScreenUpdating = True
    MsgBox "The folder is empty."
    Exit Sub
End If

Set myWkSht = ActiveSheet
ActiveSheet.Cells.Clear
Range("A1") = "Test 1"
Range("B1") = "Test 2"
Range("A1:B1").Font.Bold = True

i = myWkSht.Cells(myWkSht.Rows.Count, 1).End(xlUp).Row
strFile = Dir(myFolder & "\*.docx", vbNormal)

While strFile <> ""
    i = i + 1

    Set myDoc = wdApp.Documents.Open(Filename:=myFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)

    With myDoc
        j = 0
        For Each CCtl In .ContentControls
            j = j + 1
            myWkSht.Cells(i, j) = CCtl.Range.Text
        Next
        myWkSht.Columns.AutoFit
    End With

    myDoc.Close SaveChanges:=False
    strFile = Dir()
Wend

wdApp.Quit
Set myDoc = Nothing: Set wdApp = Nothing: Set myWkSht = Nothing
Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

尝试使用此块

With myDoc
    j = 0
    For Each CCtl In .ContentControls
        j = j + 1
        CCtl.Range.Copy
        myWkSht.Cells(i, j).PasteSpecial Paste:=xlPasteValues
    Next
    myWkSht.Columns.AutoFit
End With