尝试使用VBA到Word文档中的书签时出错-运行时错误'438'对象不支持此属性或方法

时间:2019-03-26 18:53:29

标签: excel vba

我正在尝试在excel中创建聊天记录并将其粘贴到Word文档中书签“ 1”的位置。

该宏将死于“ Selection.GoTo What:= wdGoToBookmark,名称:=“ 1”“

Sub Macro1()

    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
'open the word file 
    objWord.Documents.Open "N:\Template\Template.docx"
    objWord.Visible = True

'create and format the chart
    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"

'copy the chart
    ActiveChart.Parent.Copy

    objWord.Activate
    objWord.Visible = True
    Set objSelection = objWord.Selection
   'go to the bookmark in the word doc
    Selection.GoTo What:=wdGoToBookmark, Name:="1"

    'paste the chart
    objWord.Selection.Paste

End Sub

1 个答案:

答案 0 :(得分:0)

Selection.GoTo What:=wdGoToBookmark, Name:="1"

应该是

objSelection.GoTo What:=wdGoToBookmark, Name:="1"

您是否在VBA项目中添加了对Word对象模型库的引用?如果不是这样,则不能使用wdGoToBookmark之类的单词中的常量-您需要在Excel VBA中定义它们,或者将其替换为它们的数值。