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

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
LVL 18
Cory VandenbergSenior Risk ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joanne M. OrzechManager, Document Services CenterCommented:
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
Joanne M. OrzechManager, Document Services CenterCommented:
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
GrahamSkanRetiredCommented:
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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Cory VandenbergSenior Risk ManagerAuthor Commented:
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
Cory VandenbergSenior Risk ManagerAuthor Commented:
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
Joanne M. OrzechManager, Document Services CenterCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cory VandenbergSenior Risk ManagerAuthor Commented:
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
Cory VandenbergSenior Risk ManagerAuthor Commented:
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
Joanne M. OrzechManager, Document Services CenterCommented:
Did you test the last macro I posted?
0
Cory VandenbergSenior Risk ManagerAuthor Commented:
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
Cory VandenbergSenior Risk ManagerAuthor Commented:
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
Cory VandenbergSenior Risk ManagerAuthor Commented:
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
Joanne M. OrzechManager, Document Services CenterCommented:
Oh okay.... I didn't realize that WC....but thanks anyway :)

Joanne
0
Cory VandenbergSenior Risk ManagerAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.