Solved

Macro to Prompt User for Entry of Text into Word Bookmarks

Posted on 2007-11-14
2
1,729 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to check if an MS Word is closed in C# 21 90
How to Adjust Amount of Word Outline 9 32
Weird line on Table of Contents 3 33
Microsoft Word Add-in Start automatically 8 46
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…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

919 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

14 Experts available now in Live!

Get 1:1 Help Now