要将表格列表从Excel复制到Word

lqfhib0f  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(110)

我是Excel的新手,对编码也不太了解,正面临着这个问题-
我试图复制列表中的表格在一个单一的工作表到Word中,但表格被粘贴在文件的开头,有没有办法粘贴在书签,也可以代码包含的方式来格式化从书签本身。

Sub ListObjectToWord_Multi()

    'Declare Word Variables
    Dim WrdApp As Object
    Dim WrdDoc As Word.Document
    Dim WrdTbl As Word.Table
    
    'Declare Excel Variables
    Dim ExcLisObj As ListObject
    Dim WrkSht As Worksheet
    
    'Create a new instance of word
    Set WrdApp = CreateObject("Word.Application")
    
    With WrdApp
        .Visible = True
        .Documents.Open Range("F3").Value
        .Activate
    
   'Loop through all the Worksheets in Active Workbook
    For Each WrkSht In ThisWorkbook.Worksheets
        
        'Loop thorugh all objects on the active sheet
        For Each ExcLisObj In WrkSht.ListObjects
        
        'Copy the List Object
        ExcLisObj.Range.Copy
        
        'Pause the excel Application for few seconds
        Application.Wait Now() + #12:00:03 AM#
        
        'Go to New Page
        WrdApp.Selection.GoTo What:=wdGoToBookmarks, Which:=wdGoTo
        
        'Paste List Objects into the word document
        With WrdApp.Selection
             .PasteExcelTable LinkedToExcel:=True, WordFormatting:=True, RTF:=True
        End With
        
        'Clear my Clipboard
        Application.CutCopyMode = False
        
        Next
    
    'Go to First Page
    WrdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst
    
    Next
    
    End With
    
End Sub

字符串
这是我使用的代码,请帮助提供任何见解,可以解决我的问题

zbdgwd5y

zbdgwd5y1#

请试试看。

Option Explicit
Sub ListObjectToWord_Multi()
    'Declare Word Variables
    Dim WrdApp As Object
    Dim WrdDoc As Word.Document
    Dim WrdTbl As Word.Table
    'Declare Excel Variables
    Dim ExcLisObj As ListObject
    Dim WrkSht As Worksheet
    Dim i As Long, bmCount As Long
    'Create a new instance of word
    Set WrdApp = CreateObject("Word.Application")
    '    Set WrdApp = GetObject(, "Word.Application")
    With WrdApp
        .Visible = True
        .Documents.Open Range("F3").Value
        Set WrdDoc = .ActiveDocument
        bmCount = WrdDoc.Bookmarks.Count
    End With
    i = 1
    'Loop through all the Worksheets in Active Workbook
    For Each WrkSht In ThisWorkbook.Worksheets
        'Loop thorugh all objects on the active sheet
        For Each ExcLisObj In WrkSht.ListObjects
            If i > bmCount Then
                MsgBox "The count of bookmarks is less than listbox."
                Exit Sub
            Else
                WrdApp.Selection.GoTo What:=wdGoToBookmark, Name:=WrdDoc.Bookmarks(i).Name
            End If
            'Paste List Objects into the word document
            With WrdApp.Selection
                .Collapse wdCollapseEnd
                ExcLisObj.Range.Copy
                'Pause the excel Application for few seconds
                ' Application.Wait Now() + TimeSerial(0, 0, 1)
                .PasteExcelTable LinkedToExcel:=True, WordFormatting:=True, RTF:=True
            End With
            'Clear my Clipboard
            Application.CutCopyMode = False
            i = i + 1
        Next
    Next
    'Go to First Page
    WrdApp.Selection.HomeKey Unit:=wdStory
End Sub

字符串

相关问题