Duplicate text

P

Paul

I'm trying to sort out a document which can have duplicate text over 8 or so
pages. The reference is simply Project_Name and Project_Number, these are the
only two fields that I need. The tricky bit for me is, not only need this in
a text box and the main body of document but also the Project Number needs to
be in the header. I have managed to get a little window to pop up when the
template is opened so the Project Name and Number can be put in there, hoping
that it would generate through the document, but alas it did not work.

So any help would be appreciated.
 
D

Dave Lett

Hi,

You could bookmark the Project_Name and Project_Number, and then insert
{REF} fields that point to those bookmarks in your document.

HTH,

Dave Lett
 
P

Paul

I've tried that to, but I think because of the text box and the header it
doesn't seem to work. Should this be the case?
 
D

Dave Lett

Hi,

It sounds like the fields simply aren't being updated. Click the Print
Preview button; this should update your fields.

HTH,
Dave Lett
 
P

Paul

Dave

I've just tried that and nothing so I guess I've made a mistake somewhere.

The Project Number bookmark is called Project_Number - so I have written
{REF Project_Number} is that right?

Paul
 
P

Paul

Dave

I've copied Step 2 into my attempt at VB. On the .Text = would this be {REF
Project_Number} but if it is how would it know what to replace it with as for
each document the project number will be different. Sorry to be a pain.
 
P

Paul

OK this is what I have so far.

Public Sub FindReplaceAlmostAnywhere()
Dim rngStory As Word.Range
Dim lngJunk As Long
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
With rngStory.Find
.Text = "projno"
.Replacement.Text = ("Project_Number")
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub

With this I can replace any instance of projno - but how do I get it to
replace it with the bookmark called Project_Number?

I think I'm nearly there, you'll be pleased to know :)
 
D

Dave Lett

Hi Paul,

<snip>With this I can replace any instance of projno - but how do I get it
to replace it with the bookmark called Project_Number?</snip>

Just to be sure, you want to replace "projno" with the CONTENTS of a
BOOKMARK called Project_Number. Correct?
This means that the bookmark Project_Number exists in the current document.
If so, then, great, you're almost there.

Dim rngStory As Word.Range
Dim lngJunk As Long
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
With rngStory.Find
.Text = "projno"
Do While .Execute
ActiveDocument.Fields.Add _
Range:=rngStory, _
Type:=wdFieldRef, _
Text:="Project_Number", _
PreserveFormatting:=True
Loop
End With
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next

HTH,

Dave
 
P

Paul

Hi Dave

Thanks for the reply.

You are correct that is what I want to do.

I've put the code you sent in and when it is run I get the following:
{REF Project_Number \* MERGEFORMAT}

I've tried to update the field afterwards, but it goes blank.

Paul
 
D

Dave Lett

Hi Paul,

Yes, you are getting the correct field. HOWEVER, if it is blank, then it
means that your bookmark ("Project_Number") has been inserted as a
placeholder bookmark. That is, the range of the bookmark doesn't include any
text. To test this hypothesis, run the following by itself:

Debug.Print ActiveDocument.Bookmarks("Project_Number").Range.Text

To insert the bookmark, you should have the text selected. Or, to expand the
range of the bookmark to include text, you could use something like the
following:

Dim oRng As Range
Set oRng = ActiveDocument.Bookmarks("Project_Number").Range
oRng.Text = "My New Project"
ActiveDocument.Bookmarks.Add _
Name:="Project_Number", _
Range:=oRng

This will change a placeholder bookmark to an enclosing bookmark.

For a good discussion about the differences between these bookmarks, have a
look at the article "Working with Bookmarks in VBA" at
http://word.mvps.org/FAQs/General/index.htm

HTH,
Dave
 
P

Paul

Dave

Thanks for your help. For some reason I couldn't get my head round that bit,
but the bit below seems to work:

Private Sub CommandButton1_Click()

With ActiveDocument
.Bookmarks("Project_Name").Range _
.InsertBefore TextBox1
.Bookmarks("Project_Name2").Range _
.InsertBefore TextBox1
.Bookmarks("Project_Name3").Range _
.InsertBefore TextBox1
.Bookmarks("Project_Number").Range _
.InsertBefore TextBox2
.Bookmarks("Project_Number2").Range _
.InsertBefore TextBox2

End With

UserForm1.Hide

End Sub

I've just created a couple more bookmarks are related them to the relevant
text boxes, probably not the ideal way of doing it, but it seems to do what I
want, which in itself is a bonus.

Thanks again for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top