Bookmark "name" being erased

B

Brad

I set up a bookmark labelled "PenalityFree" and run the macro below and it
works except the bookmark name "PenalityFree" gets erased somehow. Any
suggestions? The information in "PenalityFee" will either be 0, 10, 20, or
100.

Sub HideAndChangeDocument()
' ActiveDocument.Range.Sentences(9).Font.Hidden = False
ActiveDocument.Bookmarks("Test1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("PenalityFree").Range.Characters = 20
End Sub
 
J

Jay Freedman

Hi Brad,

For starters, if that code is exactly what you have in your VBA window, the
line you're asking about gets a compile error because the syntax is wrong --
you can't assign a single value to the Characters collection. Maybe you
meant

ActiveDocument.Bookmarks("PenalityFree").Range.Text = 20

That statement would indeed replace the content of the bookmark with the
characters "20" and at the same time delete the bookmark. The explanation of
that, and the workaround, can be found at
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
B

Brad

Got it. Thanks


Jay Freedman said:
Hi Brad,

For starters, if that code is exactly what you have in your VBA window, the
line you're asking about gets a compile error because the syntax is wrong --
you can't assign a single value to the Characters collection. Maybe you
meant

ActiveDocument.Bookmarks("PenalityFree").Range.Text = 20

That statement would indeed replace the content of the bookmark with the
characters "20" and at the same time delete the bookmark. The explanation of
that, and the workaround, can be found at
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Gordon Bentley-Mix at news.microsoft.com

Brad,

If you want the bookmark to remain after you insert text into it, try
something like this, which contains a bit of error handling as well:

Sub HideAndChangeDocument()
If ActiveDocument.Bookmarks.Exists("PenalityFree") = True Then
Dim oRange as Range
Set oRange = ActiveDocument.Bookmarks.("PenalityFree").Range
oRange.Text = "20" '(or 0, 10, 100, etc. - you might want to use a
String variable for this value)
ActiveDocument.Bookmarks.Add "PenalityFree", oRange
Else: MsgBox "Oops! 'PenalityFree' is missing!", vbCritical, "OOPS!"
End If
End Sub

If you do this a lot in your document - insert a value into a bookmark and
want to retain the bookmark afterwards - you might consider writing a
procedure that accepts arguments for the bookmark name and the value to
insert, and call that procedure repeatedly instead of writing virtually the
same block of code multiple times for each bookmark and value. That would
look something like this:

Sub InsertBookmarkValue(BookmarkName As String, ValueToInsert As String)
If ActiveDocument.Bookmarks.Exists(BookmarkName) = True Then
Dim oRange as Range
Set oRange = ActiveDocument.Bookmarks(BookmarkName).Range
oRange.Text = ValueToInsert
ActiveDocument.Bookmarks.Add BookmarkName, oRange
Else: MsgBox "Oops! " & BookmarkName & " is missing!", vbCritical, "OOPS!"
End If
End Sub

Then in your main procedure you would call the above like this:

Sub HideAndChangeDocument()
InsertBookmarkValue "PenalityFree", "20" '(or the String discussed above)
End Sub
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
B

Brad

FANTASTIC!

This is my first attempt using VBA in Word and I don't use Word that often
(I'm more the Excel type - and your answer will be used. What I'm trying to
do is customize a Word document that will have text appearing and
disappearing, and changing depending on certain assumptions.

My feeling is that I will be using functionality of mail merge along with
the hiding of "bookmarks" as well as changing text within a bookmark.

Thanks again!
 
G

Gordon Bentley-Mix at news.microsoft.com

Brad,

I just realised I missed the bit about hiding/showing the bookmark. The
showing part can be incorporated into the code I provided previously without
too much trouble; just modify it like this:

Sub InsertBookmarkValue(BookmarkName As String, ValueToInsert As String)
If ActiveDocument.Bookmarks.Exists(BookmarkName) = True Then
Dim oRange as Range
Set oRange = ActiveDocument.Bookmarks(BookmarkName).Range
oRange.Text = ValueToInsert
oRange.Font.Hidden = False '<<<--- add this line
ActiveDocument.Bookmarks.Add BookmarkName, oRange
Else: MsgBox "Oops! " & BookmarkName & " is missing!", vbCritical, "OOPS!"
End If
End Sub

However, I do this sort of thing all the time, and I usually make the
"hide/show" functionality a separate process from the "insert text"
functionality, using something like this:

Sub HideBookmarkRange(BookmarkName As String)
If ActiveDocument.Bookmarks.Exists(BookmarkName) Then
ActiveDocument.Bookmarks(BookmarkName).Range.Font.Hidden = True
End If
End Sub

Sub ShowBookmarkRange(BookmarkName As String)
If ActiveDocument.Bookmarks.Exists(BookmarkName) Then
ActiveDocument.Bookmarks(BookmarkName).Range.Font.Hidden
End If
End Sub

Then you just call those procedures as needed depending on whatever sort of
logic you would use to decide when a bookmark should be hidden or visible.
That would look something like this:

Sub HideAndChangeDocument()
If [condition requiring bookmark to show] Then
ShowBookmarkRange "PenalityFree"
InsertBookmarkValue "PenalityFree", "20"
Else
HideBookmarkRange "PenalityFree"
End If
End Sub

Of course, in this instance you could use the orginal version of my code and
not have to worry about adding the line to show the bookmark to is as
discussed above.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top