VBA – Excel + Word集成 – 没有理由的代码循环

我正在创build一个Excel文档,其中包括将几个数据数组复制到一个Word文件中。

一切工作绝对正常,除了粘贴我感兴趣的select,它将无限粘贴,直到我介入通过任务pipe理器取下Word。

就我所知,没有理由让它循环。 所以我对接下来要做的事情一无所知,而且我希望你们能够清楚我做错了什么。 这个概念如下:

Sub TestA() 'Word objects. Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim wdbmRange As Word.Range 'Excel objects. Dim wbBook As Workbook Dim wsSheet As Worksheet Dim rnReport As Range 'Opening app and document Set wdApp = New Word.Application Set wdDoc = wdApp.Documents.Open("C:\Users\RCO1\Desktop\Teste VBA\2. Conceptual Testing\Export\XLWDTST.docx") Set wdbmRange = wdDoc.Bookmarks("TableInsertion").Range 'Selecting array Sheets("ExportMe").Select Range("A1").Select Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.Copy 'Pasting data With wdbmRange .PasteSpecial '<------- it simply goes back up to the beginning of the code at this point CutCopyMode = False End With 'Closing and saving wdDoc.Save wdDoc.Close wdApp.Quit Set wdApp = Nothing Set wdDoc = Nothing End Sub 

请注意,我不能简单地将这个数组转换成一个表格,以便说出格式化的原因。

在做了一些调查之后,我得到了以下简单而有效的一段代码:

 Set wdApp = CreateObject("Word.Application") Filepath = Sheets("Proposal Generator").Range("I9") Set wdDoc = Word.Documents.Open(Filepath) with wdDoc Sheets("Shhet1").Cells(1).CurrentRegion.Copy .Bookmarks("TableInsertion").Range.PasteExcelTable 0, 0, 0 .SaveAs (TxtSaveFolder.Text & "\" & TxtFileName.Text) End With 

我希望这适用于任何试图导出表的人。