'Word 2016 Error copying shape to header. Method 'Paste' of object 'Range' failed (80010108)

I have Word 2016 MSO (16.0.4266.1001), 64-bit

I have a document that has two sections with different headers. In the first section, in the header, there is a shape with text that needs to be copied into the header of the second section.

In Word 2010 I had some code that worked.

Sub TEST()    
    Set hHeaderRange = ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary).Range
    ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.Item(1).Select
    Selection.Copy
    hHeaderRange.Paste
End Sub

But in Word 2016, this code gives an error (see below) on the line hHeaderRange.Paste enter image description here

When you click the End button, Word crashes and restarts in an attempt to restore the last saved version of the document. Moreover, if you stop the code before the line hHeaderRange.Paste, then you can go to the header of the second section and click Paste, and the figure will be inserted. I also tried PasteSpecial, same thing. If you specify in the code to insert a shape on the page, there are no problems.

Can anyone please tell me how to solve this problem?

The task is simple, you need to copy the shape from the header of one section to the header of another section.

The strangest thing is that Word is crashing!

If anyone has other versions of Word 2016 (not 16.0.4266.1001) or Word 2019, please check the code.



Solution 1:[1]

I can confirm that the code you are using causes Word to crash (Version 2201 Build 16.0.14827.20158)

As there is no other content in the header you are copying from there is a workaround that is actually better than your original method. A Range object has a FormattedText property (read/write) that can be used instead of copying and pasting. So your code should be:

Sub TEST()
    Dim hdr1 As Range, hdr2 As Range
    Set hdr2 = ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary).Range
    Set hdr1 = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
    hdr2.FormattedText = hdr1.FormattedText
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Timothy Rylatt