Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-25-2021, 09:19 AM
TheBigBoss TheBigBoss is offline Add footnote in a bookmark Windows 7 32bit Add footnote in a bookmark Office 2010 32bit
Advanced Beginner
Add footnote in a bookmark
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default 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
Thanks
Reply With Quote
  #2  
Old 09-25-2021, 04:19 PM
Guessed's Avatar
Guessed Guessed is offline Add footnote in a bookmark Windows 10 Add footnote in a bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

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
Reply With Quote
  #3  
Old 09-27-2021, 02:51 AM
TheBigBoss TheBigBoss is offline Add footnote in a bookmark Windows 7 32bit Add footnote in a bookmark Office 2010 32bit
Advanced Beginner
Add footnote in a bookmark
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default

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
Attached Files
File Type: dotm TEST-BOOKMARK.dotm (42.2 KB, 6 views)
Reply With Quote
  #4  
Old 09-27-2021, 04:19 PM
Guessed's Avatar
Guessed Guessed is offline Add footnote in a bookmark Windows 10 Add footnote in a bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

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
Reply With Quote
  #5  
Old 09-30-2021, 12:10 AM
TheBigBoss TheBigBoss is offline Add footnote in a bookmark Windows 7 32bit Add footnote in a bookmark Office 2010 32bit
Advanced Beginner
Add footnote in a bookmark
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default

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]
This is my result.

Code:
KTP.EG.2021 52 10x
KTP.EG.10x
Reply With Quote
  #6  
Old 09-30-2021, 02:00 AM
Guessed's Avatar
Guessed Guessed is offline Add footnote in a bookmark Windows 10 Add footnote in a bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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
  #7  
Old 09-30-2021, 03:42 AM
TheBigBoss TheBigBoss is offline Add footnote in a bookmark Windows 7 32bit Add footnote in a bookmark Office 2010 32bit
Advanced Beginner
Add footnote in a bookmark
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add footnote in a bookmark Footnote references in the footnote section losing their style when cut+pasted from same doc emblaw Word 4 12-08-2020 06:23 AM
Add footnote in a bookmark 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
Add footnote in a bookmark 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft