Solved

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

Posted on 2009-07-08
14
1,659 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:WarCrimes
  • 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
 
LVL 18

Author Comment

by:WarCrimes
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:WarCrimes
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:WarCrimes
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
A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 18

Author Comment

by:WarCrimes
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:WarCrimes
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:WarCrimes
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:WarCrimes
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:WarCrimes
ID: 24807396
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
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.

747 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

11 Experts available now in Live!

Get 1:1 Help Now