Open with Cursor at bookmark in Header

I

ItsBarbara

I created a template in Word in which the user must enter his/her name in the
header. I would like for the document to open with the cursor in this spot
within the header. I created a bookmark in the header. How can I set it so
that when the document is created from the template, that the cursor
immediately goes to the bookmark in the header?
 
L

Lene Fredborg

In the template, you could save a macro named AutoNew and let the macro
display the header with the bookmark selected. However, I suggest another
approach that automatically inserts the user name. This requires that you can
rely on the name defined in Tools > Options > User Information tab being
correct.

You could then insert the AutoNew macro below in the template - the macro
will run when a new document is created, based on that template. In the
macro, change "UserName" to the name of your bookmark - the macro will
automatically update the bookmark with the currently defined user name:

Sub AutoNew()
Dim oRange As Range
Dim strBkmName As String

strBkmName = "UserName"

'To prevent error if bookmark is missing, check first
If ActiveDocument.Bookmarks.Exists(strBkmName) Then
Set oRange = ActiveDocument.Bookmarks(strBkmName).Range
'Insert the user name
oRange.Text = Application.UserName
'The bookmark is gone - recreate it
ActiveDocument.Bookmarks.Add strBkmName, oRange
Else
'What to do if the bookmark was not in the template?
'Insert code here
End If

'Clean up
Set oRange = Nothing
End Sub

If you cannot rely on the defined user name, you could change the macro so
that it displays an inputbox, asking about the user name. Please post back in
case you need an inputbox and need help on the VBA code.

For help on installing a macro, see:
http://www.gmayor.com/installing_macro.htm

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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