Reputation: 335
I am trying to copy/paste some images from my Excel sheet to a word file. Sometimes, it works flawlessly, but often I run into the great Run-time error '-2147023170 (800706be)': Automation error. The remote procedure call failed. message. Google showed my that I'm not alone with this error and that it often results from the fact that e.g. ranges are not precisely defined.I think they are in my project, however.
Here's my code:
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
The error always occurs at .Paste
. I already tried .PasteSpecial DataType:=8
and various other things but it didn't help. Any help is very welcome!
Upvotes: 0
Views: 1916
Reputation: 234
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:
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.
Upvotes: 1
Reputation: 335
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.
Upvotes: 1