Link to home
Start Free TrialLog in
Avatar of charming
charmingFlag for United States of America

asked on

Macro to Prompt User for Entry of Text into Word Bookmarks

I have a Word Document with 2 Bookmarks Named Bookmark_Header and Bookmark_Footer, which have been placed into my Word document in their respective Header/Footer Locations.

What I need is a macro that prompts the user for Text and places that Text in both Bookmarks

i.e.Document Opens
Input Box says
Please Enter Filename
User types in New Business Data (or whatever they choose)
Result
the Bookmark_Header displays New Business Data
and the Bookmark_Footer also displays New Business Data

Also, can we clear out any Text to the right of the bookmark that already exists before typing thr File Name

What is the VBA Code for a Macro that I can use in the Document Open event to do this?  Thanks
Avatar of charming
charming
Flag of United States of America image

ASKER

Created a Sub Named EnterData.  This works beautifully if the Bookmark is on the Main Document.  However if the Bookmark is in the Header or Footer I get this error

Runtime error 5678 Word cannot find the requested bookmark.

Breaks here Selection.GoTo What:=wdGoToBookmark, Name:="Bookmark_Header"
Any ideas? Thanks

Sub EnterData()

Dim strData As String
strData = InputBox("Enter data?")
Selection.GoTo What:=wdGoToBookmark, Name:="Bookmark_Header"
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InsertAfter strData
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Bookmark_Header"

End Sub
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial