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. Doug Robbins - MVP 716 Reputation points
    2022-03-06T12:26:02.303+00:00

    The 5101 error would indicate that the bookmark is not present.

    Rather than using bookmarks, I would insert a { DOCVARIABLE rg } field at the required location(s) in each of the templates and set the .Value of the associated variable to the Excel Range(rg).Value and include a command to update the fields in the document.

     Dim NewDoc as Document
     Select Case tipo
         Case "typeOne"
             template = "template.dotx"
         Case "typeTwo"
             template = "templateTwo.dotx"
         Case "typeThree"
             template = "templateThree.dotx"
     End Select
     template = temPath + template
     Set NewDoc =.Documents.Add(template)
     rg = "B" + CStr(i)
     NewDoc.Variables("rg").Value = Range(rg).Value
     NewDoc.Range.Fields.Update
    
    0 comments No comments

  2. Jordi Solis 21 Reputation points
    2022-03-07T07:50:58.487+00:00

    As I previously mentioned, I'm using the exact template file for the working and the non working code so the bookmark does exist. If I run again the more basic version the pasteSpecial keeps working whilst it doesn't in the more complicated one.

    In any case, here are a couple screenshots to prove it is there:
    180605-sample1-vba.png

    180612-sample2-vba.png

    I am trying to understand the fields thing, never worked with it and honestly is confusing a.f.

    I honestly don't understand why the same code cannot find the bookmarks that are actually there.

    0 comments No comments

  3. Jordi Solis 21 Reputation points
    2022-03-07T09:00:06.837+00:00

    Well I manage to have it working.

    I replaced:

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

    By

        .ActiveDocument.Bookmarks(9).Range.Text = Range(rg).Value
    

    Since I could watch and see which bookmark has which number it was faster to do than learning fields. Funny thing though, I had to add two extra bookmarks because referring to the last index it would brake with runtime error 5941, but add two more (so you never point to the last one) and it works. I know, it doesn't make any sense.

    I believe the problem in my previous code was using the ".Selection" but I don't really have the time to investigate it.

    Thanks anyway.


  4. Jordi Solis 21 Reputation points
    2022-03-07T10:38:06.637+00:00

    So, last update.

    Looks like word bookmarks indexes are not exactly what the watch says, so I had to do:

    Dim bkm As Bookmark
    
    For Each bkm In .ActiveDocument.Bookmarks
          If bkm.Name = "cuenta" Then bkm.Range.Text = Range(rg).Value
    Next
    

    At least is working now!

    0 comments No comments