Runtime Error 5101, but sometimes only

Jordi Solis 21 Reputation points
2022-03-04T08:58:09.187+00:00

Basically I have different versions of this excel file since I'm adding functionality and newer versions give the error while using the exact same word template.

Working version:

    .Documents.Add "C:\path\template.dotx"
    rg = "B" + CStr(i)
    Range(rg).Copy

    .Selection.Goto wdGoToBookmark, , , "cuenta"
    .Selection.PasteSpecial xlPasteValues

Non-Working version:

    Select Case tipo
        Case "typeOne"
            template = "template.dotx"
        Case "typeTwo"
            template = "templateTwo.dotx"
        Case "typeThree"
            template = "templateThree.dotx"
    End Select
    template = temPath + template
    .Documents.Add template
    rg = "B" + CStr(i)
    Range(rg).Copy

    .Selection.Goto wdGoToBookmark, , , "cuenta"
    .Selection.PasteSpecial xlPasteValues

The template.dotx file is exactly the same in both cases, and I've checked and the path gets generated properly.
The document.add doesn't fail so I imagine the file has been properly added, still, the PasteSpecial fails with Runtime Error 5101 (bookmark not found).

How do I fix this?

Thanks.,

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,720 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
908 questions
0 comments No comments
{count} votes

5 additional answers

Sort by: Most helpful
  1. Jordi Solis 21 Reputation points
    2022-03-08T10:00:03.413+00:00

    Thanks @Doug Robbins - MVP !

    Almost, the InsertBefore would keep the text on the bookmark so I did:

    .ActiveDocument.Bookmarks("cuenta").Range.PasteSpecial xlPasteValues

    Which worked like a charm.

    Thanks!!

    0 comments No comments