Solved

Macro to Prompt User for Entry of Text into Word Bookmarks

Posted on 2007-11-14
2
1,742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

630 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