That should not be necessary. The following will do what you want.
.ActiveDocument.Bookmarks("cuenta").Range.InsertBefore Range(rg).Value
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.,
That should not be necessary. The following will do what you want.
.ActiveDocument.Bookmarks("cuenta").Range.InsertBefore Range(rg).Value
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
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:
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.
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.
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!