简体   繁体   English

我想使用VBA将excel中的命名范围插入到word文件中的特定位置的现有word文件中(它有20个表)

[英]I want to insert a named range from excel to an existing word file at a specific position in the word file (it has 20 tables) using VBA

I have a word file containing over 20 tables. 我有一个包含超过20个表的word文件。 I want to insert a named range (or a table) from excel into the word file just before the 13th table. 我想将excel中的命名范围(或表)插入到第13个表之前的word文件中。 I do not want the new table to merge with any existing tables. 我不希望新表与任何现有表合并。 I am using VBA to do this. 我正在使用VBA来做到这一点。 The problem is the inserted table gets merged with the table(13). 问题是insert表与表(13)合并。

// Yakuza is the name of my named range

Set tbl = ThisWorkbook.Worksheets(Sheet2.Name).Range("Yakuza")
    tbl.Copy
    objDoc.Tables(tblno).Range.InsertBefore (Chr(10))
    objDoc.Tables(13).Range.PasteExcelTable False, False, False

//I have tried this as well: objDoc.Tables(tblno).Range.PasteAndFormat wdFormatOriginalFormatting

To prevent the tables merging, you need to have an ordinary paragraph between them. 要防止表合并,您需要在它们之间有一个普通的段落。 Hence: 因此:

objDoc.Tables(tblno).Range.Characters.First.Previous.InsertBefore vbCr & vbCr
objDoc.Tables(tblno).Range.Characters.First.Previous.Previous.PasteExcelTable False, False, False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM