Update automatically text from another

A

Anderflash

Well, I have a document with common text. I want the following:
- On the first line of the second page I want a text equals to the first
line of the first page. (This would be easy putting a bookmark - 'first' for
example) and put on the second page {first}.
- I want that when I change the first line of the first page, this
automatically updates the first line of the second page, but the paragraph
above doesn't do this (I need to press F9).
- This can be solved transforming the first line of the first page in a text
field from the Forms toolbar and checking 'Calculating on exit'. But if I do
this, I need always to protect the document to update automatically the
second page. I'd like the update in a pure text, not in a textfield.

Is there a solution?
 
P

Peter Jamieson

For both your update requirements, you need to use a VBA macro that is named
in the "Run macro on Exit" field of the text form field properties.

The field in the second page needs to be a "REF" field that references the
name of the text form field you want to duplicate - e.g. ir it is Text1, you
need { REF Text1 } or { Text1 }

If you want, us the macro just posted to update all the fields in the
document

Sub UpdateAllFields()
Dim objRange As Word.Range
For Each objRange In ActiveDocument.StoryRanges
Do
objRange.Fields.Update
Set objRange = objRange.NextStoryRange
Loop Until objRange Is Nothing
Next
End Sub

or just update the ranges you need (you'll have to look at the Word
StoryRanges object for more info. on that). If you're unfamiliar with using
VBA macros, try Graham Mayor's page at

http://www.gmayor.com/installing_macro.htm

Peter Jamieson
 
A

Anderflash

Peter, thanks for the information. I almost did the document using your
suggestion.
Let me explain more about the document.
I did another question (about the footer) because I'd like to have a text
and the text on the footer needs to be equals to the first line of the body
text.
I put a text field on the first line of this body text (bookmark = 'first'
for example), and on the footer I did {ref first}. I did the update macro
that select all the text and press F9, and I put on exit field from the text
field properties. But there was an error: the document is protected (for
effect of the field), so I edited the VBA code to unprotect before the
actions, and protect. But the protect function erase the text field content
on the first line, although the ref field updates. I changed the action
"select all and press f9" to "open header/footer, go to footer, select the
ref and press f9, go back to body", but it continues to erase the text field.
It seems that the macro on exit from the text field properties is the right
way, but I'm doing something wrong.

Thanks for the attention and for the answers.
 
P

Peter Jamieson

I did the update macro
that select all the text and press F9

Don't do it that way: use the macro I suggested:

Sub UpdateAllFields()
Dim objRange As Word.Range
For Each objRange In ActiveDocument.StoryRanges
Do
objRange.Fields.Update
Set objRange = objRange.NextStoryRange
Loop Until objRange Is Nothing
Next
End Sub

Peter Jamieson
 
A

Anderflash

Great, I did, thanks. Two questions:
-Why does only the function "activedocument.fields.update" not work?
-Is there a solution without using the text field (using only pure text on
the body)?
 
P

Peter Jamieson

-Why does only the function "activedocument.fields.update" not work?

Because although it sounds as if it will update /all/ the fields in the
document (and aI wish it did do that because everythign would be much
simpler), "activedocument.fields" only references the "main" story. (Well,
actually it may reference more stories than that, but that's what I think it
does).
-Is there a solution without using the text field (using only pure text on
the body)?

You could /probably/ do it by bookmarking the text (you could do this for
the header as well) and using the macro to replace the text. Because the
operation destroys the bookmark, you have to replace it. However, I haven't
tested it so don't take my word for it!

Let's suppose you have a bookmark on page 2 called page2bm and a bookmark in
the header called headerbm, then the following would probably do it:

Sub UpdateMyBMs()
UpdateBookmark "page2bm", ActiveDocument.Bookmarks("the name of your text
form field").Range.Text
UpdateBookmark "headerbm", ActiveDocument.Bookmarks("the name of your text
form field").Range.Text
End Sub

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

The above macro is adapted from Dave Rado's page at

http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm

Peter Jamieson
 
A

Anderflash

Good, but if I replace the text field to a pure text, I will not have the
event 'on exit'. In other words, I don't have an event 'change' in the
document like worksheet_change from the excel and 'on exit' to the pure text.
In that case, I would need the user manipulation (a button or shortcut key)
to activate the bookmark's updating procedure.
But thanks for the information. I don't know if the Word VBA is too
important for me like Excel/Access VBA, but I liked the learning.
 

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