Group: microsoft.public.word.vba.general
From: "Doug Robbins - Word MVP"
Date: Monday, March 03, 2008 3:11 AM
Subject: Re: Value of a REF field via macro/vba inserted into save as filen

Hi Frank,

To get the information to appear in multiple places in the document, there
are several methods. In each of these, I am going to assume that it is the
name of a person that you want to appear in multiple places and that on the
userform, you have a textbox control to which you have assigned the name
txtName.

1. You can have multiple bookmarks - Assuming that you have bookmarks with
the names "Name1", "Name2", etc., then in the code for the command button
click event in the user form (at least the way I do it, Malcolm Smith might
do it another way) you would have:

With ActiveDocument
.Bookmarks("Name1").Range.InsertBefore txtName.Text
.Bookmarks("Name2").Range.InsertBefore txtName.Text
'etc.
End With

2. Another way is just to have the one bookmark and use cross reference
fields at the other locations to get the text of the bookmark. For this to
work, you have to be sure that the text is actually being inserted into the
.Range of the bookmark and not before it. To get the text inserted into the
bookmark, when you insert the bookmark into the template, you must have a
space selected, so that if you turn on the display of bookmarks, it looks
like [] rather than a single |.

The other thing is that you have to use some code to update the fields in
the document so that the cross references are updated. If the cross
references are all in the body of the document, you can simply use:

ActiveDocument.Range.Fields.Update

But that will not update fields that are in the Header(s)/Footer(s) of the
document, and there might be multiple headers as they are a property of a
Section of which can have a First Page Header/Footer, a Primary
Header/Footer, and Odd Page Header/Footer and an Even Page Header/Footer
(though I think that one of these is considered as the Primary.

Now you can use code that updates the Range of all of the possible
headers/footers in the document as follows:

Dim i As Long
With ActiveDocument
.Range.Fields.Update
For i = 1 To .Sections.Count
With .Sections(i)
.Headers(wdHeaderFooterEvenPages).Range.Fields.Update
.Headers(wdHeaderFooterFirstPage).Range.Fields.Update
.Headers(wdHeaderFooterPrimary).Range.Fields.Update
.Footers(wdHeaderFooterEvenPages).Range.Fields.Update
.Footers(wdHeaderFooterFirstPage).Range.Fields.Update
.Footers(wdHeaderFooterPrimary).Range.Fields.Update
End With
Next i
End With

But as long as the user has the Update Fields at Print option set under
Tools>Options>Print, you can simply use:

With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With

or, in case they don't have that option checked, you can check it for them
with

Options.UpdateFieldsAtPrint = True
With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With

3. The third way is to have code in the command button click event, set
values of variables (document variables actually) to the data that is
entered into the form and then have that displayed in the document by the
use of { DOCVARIABLE [variable name] } fields. Assuming that you are going
to use a variable with the name "varName", in the command button click
event, you would use:

With ActiveDocument
.Variables("varName").Value = txtName.Text
' similar code for other variables
End With

Then in the document, where ever you want what was entered into the txtName
control on the user form to appear, you would insert docvariable fields

{ DOCVARIABLE varName }

Unlike bookmarks, each of which must have a unique name, you can have as
many of these { DOCVARIABLE varName } fields in the document as you like.
Being fields however, they do need to be updated in the same way as the
cross reference fields to the text of bookmarks would, so you either have to
update the fields in all of the possible .Ranges of Headers and Footers of
each Section, or resort to the .PrintPreview/.ClosePrintPreview routine
mentioned above.

One particular advantage of the use of Docvariable fields, though it does
also apply to the use of cross reference fields is that you can use the
\*charformat switch on the fields to control the way in which the text
appears by applying the desired font characteristics to the D of
DOCVARIABLE.

You also avoid the problem mentioned above of the data not actually being
inserted into the .Range of the bookmark.

Now for you other quest to get the data entered into the form for use in

Dim dlg As Dialog
Set dlg = Dialogs(wdDialogFileSaveAs)
With dlg
.name = strName & strNumber & "_" & strDocumentType & strDate & ".doc"
.show
End With

Assuming that this code is used after the .Values of the document variables
have been set, you would use

.name = ActiveDocument.Variables("varName").Value &
ActiveDocument.Variables("varNumber").Value & "_" & strDocumentType &
strDate & ".doc"



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"frank dobbelaere" wrote in
message news:AB4D4397-9EE6-4FB5-B757-181B7CA693E1@microsoft.com...
> [The internet seems to type out. Apologies if this becomes a double post]
>
> Hi Doug,
>
> Thank you for your response.
>
> I agree with you that one box is better than a series.
>
> That was my goal over a decade ago when I last made some documents (but
> access to resources were limited, there was no internet, no mvp, ...) Now
> over a decade later, learning about userform and seeing that it is
> relatively
> easy to do I wanted to go that route. Plus I didn't remember my old
> autonew
> ask setup.
>
> So, I read and printed at least three userform tutorials yesterday. This
> includes the mvp one you linked and the excellent one from Malcolm Smith
> at
> Dragon Drop. With that in hand I created my userform and it worked like a
> charm (with a few bumps at first of course)
>
> My problem with the userform is that according to the examples I read so
> far, I can only use the entered data once.
>
> Answer 1 -> Bookmark 1
> Answer 2 -> Bookmark 2
> etc
>
> and it wasn't clear to me how I could go farther to get the value and go
> to
> the next steps.
>
> On top of that I was at that point struggling with the fact that bookmarks
> in the header did not get updated. I fell back on ASK / REF. Same problem.
> By
> then half a day later I discovered later you have to macro/vba into the
> header to update the fields there.
>
> Even though I am still on the ASK REF sidetrack, right now, I am most
> interested in making the userform.
>
> What would be your suggestion to have one answer repeated in two or three
> places? Can I use a REF field or something instead of a bookmark?
> According
> to Malcolm Smith I should create more bookmarks, install them in the
> template
> and then in macro link them to the same value.
>
> Also, you said I can use the .Text property of the controls on the
> userform
> to supply the information that I want.
>
> Any tutorial or example you could link to that would cover that extra
> step?
> ... I admit it has been a long day and I can't think straight right now. I
> may have something printed some example that covers it, but I don't see it
> in
> front of me.
>
> That way I could recreate what Jay just helped me with, but using a
> userform.
>
> Thank you in advance,
>
> Frank
>