Bookmarks not working

A

Angie M.

Hi,

I have several templates that use bookmarks to insert text from a user form.
They work without incident on my computer, no problems whatsoever.

When I run the templates on a client's computer, the bookmarks aren't
recognized, so the macro inserts the text at the current cursor position,
usually the top of the document.

Have you ever seen code not function properly in this way? Again, the code
is correct and works perfectly on most computers including mine. I just have
problems on certain computers at a client's site. Should I suggest they
reinstall Word? Could the problems be caused by Internet downloads or
anything like that?

I fixed one template by adding selection.endkey, you know, physically moving
the cursor to where the text needed to be inserted but some of my other
templates are too complicated to go this route.

Any help/advice is appreciated.
 
C

consul

Angie said:
Hi,

I have several templates that use bookmarks to insert text from a user form.
They work without incident on my computer, no problems whatsoever.

When I run the templates on a client's computer, the bookmarks aren't
recognized, so the macro inserts the text at the current cursor position,
usually the top of the document.

Have you ever seen code not function properly in this way? Again, the code
is correct and works perfectly on most computers including mine. I just have
problems on certain computers at a client's site. Should I suggest they
reinstall Word? Could the problems be caused by Internet downloads or
anything like that?

I fixed one template by adding selection.endkey, you know, physically moving
the cursor to where the text needed to be inserted but some of my other
templates are too complicated to go this route.

Any help/advice is appreciated.


You'd better show your code.
Generally,
ActiveDocument.bookmarks!your_bookmark_name.range.text="new text"
should work without problem provided that your_bookmark_name actually exists
in the ActiveDocument.
 
M

macropod

Hi Angie,

How are you accessing the bookmarks? You shouldn't need to use Selection.Endkey.

Here's some code you'd call from the userform, with parameters for the bookmark’s name and new string:
Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
With ActiveDocument
If.Bookmarks.Exists(BmkNm) Then
Set BmkRng =.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
Else
MsgBox "Bookmark: " & BmkNm & " not found."
End if
End With
Set BmkRng = Nothing
End Sub
 
G

Gordon Bentley-Mix

Angie,

I do this so often in so many of my templates that I have written a Public
procedure as part of a standard 'Tools' module that I include in almost
every template. It looks like this:

Public Sub InsertBookmarkValue(BkmkName As String, ByVal InsertValue As
String)
With ActiveDocument
If .Bookmarks.Exists(BkmkName) Then
Dim myRange As Range
Set myRange = .Bookmarks(BkmkName).Range
myRange.Text = InsertValue
.Bookmarks.Add BkmkName, myRange
End If
End With
End Sub

Calling it is simply a matter of using something like:

InsertBookmarkValue "Bookmark1", myValue

where Bookmark1 is the name of a bookmark and myValue is the value to be
inserted - usually a variable but it could just be raw text (enclosed in
quotation marks of course).

This procedure accepts arguments for the bookmark name (BkmkName) and the
value to be inserted (InsertValue), and besides being very convenient to
call it does a couple of other nice things. It checks for the existence of
the bookmark first (and whilst debugging I usually include an Else statement
that prints the bookmark name and the value to the Immediate window or pops
a message box with the same info), and it also re-inserts the bookmark after
the value has been inserted - this last because inserting the value will
remove the bookmark, and I usually configure my templates to support
rerunning the code after the document has been created.

HTH
 

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