运行-将图像从 Excel 复制粘贴到 Word 时出现错误 -2147023170 (800706be)

Run-time error -2147023170 (800706be) when copy-pasting images from Excel to Word

我正在尝试 copy/paste 我的 Excel sheet 中的一些图像到 word 文件。有时,它可以完美运行,但我经常 运行 遇到严重的 运行 时间错误“-2147023170 (800706be)”:自动化错误。远程过程调用失败。 消息。 Google 向我表明,我并不是唯一遇到此错误的人,而且它通常是由于例如然而,范围并不完全 defined.I 认为它们在我的项目中。

这是我的代码:

Dim wdDoc As Object
Dim wdApp As Object
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add
Dim wdImg As Object
ThisWorkbook.Worksheets("Backup - Do not change").Shapes("companyLogo").Copy
With wdDoc.Sections(1).Headers(2).Range
    .PageSetup.DifferentFirstPageHeaderFooter = True
    .Paste
    Set wdImg = .InlineShapes(.InlineShapes.Count).ConvertToShape
    With wdImg
       'some specifications
    End With
End With
ThisWorkbook.Worksheets("Backup - Do not change").Shapes("projectLogo").Copy
With wdDoc.Sections(1).Headers(2).Range
    .Paste
    Set wdImg = .InlineShapes(.InlineShapes.Count).ConvertToShape
    With wdImg
       'some specifications
    End With
End With
Application.CutCopyMode = False

错误总是发生在.Paste。我已经尝试了 .PasteSpecial DataType:=8 和其他各种方法,但没有帮助。非常欢迎任何帮助!

好吧,我现在没有直接粘贴到 header 范围,而是在 header 范围内创建了一个 table 并将图像粘贴到两个不同的单元格中。从那时起,我已经成功 运行 代码超过 10 倍,所以这似乎解决了问题(我希望它保持这样)。不过,仍然不确定是什么原因造成的。

我想补充一点,因为这是我遇到类似问题时遇到的第一篇帖子。至少对我来说,解决方案是使用 copypicturepastespecial,如下所述:

Stack Overflow

尽管进行了大量搜索,但直到我开始输入问题并且它出现在类似问题列表中时,我才发现这个线程。希望这可以让其他人免去我要解决的巨大麻烦。