Solved

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

Posted on 2009-07-08
14
1,693 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

776 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