ActiveDocument.Selection.PasteSpecial error: Run-time error 438: Object doesn't support this property or method
ActiveDocument.Selection.PasteSpecial error: Run-time error 438: Object doesn't support this property or method
我正尝试在 Excel 中编写 VBA 代码来更新 Word 报告。
我正在从 Stack Overflow 复制代码片段并进行改编。
我的所有代码都有效,我单独尝试过,但是这一行:
ActiveDocument.Selection.PasteSpecial
它给出了以下错误代码:
Run-time error 438: Object doesn't support this property or method.
完整代码:
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
您的代码相当不一致,最初我被您对使用早期绑定还是后期绑定的混淆弄糊涂了,并且误读了您的代码。
ActiveDocument.Selection.PasteSpecial
实际上应该是 wd.Selection.PasteSpecial
。 Selection
是 Application
而非 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
编辑:您可以找到早期和晚期绑定的解释
进一步编辑:上面的代码是正确的,应该可以工作。如果您还声明 ObjDoc As Word.Document
而不是 ObjDoc As Object
将会对您有所帮助,因为这将使您能够访问 Intellisense。
您的代码中还有一个未声明的变量,因此您应该将 Dim i As Long
添加到您的变量声明中。您可以通过在代码模块顶部添加 Option Explicit 来避免这些问题。当您有未声明的变量时,这将阻止您的代码编译。要将其自动添加到新模块,请打开 VBE 并转到工具 |选项。在“选项”对话框中,确保选中“要求变量声明”。
进一步编辑以回应评论:
有了 re-read 你的代码,我猜你试图粘贴的书签位置包含图片内容控件。这将解释“此命令不可用”,因为选择性粘贴不适用于图片内容控件。
进一步假设这是名为 '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
这将导致内容控件包含图表而不是图像,但图表将不再链接到它的数据。
按照我之前的评论 Dim imageControl As ContentControl
应该是 Dim imageControl As Word.ContentControl
。最好养成始终指定对象类型来自哪个库的习惯,因为某些对象存在于多个库中,例如Selection
.
作为(希望)最后一点,With imageControl.Range.InlineShapes(i)
会出错,因为图片内容控件只能包含一个 InlineShape
并且 i
从 1 到 8。
我正尝试在 Excel 中编写 VBA 代码来更新 Word 报告。
我正在从 Stack Overflow 复制代码片段并进行改编。
我的所有代码都有效,我单独尝试过,但是这一行:
ActiveDocument.Selection.PasteSpecial
它给出了以下错误代码:
Run-time error 438: Object doesn't support this property or method.
完整代码:
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
您的代码相当不一致,最初我被您对使用早期绑定还是后期绑定的混淆弄糊涂了,并且误读了您的代码。
ActiveDocument.Selection.PasteSpecial
实际上应该是 wd.Selection.PasteSpecial
。 Selection
是 Application
而非 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
编辑:您可以找到早期和晚期绑定的解释
进一步编辑:上面的代码是正确的,应该可以工作。如果您还声明 ObjDoc As Word.Document
而不是 ObjDoc As Object
将会对您有所帮助,因为这将使您能够访问 Intellisense。
您的代码中还有一个未声明的变量,因此您应该将 Dim i As Long
添加到您的变量声明中。您可以通过在代码模块顶部添加 Option Explicit 来避免这些问题。当您有未声明的变量时,这将阻止您的代码编译。要将其自动添加到新模块,请打开 VBE 并转到工具 |选项。在“选项”对话框中,确保选中“要求变量声明”。
进一步编辑以回应评论: 有了 re-read 你的代码,我猜你试图粘贴的书签位置包含图片内容控件。这将解释“此命令不可用”,因为选择性粘贴不适用于图片内容控件。
进一步假设这是名为 '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
这将导致内容控件包含图表而不是图像,但图表将不再链接到它的数据。
按照我之前的评论 Dim imageControl As ContentControl
应该是 Dim imageControl As Word.ContentControl
。最好养成始终指定对象类型来自哪个库的习惯,因为某些对象存在于多个库中,例如Selection
.
作为(希望)最后一点,With imageControl.Range.InlineShapes(i)
会出错,因为图片内容控件只能包含一个 InlineShape
并且 i
从 1 到 8。