Solved

Macro to Prompt User for Entry of Text into Word Bookmarks

Posted on 2007-11-14
2
1,725 Views
Last Modified: 2012-07-06
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
0
Comment
Question by:charming
2 Comments
 

Author Comment

by:charming
ID: 20282467
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
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 265 total points
ID: 20285028
Try this:

Sub EnterData()
    Dim strData As String
    Dim rng As Range
    Dim Start As Long
   
    strData = InputBox("Enter data?")
    Set rng = ActiveDocument.Bookmarks("Bookmark_Header").Range
    rng.Collapse wdCollapseEnd
    Start = rng.End
    rng.WholeStory
    rng.Start = Start
    rng.Text = strData
    ActiveDocument.Bookmarks.Add "Bookmark_Header", rng 'redefine bookmark to encompass new text
End Sub
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now