我正在尝试将光标移动到word文档的末尾并粘贴图表。但是我在“ objSelection.EndKey单元:= wdStory”这一行上收到“错误参数”的错误消息
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Documents.Open "N:\Template\Template.docx"
objWord.Visible = True
ThisWorkbook.Activate
...
然后我在while循环中遇到了这个问题:
Do While IsEmpty(Range("A1").Value) = False
...
Range("D:D,F:F").Select 'select date and value
Range("F1").Activate
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.ChartTitle.Text = subregion & " " & typ & " " & cate
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlCategory).Select
ActiveChart.PlotArea.Select
ActiveChart.FullSeriesCollection(1).Select
ActiveChart.FullSeriesCollection(1).ApplyDataLabels
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.NumberFormat = "m/yyyy"
ActiveChart.Parent.Copy
objWord.Activate
objWord.Visible = True
Set objSelection = objWord.Selection
objSelection.EndKey Unit:=wdStory
objWord.Selection.Paste
...
loop
同一代码在较小的示例中起作用,而没有while循环。现在,我尝试将代码与while循环合并,而oibjSelection.EndKey行会杀死宏
有效的我的较小样本:
Sub Macro1()
'
' Macro1 Macro
'
'
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
'Change the directory path and file name to the location'of the document you want to open from Excel
objWord.Documents.Open "N:\Template\Template.docx"
objWord.Visible = True
ThisWorkbook.Activate
Range("A:A,C:C").Select
Range("C1").Activate
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A:$A,Sheet1!$C:$C")
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.NumberFormat = "m/yyyy"
ActiveChart.Parent.Copy
objWord.Activate
objWord.Visible = True
Set objSelection = objWord.Selection
objSelection.EndKey Unit:=wdStory
objWord.Selection.Paste
End Sub
答案 0 :(得分:1)
到达文档末尾的更可靠方法:
Dim rng as Object ' or Word.Range
Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
rng.Paste
但是请注意,如果Word是从另一个应用程序(例如Excel)中自动执行的,例如并且,则该项目中没有对Word对象库的引用,则不能使用特定于Word的枚举因为其他应用程序中的VBA无法识别它。在这种情况下,需要提供等效的数字而不是wd
枚举。所以:
rng.Collapsed 0