0

我正在尝试将一些图像从我的 Excel 工作表复制/粘贴到 word 文件中。有时,它可以完美运行,但我经常遇到严重的运行时错误“-2147023170 (800706be)”:自动化错误。远程过程调用失败。信息。谷歌向我表明,我并不孤单地遇到这个错误,而且它通常是由于没有精确定义例如范围。我认为它们在我的项目中,但是。

这是我的代码:

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和其他各种事情,但没有帮助。非常欢迎任何帮助!

4

2 回答 2

1

Okay so instead of pasting directly to the header range, I now created a table within the header and paste the images into two different cells. Since then I've successfully run the code for more than 10x so it seems like this fixed the issue (I hope it stays like this). Still not sure what caused it, though.

于 2020-05-30T20:19:59.287 回答
1

I want to add to this because this was one of the first posts I come across when having a similar issue. The solution at least for me was using copypicture and pastespecial as explained here:

Stack Overflow

Despite much searching this thread never showed up for me until I started to type a question and it showed up in the list of similar questions. Hopefully this can save someone else the massive headache that this was for me to solve.

于 2021-10-30T01:43:46.403 回答