【问题标题】:ActiveDocument.Selection.PasteSpecial error: Run-time error 438: Object doesn't support this property or methodActiveDocument.Selection.PasteSpecial 错误:运行时错误 438:对象不支持此属性或方法
【发布时间】:2020-09-15 14:03:10
【问题描述】:

我正在尝试在 Excel 中编写 VBA 代码来更新 Word 报告。

我正在从 Stack Overflow 复制代码片段并对其进行调整。

我所有的代码都在工作,我单独尝试过,但是这一行:

ActiveDocument.Selection.PasteSpecial

它给出以下错误代码:

运行时错误 438:对象不支持此属性或方法。

完整代码:

Sub UpdateReport()
'
' UpdateReport Macro
' Update the monthly ITCV report
'

'
    Dim wd As Object
    Dim ObjDoc As Object
    Dim FilePath As String
    Dim FileName As String
    Dim originalImage As InlineShape
    Dim Lines(1 To 8) As Long
    Lines(1) = 103
    Lines(2) = 107
    Lines(3) = 109
    Lines(4) = 110
    Lines(5) = 115
    Lines(6) = 116
    Lines(7) = 117
    Lines(8) = 121
    FilePath = "C:\Users\nmoesen\Desktop"
    FileName = "Test.docx"

    'check if template document is open in Word, otherwise open it
    On Error Resume Next
    Set wd = GetObject(, "Word.Application")
    If wd Is Nothing Then
        Set wd = CreateObject("Word.Application")
        Set ObjDoc = wd.Documents.Open(FilePath & "\" & FileName)
    Else
        On Error GoTo notOpen
        Set ObjDoc = wd.Documents(FileName)
        GoTo OpenAlready
notOpen:
        Set ObjDoc = wd.Documents.Open(FilePath & "\" & FileName)
    End If

OpenAlready:
    On Error GoTo 0

    'initialise loop
    
    For i = LBound(Lines) To UBound(Lines)
    
        'set value to generate correct chart
        Sheets("Calculations").Range("AE2").Value = Lines(i)
        
        'copy chart from Excel
        Sheets("Calculations").ChartObjects("Chart 1").Chart.ChartArea.Copy

        Set originalImage = ActiveDocument.InlineShapes(i)
        Dim imageControl As ContentControl
        If originalImage.Range.ParentContentControl Is Nothing Then
            Set imageControl = ActiveDocument.ContentControls.Add(wdContentControlPicture, originalImage.Range)
        Else
            Set imageControl = originalImage.Range.ParentContentControl
        End If

        Dim imageW As Long
        Dim imageH As Long
        imageW = originalImage.Width
        imageH = originalImage.Height
        originalImage.Delete
    
        ActiveDocument.Bookmarks("Bookmark" & i).Select
   
        ActiveDocument.Selection.PasteSpecial Link:=False, _
          DataType:=wdPasteMetafilePicture, _
          Placement:=wdInLine, _
          DisplayAsIcon:=False
        
        With imageControl.Range.InlineShapes(i)
            .Height = imageH
            .Width = imageW
        End With
    
    Next i

End Sub

【问题讨论】:

  • 当您已经将ObjDoc 定义为您需要使用的文档时,为什么还要使用ActiveDocument
  • 我会尝试使用它,看看它是否有帮助!编辑:刚刚尝试将其更改为 ObjDoc,得到了同样的错误。

标签: excel vba ms-word


【解决方案1】:

您的代码相当不一致,我最初被您混淆是使用早期绑定还是晚期绑定并误读了您的代码而分心。

ActiveDocument.Selection.PasteSpecial 实际上应该是wd.Selection.PasteSpecialSelectionApplication 的成员,而不是 Document

但是,由于您似乎要粘贴到带有书签的位置,因此您的代码实际上应该是:

'    ActiveDocument.Bookmarks("Bookmark" & i).Select
'
'    ActiveDocument.Selection.PasteSpecial Link:=False, _
'    DataType:=wdPasteMetafilePicture, _
'    Placement:=wdInLine, _
'    DisplayAsIcon:=False

    If ObjDoc.Bookmarks.Exists("Bookmark" & i) then
        ObjDoc.Bookmarks("Bookmark" & i).Range.PasteSpecial Link:=False, _
        DataType:=wdPasteMetafilePicture, _
        Placement:=wdInLine, _
        DisplayAsIcon:=False
    End if

如果您使用后期绑定,您需要保持一致并声明originalImage As Object。如果你使用早期绑定,你需要清楚你的对象类型来自哪个库,所以你应该声明originalImage As Word.InlineShape

编辑:您可以找到早期和后期绑定的解释here

进一步编辑:上面的代码是正确的,应该可以工作。如果您还声明 ObjDoc As Word.Document 而不是 ObjDoc As Object 这将对您有所帮助,因为这将使您能够访问 Intellisense。

您的代码中还有一个未声明的变量,因此您应该在变量声明中添加Dim i As Long。您可以通过在代码模块顶部添加 Option Explicit 来避免这些问题。当您有未声明的变量时,这将阻止您的代码编译。要将其自动添加到新模块中,请打开 VBE 并转到工具 |选项。在“选项”对话框中,确保选中“要求变量声明”。

针对评论的进一步编辑: 重新阅读您的代码后,我猜测您尝试粘贴到的带书签的位置包含图片内容控件。这将解释“此命令不可用”,因为选择性粘贴不适用于图片内容控件。

进一步假设这是名为“imageControl”的对象所引用的图片内容控件,您可以按如下方式修改您的代码:

   '    ActiveDocument.Bookmarks("Bookmark" & i).Select
   '
   '    ActiveDocument.Selection.PasteSpecial Link:=False, _
   '    DataType:=wdPasteMetafilePicture, _
   '    Placement:=wdInLine, _
   '    DisplayAsIcon:=False
   '
   '    With imageControl.Range.InlineShapes(i)
   '        .Height = imageH
   '        .Width = imageW
   '    End With
   
   imageControl.Range.Paste
   With imageControl.Range.InlineShapes(1)
      .Chart.ChartData.BreakLink
      .Height = imageH
      .Width = imageW
   End With

这将导致内容控件包含图表而不是图像,但图表将不再链接到其数据。

按照我之前的cmetsDim imageControl As ContentControl应该是Dim imageControl As Word.ContentControl。最好养成始终指定对象类型来自哪个库的习惯,因为某些对象存在于多个库中,例如Selection.

作为(希望)最后一点,With imageControl.Range.InlineShapes(i) 会出错,因为图片内容控件只能包含一个 InlineShapei 从 1 变为 8。

【讨论】:

  • 您好,我编辑了您所说的部分,并将 ActiveDocument 的所有引用更改为 ObjDoc。我还将 originalImage 声明为 Word.InlineShape。现在他对 If 语句中的所有内容都给出了错误 此命令不可用。我真的不知道什么是早期或晚期绑定。所以我只是复制、粘贴和调整,直到它起作用为止。
  • 我现在上班,明天改,如果可行,我会接受你的回答。感谢所有的工作!
猜你喜欢
  • 1970-01-01
  • 2014-03-21
  • 2023-03-30
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多