Solved

Macro to Prompt User for Entry of Text into Word Bookmarks

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.

738 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