?
Solved

VBA insert text into enclosed bookmark, then select inserted text and "reset" the enclosed bookmark

Posted on 2009-07-08
14
Medium Priority
?
1,803 Views
Last Modified: 2012-05-07
I'm not sure if this is the best approach for doing this, so if you have a better one, please feel free to suggest it.

I have a document with a bookmark set up.  I am using the bookmark to insert a PROJECT_NAME into the document.  I then add lots of stuff to the document, text, tables, figures, etc.  When I want to refer to the PROJECT_NAME in the rest of the document, I have the string 'PROJECT_NAME'.  What I need to do is find all of these occurrences and insert a field that references the value I first inserted into the enclosed bookmark, call this the lookup value.  This includes the Headers of the pages as well.

This way, if the 'lookup value' is changed after the document is finished being created, all the occurrences can be easily updated.  

I need the solution in VBA code.

Thanks in advance for any help,

WC
0
Comment
Question by:Cory Vandenberg
[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
  • 8
  • 5
14 Comments
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24806647
I would use the REF field to accomplish this... bookmarks can be testy.... and often get inadvertently deleted.
http://gregmaxey.mvps.org/Repeating_Data.htm
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24806689
Or you could use VBA -- simply insert a DocVariable field named "ProjName" wherever you want the project name to appear in your document.
Sub ProName()
ProjectName = InputBox("Please enter Project Name")
ActiveDocument.Variables.Add Name:="ProjName", Value:=ProjectName
ActiveDocument.Fields.Update
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24806914
Just be aware that a bookmark can either mark a single position (non-containing) where the start and end are the same, or it can span a range (containing). If you set the text for a bookmark's range:

MyDocument.Bookmarks("MyBookmark").range.text = "Some Text"

it does not automatically convert a non-containing bookmark to one that contains the text. Hence a reference to the bookmark's text could be empty, because the non-containing bookmark would simply mark the start of the text.

You could use some code like this to avoid that possibility
Sub FillBookmark(doc As Word.Document, strBookmarkName As String, strText As String)
    Dim rng As Word.Range
    Set rng = doc.Bookmarks(strBookmarkName).Range
    rng.Text = strText
    doc.Bookmarks.Add strBookmarkName, rng 're-add & hence redefine the bookmark to include the new range
End Sub

Open in new window

0
Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24806916
J,

For your first suggestion, to use a Ref field, I need a bookmark to refer to.  The problem is, I'm inserting text into the enclosed bookmark, thus it is deleted and replaced by the text.  How then do I make the inserted text a bookmark for the Ref fields to refer to?

The second option could be promising as I already have the variable value stored.

The problem with either option though is this,

I need to find all occurrences of 'PROJECT_NAME' and replace it with either the Ref field or the DocVariable field.  Is that possible?

WC
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24806924
Actually, option 2 won't work, as I need this to be able to update after creation, so please tell me how to do option 1 with the Ref fields.

WC
0
 
LVL 37

Accepted Solution

by:
Joanne M. Orzech earned 2000 total points
ID: 24806955
This should help you with this portion of your question:
"For your first suggestion, to use a Ref field, I need a bookmark to refer to.  The problem is, I'm inserting text into the enclosed bookmark, thus it is deleted and replaced by the text.  How then do I make the inserted text a bookmark for the Ref fields to refer to?"
http://word.mvps.org/faqs/MacrosVBA/WorkWithBookmarks.htm

Let me think about the second portion... although I see Graham is here and he's a wiz at this stuff.....so you already have the constant text string "PROJECT NAME" throughout your document and you need to replace it?  You could get the info from the InputBox and then just do a search and replace in your Word document via VB.  Does that sound like it would work?


Sub ProName()
ProjectName = InputBox("Please enter Project Name")
    Selection.HomeKey Unit:=wdStory
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "PROJECT NAME"
        .Replacement.Text = ProjectName
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Fields.Update
End Sub

Open in new window

0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24807014
So to clarify,

I start with a bookmark, [PROJECT_NAME]

I insert the project name into this bookmark, so it is then, "This project name", as just text

I need to then enclose that inserted text as a bookmark to be referenced by the Ref fields.

I need to find all occurrences of 'PROJECT_NAME' in the text and headers and replace this with a Ref field pointing back to the new bookmark.

WC
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24807061
J,

Thanks for pointing me to that link.  I've actually got it bookmarked but must have missed the bottom part describing exactly what I need to do.

Ya, I had a feeling inserting Ref fields to replace a constant string wouldn't be as straightforward as just replacing with another string.

WC
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24807183
Did you test the last macro I posted?
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24807284
At first the code from the link only inserted an placeholder bookmark, but I abstracted it and now it seems to be working correctly, enclosing the entire inserted text.

Since I'm working in Excel VBA and dimming the wddoc and ranges as objects, it's a bit different than the code on the link.

Here is what I used, if anyone else runs into this thread later.

Sub UpdateBookmark(wdDoc As Object, BookmarkToUpdate As String, TextToUse As String)
    Dim BMRange As Object
    Set BMRange = wdDoc.Bookmarks(BookmarkToUpdate).Range
    BMRange.Text = TextToUse
    wdDoc.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub

Then I use the defined sub in the following manner,

    Call PublicSubs.UpdateBookmark(wdDoc, "BM_Project_Name", ReportTitle)


Got that part, now to find all occurrences of "PROJECT_NAME" and replace them with a Ref field.

WC
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24807311
J,

No, I didn't test the macro you posted, as I want to refrain from using an inputbox to get the project name.  

WC
0
 
LVL 18

Author Closing Comment

by:Cory Vandenberg
ID: 31601252
I'll accept this for the first half of my problem.  See below for my code that was made from the link provided.  I'll post a link to 2nd half of the question which I'll award separate points to.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24807375
Oh okay.... I didn't realize that WC....but thanks anyway :)

Joanne
0
 
LVL 18

Author Comment

by:Cory Vandenberg
ID: 24807396
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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 article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
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.
Suggested Courses

762 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