我正在尝试将光标移动到word文档的末尾并粘贴图表。但是我在"objSelection.EndKey Unit:=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
发布于 2019-03-27 06:09:03
一种更可靠的到达文档结尾的方法:
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
https://stackoverflow.com/questions/55366500
复制相似问题