Update Reference Field

B

Blake

I have a userform that fills in formfields in a document.
In several places, the same information is inserted, so I
cross-referenced those formfields with ref field. The
problem is that I can't figure out how to update those
ref fields without also clearing the formfields. When I
update all the fields in the document, the form fields
clear and the ref fields update, I suppose, but since the
form fields are now blank so are the ref fields. I need a
way to update only the ref fields but I am not sure how
to do this.

Thanks for any help.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Blake > écrivait :
In this message, < Blake > wrote:

|| I have a userform that fills in formfields in a document.
|| In several places, the same information is inserted, so I
|| cross-referenced those formfields with ref field. The
|| problem is that I can't figure out how to update those
|| ref fields without also clearing the formfields. When I
|| update all the fields in the document, the form fields
|| clear and the ref fields update, I suppose, but since the

Formfields should not be cleared when updated.... How do you update your
fields? Post your code and I am sure it will be fairly easy to point you in
the right direction then.

|| form fields are now blank so are the ref fields. I need a
|| way to update only the ref fields but I am not sure how
|| to do this.
||
|| Thanks for any help.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Guest

The code that is causing the problem is simply:

..Fields.Update

When I disable this, the ref fields I want updated of
course are not updated, but the formfields remain as they
should. If I enable this and run userform, all the
fields are empty. I can, since this is the last action
performed to the document, undo that action and see that
updating the fields is in fact what cleared them.

Disabling this once again and inserting

..PrintPreview
..ClosePrintPreview

instead updates all the fields in the way I want. So I
guess my problem is solved although I would still like to
figure it out.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < (e-mail address removed) > écrivait :

| The code that is causing the problem is simply:
|
| .Fields.Update
|
| When I disable this, the ref fields I want updated of
| course are not updated, but the formfields remain as they
| should. If I enable this and run userform, all the
| fields are empty. I can, since this is the last action
| performed to the document, undo that action and see that
| updating the fields is in fact what cleared them.
|

I guess I still do not understand your situation or there is something else
interfering in your code.It is probably because I do not see the connection
between the userform and the formfields. You have a userform the asks the
users to fill out some info, and then you populate the formfields with that
info. Is your userform doing anything else? If not, the userform is
superfluous. Just let the user navigate in the document filling out the
formfields.

There is a simple way of updating REF fields based on formfields. Make sure
you check the "Calculate on exit" checkbox in the formfield property dialog
box. Then leaving the formfields will automatically update the REF fields.
But for this to work you must be in a document protected for forms (Tools >
Protect)

If you have Formfields and REF fields pointing to those formfields, then
ActiveDocument.Fields.Update
will update all REF fields and will not change the content of the
formfields.
The only way I can reproduce your situation is by unprotecting the document.
Is this what you are doing?
In that case, you do not need formfields (In fact you cannot use them if the
document is unprotected). See the following code for a sample of what you
could do instead. You just need a userform with a textbox and a command
button, a bookmark in the document named Text1 and some REF fields pointing
to that bookmark.

'_______________________________________
Private Sub CommandButton1_Click()

FillBookMarks "Text1", Me.TextBox1.Text

ActiveDocument.Fields.Update

Me.Hide

Unload Me

End Sub
'_______________________________________

'_______________________________________
Function FillBookMarks(BKName As String, BKContent As String)

Dim MyRange As Range

With ActiveDocument
Set MyRange = .Bookmarks(BKName).Range
MyRange.Text = BKContent
.Bookmarks.Add BKName, MyRange
End With

End Function
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Guest

The userform performs many other actions other than
filling in the fields. Plus the document isn't really a
form and so is not protected because it also needs to be
available for editing. The only reason I was inserting
text into formfields instead of at bookmarks was because
I want some of the data entered into the document to be
inserted into a database, and formfields (or enclosed
bookmarks) was the only way I knew to do it.

Are you saying that I can cross-reference non-enclosed
bookmarks?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < (e-mail address removed) > écrivait :

| The userform performs many other actions other than
| filling in the fields. Plus the document isn't really a
| form and so is not protected because it also needs to be
| available for editing. The only reason I was inserting
| text into formfields instead of at bookmarks was because
| I want some of the data entered into the document to be
| inserted into a database, and formfields (or enclosed
| bookmarks) was the only way I knew to do it.
|
| Are you saying that I can cross-reference non-enclosed
| bookmarks?
|

No.
But look at the code I posted. It will take userform info and put it in the
document in a range enclosed by bookmarks (but not in a formfield).
Then all you have to do is use
ActiveDocument.Bookmarks("BkName").Range.Text.
to transfer the info into a database.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Guest

Wow. Thanks, that's great. Just one question: I'm not
sure what exactly to do with the Function. Should it be
contained in the command button click event?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < (e-mail address removed) > écrivait :

| Wow. Thanks, that's great. Just one question: I'm not
| sure what exactly to do with the Function. Should it be
| contained in the command button click event?

Not Quite.

Just paste the function under the code for the command button in the
userform code.
Then, use the command button code to call the function.

In the example I posted, from the button code I call the Function named
"FillBookMarks" while passing the value of the name of the bookmark
("Text1") and the info string from the userform text box
("Me.TextBox1.Text").

At its simplest, you only have to have as many calls as you have textboxes:

'_______________________________________
Private Sub CommandButton1_Click()


FillBookMarks "Text1", Me.TextBox1.Text
FillBookMarks "Text2", Me.TextBox2.Text
FillBookMarks "Text3", Me.TextBox3.Text
FillBookMarks "Text4", Me.TextBox4.Text
FillBookMarks "Text5", Me.TextBox5.Text
'etc.

ActiveDocument.Fields.Update

Me.Hide

Unload Me

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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