#1
|
|||
|
|||
Add footnote in a bookmark
Hi there
Trying to add a footnote reference in the bookmark [SymbolAst] I basically have three consecutive bookmarksm: [SymbolA][SymbolB][SymbolAst] This code will add the asterisk but before SymbolB? I have tried r.End and r.End-1 also Code:
If CheckBox13 = True Then If BookmarkExists("SymbolAst") Then Dim r As Range Set r = ActiveDocument.Bookmarks("SymbolAst").Range ActiveDocument.Footnotes.Add Range:=r, _ Text:=TextBox4.Value, _ Reference:="*" Else MsgBox "Nope" End If End If |
#2
|
||||
|
||||
Try this
Code:
Sub aTest() Dim r As Range If ActiveDocument.Bookmarks.Exists("SymbolAst") Then Set r = ActiveDocument.Bookmarks("SymbolAst").Range r.Collapse Direction:=wdCollapseStart ActiveDocument.Footnotes.Add Range:=r, Text:="This is a test", Reference:="*" Else MsgBox "Nope" End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thanks for your reply
I think I actually I found what the problem is but got no solution so far. I use several bookmarks, some separated, others not... KTP/EG.[SymbolA][SymbolPart]/[SymbolB]/[SymbolC][SymbolAst] My code works great if bookmarks are separated with a character... But if bookmarks are attached/joined (as for [SymbolA][SymbolPart]) then everything get messed up. Inserting "Part" will work but the "Part" bookmark will be deleted and content added into SymbolA. Same with "SymbolAst", adding content in the bookmark will result in "SymbolAst" being deleted and content added in "SymbolC") I think it has to do on the fact that writing bookmark is actually equivalent to deleting it and readding it, something is happening there and it is messing up bookmarks. Here is a dotm with a small macro and form (RunIt). This is the only thing that is preventing me to introduce the macros to my colleague so I am really eager to find a solution here. Thanks a lot |
#4
|
||||
|
||||
I'm not seeing the issue you describe when using my macro in that test document. It had 5 bookmarks before and after running the macro.
The code is not deleting and re-adding a bookmark, it is just adding a footnote anchor at the starting position of a bookmark exists. If the bookmark encloses a range, then the footnote will be inside the bookmark. If the bookmark is a zero length range then the footnote gets added after the bookmark.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Thanks Guessed for your reply. Well, I have failed.
Here a simpler macro. Code:
Option Explicit Sub BMTest() Dim tmp As String tmp = "2021" WB "BM1", tmp tmp = "52" WB "BM2", tmp tmp = "10x" WB "BM3", tmp tmp = "2021" WB "BM4", tmp tmp = "52" WB "BM5", tmp tmp = "10x" WB "BM6", tmp End Sub Public Sub WB(ByVal BName As String, ByVal inhalt As String) If ActiveDocument.Bookmarks.Exists(BName) Then Dim r As Range Set r = ActiveDocument.Bookmarks(BName).Range r.Text = inhalt ActiveDocument.Bookmarks.Add BName, r Else Debug.Print "Bookmark not found: " & BName End If End Sub In word, copy the following and insert the bookmarks Code:
/* This works (note the space)*/ KTP.EG.[BM1] [BM2] [BM3] /* This fails */ KTP.EG.[BM4][BM5][BM6] Code:
KTP.EG.2021 52 10x KTP.EG.10x |
#6
|
||||
|
||||
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 |
#7
|
|||
|
|||
Guessed,
I am sorry mixing two issues together and bringing confusion. Please let me know if there is anything I can do to fix this such as editing the title As for your code, it does the job. I had looked for a solution for days online and found none. Thank you very much for assisting This thread can be marked "resolved". Have a great day |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Footnote references in the footnote section losing their style when cut+pasted from same doc | emblaw | Word | 4 | 12-08-2020 06:23 AM |
Removing line break and indentation between footnote number and footnote text in Word | jaanross | Word | 5 | 02-06-2020 12:04 AM |
REf Fields show Bookmark whole cell when Bookmark is created by code. | pmcpowell | Word VBA | 2 | 11-16-2019 07:05 PM |
Adding footnote number as part of footnote text | NoCalScribe | Word VBA | 3 | 07-15-2019 07:20 PM |
Find Bookmark, move to bookmark, execute code, repeat | raymm3852 | Word VBA | 10 | 04-15-2016 06:21 PM |