View Single Post
 
Old 09-30-2021, 02:00 AM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

How is this related to the original question where you wanted to "add a footnote reference in the bookmark [SymbolAst]"?

This new code has nothing to do with footnotes.

Looking at your new code, I can see what the problem is. The issue is NOT what you think it is.

When there is no separation between bookmarks AND you replace the contents of the first one with text, that text is added INSIDE the second bookmark (since inserting content immediately in front of a bookmark means the bookmark expands to include this content). This means that the next step of the macro replaces both bookmarks with the second replacement contents and restores only the second bookmark.

To avoid this issue completely, I use Content Controls and replace the content inside them. This is far easier and less prone to odd behaviour.

If you want to stay with the bookmarks method then you should change the approach a little bit. This code variation will work without destroying the bookmarks.
Code:
Sub BMTest()
  UpdateBookmark "BM1", "2021"
  UpdateBookmark "BM2", "52"
  UpdateBookmark "BM3", "10x"
End Sub

Private Sub UpdateBookmark(sBkName As String, sVal As String)
  Dim aRng As Range, aDoc As Document, aRng2 As Range
  Set aDoc = ActiveDocument
  If aDoc.Bookmarks.Exists(sBkName) Then
    Set aRng = aDoc.Bookmarks(sBkName).Range
    aRng.InsertBefore sVal
    aRng.MoveStart Unit:=wdCharacter, Count:=Len(sVal)
    aRng.Text = ""
  Else
    Debug.Print "Bookmark not found: " & sBkName
  End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote