宏是用Excel编写的。我已经将表格从excel复制到第一页,然后我想将表格插入到第2页的单词中。但是从下面设置的“MyRange”返回空,而表格中的1行没有创建。
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(str_path_folder & "\" & AdID & ".dotm")
objWord.Visible = True
objDoc.Activate
AppActivate AdID & ".dotm"
Sheets("Meta").Select
Range("B1:E" & LastRow).Select
Selection.Copy
objWord.Selection.PasteExcelTable False, False, False
objDoc.Tables(1).AutoFitBehavior wdAutoFitContent
objWord.Selection.InsertBreak.Type = wdPageBreak
Set MyRange = ActiveDocument.Content
MyRange.collapse Direction:=wdCollapseEnd
ActiveDocument.Tables.Add Range:=MyRange, NumRows:=1, _
NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
此外,如果我在下面使用它,它将替换第1页中已经复制的表:
objDoc.Tables.Add Range:=objDoc.Range, NumRows:=3, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
答案 0 :(得分:0)
获取文档结尾的另一种方法是objDoc.Range(objDoc.Range.End - 1)
objDoc.Range.PasteExcelTable 0, 0, 0
objDoc.Range(objDoc.Range.End - 1).InsertBreak
objDoc.Tables.Add objDoc.Range(objDoc.Range.End - 1), 2, 2
如果您在Word中录制宏,则会得到类似这样的内容
Selection.PasteExcelTable False, False, True
Selection.InsertBreak Type:=wdPageBreak
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed