View Single Post
  #14   Report Post  
Jay Freedman
 
Posts: n/a
Default

Hi Carol,

There are a lot of interrelated things going on in your document and
its code, and it's a hard to cover them all at one go. Still, let's
try to cover them one at at time.

1. A bookmark is just a name for a location in the document. It
doesn't have any active properties -- it can't make things happen.
Fields like the Ref field can do things, and macros/userforms are all
about doing things, but bookmarks just lie there. Think of bookmarks
and text as nouns, and macros/userforms and some fields as verbs.

So the userform code uses the bookmark as a location to stuff some
text into the document. Other than the text appearing there, *nothing*
happens. The Ref fields don't find out about the new text unless you
somehow tell them to update themselves (here, "update" means "make a
copy of the bookmark's contents and display that copy at the Ref
field's location"). A few responses ago, I said that F9 updates
fields, and so does printing or going to Print Preview. One other
thing that can cause an update is to put this line of code in your
userform's CommandButton1_Click() procedure, just before the Hide
statement:

ActiveDocument.Fields.Update

[Everything in Word is complicated. In this case, the complication is
that the line of code only updates fields in the body of the document,
not in headers, footers, or textboxes. I'll assume for now that this
doesn't make any difference in your document.]

2. Now we come to the difference between using the .InsertBefore
method and using the approach in the article I recommended. This is
completely unintuitive, even to me. As I said, a bookmark is just a
named location in the text. It can cover one or more characters, or it
can be collapsed to a single point between characters, and it behaves
differently depending on which it is.

If the bookmark is collapsed to a point (because you had nothing
selected when you created it), then the .InsertBefore method will
insert new text *after* the bookmark, *outside* it. (I think this
behavior is a VBA bug -- the new text should go before the bookmark.)
The bookmark will remain empty. If a Ref field points to that
bookmark, then updating the field will cause it to display nothing --
because that's what's inside the bookmark, nothing.

If the bookmark covers one or more characters (which could be spaces),
then the .InsertBefore method inserts the new text inside the bookmark
before the existing contents. This is the only case in which it does
what you expect. Afterward, updating the Ref field that points to the
bookmark will show a copy of the bookmark's contents, including both
the new text and the old contents. This works ok as long as you only
insert new text once. Try it again and you get *both* sets of text in
the bookmark, and *both* sets reflected in the Ref field.

The method shown in the InsertingTextAtBookmark.htm article --
assigning the new text to the range of the bookmark, and then
re-inserting the bookmark to cover the inserted text -- gives you
exactly what you need, predictably and with the ability to use the
same bookmark again.

3. The expression "Dim BMRange As Range" tells VBA that you're going
to use a variable named BMRange, and that it's going to represent a
range -- that is, a particular piece of the document. (The "Dim" is
short for "Dimension" and is a leftover from much older programming
languages.)

After that, the statement that begins with "Set BMRange =" finds the
range that corresponds to the named bookmark, and assigns that range
to the variable BMRange. That way you'll be able to use the variable
to re-insert the bookmark later.

The code you showed earlier needs to be rewritten like this. Notice
that the variable BMRange can be re-used to point to different ranges
at different times during the execution of the code.

Private Sub CommandButton1_Click()
Dim BMRange As Range

With ActiveDocument
Set BMRange = .Bookmarks("Decedent").Range
BMRange.Text = TextBox1
.Bookmarks.Add "Decedent", BMRange

Set BMRange = .Bookmarks("Venue").Range
BMRange.Text = TextBox2
.Bookmarks.Add "Venue", BMRange

Set BMRange = .Bookmarks("EstateNumber").Range
BMRange.Text = TextBox3
.Bookmarks.Add "EstateNumber", BMRange

Set BMRange = .Bookmarks("PersonalRepresentative").Range
BMRange.Text = TextBox4
.Bookmarks.Add "PersonalRepresentative", BMRange

Set BMRange = .Bookmarks("DateOfDeath").Range
BMRange.Text = TextBox5
.Bookmarks.Add "DateOfDeath", BMRange

End With

ActiveDocument.Fields.Update

UserForm1.Hide
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Wed, 30 Mar 2005 15:19:05 -0800, "Carol"
wrote:

Hi Jay,

I stepped through the code and had just about decided that the problem was
just what you said because the error popped up at the two separate words. So
now I have fixed the bookmarks and the code so that they are the same and
they do indeed fall in right where they should, but the ref fields are not
working and as you have seen, I am very new to this aspect of word, and even
after reading the article that you sent to me, I am still very unsure as to
how I should write the code for the ref fields. I guess the problem is that
I don't understand how using InsertBefore only screws up the ref fields and
not the actual bookmarks.

Also, reading the article is a bit difficult for me because I don't
understand what Dim BMRange means. Are there any articles or tutorials that
can help me with this? Or could you tell me how to do one so that I can go
from there?

I so appreciate your's and Suzanne's help!

"Jay Freedman" wrote:

Hi Carol,

You're getting there... but you have to exactly match the names of the
bookmarks in the code. Since spaces aren't legal in bookmark names, I can
tell right away some of them are wrong. Look at the names in the list when
you bring up the Insert Bookmark dialog, and make the names in the code
*exactly* the same.

There's another complication with the way you userform is putting the text
in the document. When you use the .InsertBefore command, the text goes
*before* the bookmark, not *in* the bookmark. So when you do get the
userform to finish without errors, the Ref fields won't have anything to
show because the bookmarks themselves will be empty. The proper code for
this is shown he
http://word.mvps.org/FAQs/MacrosVBA/...AtBookmark.htm

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

Carol wrote:
I started from scratch with the document. In the spots where I wanted
the different variables, I clicked on Insert | Bookmark and then
typed in the name and clicked on Add. Where a variable was used more
than once I clicked on Insert | Fields and Ref and clicked ont he
Bookmark that it pointed to. Then I went to the Visual Basic Editor
and inserted the User Form and added labels and textboxes for all of
the bookmarks. I then added a command button and named it ok. After
that I right-clicked on the command button and chose View Code. Once
there, in between "Private Sub CommandButton1_Click()" and "End Sub,"
I typed:

With ActiveDocument
.Bookmarks("Decedent").Range _
.InsertBefore TextBox1
.Bookmarks("Venue").Range _
.InsertBefore TextBox2
.Bookmarks("Estate Number").Range _
InsertBefore TextBox3
.Bookmarks("Personal Representative").Range _
.InsertBefore TextBox4
.Bookmarks(Date of Death").Range _
.InsertBefore TextBox5

End With
Userform1.Hide

Ok, then I used Alt+Q to go back to Word and I saved the template and
then I created a macro in that template called AutoNew which
essentially said UserForm1.Show.

So then I saved it and closed it and went to File, New, From my
Computer and selected it and the dialog box popped up and I filled in
all the variables and clicked on the button and it just keeps telling
me "Runtime error 5941. Teh requested member of the collection does
not exist. I have to tell you - it's making me crazy!

Any suggestions you might have would be extremely welcome at this
point. Thanks again.



"Carol" wrote:

Thanks for all the input. This is not a protected form but, rather, a
template that I am configuring so that when it is selected, a dialog
box (user form) will pop up to fill in the variables needed for the
document to be complete. I am going start over from scratch today
and will report on my success/failure. Thanks again!

"Jay Freedman" wrote:

And to answer your question about Alt+F9:

- It's a toggle; every time you press Alt+F9, it either displays the
field codes if the field results are showing, or it displays the
field results if the field codes are showing. Press it twice and
you're back where you started.

- Alt+F9 by itself does not update the fields. That function is
performed by F9 (without the Alt) for any fields that are currently
selected; or by printing or going into Print Preview (assuming the
"Update fields" option is checked in the Tools Options Print
dialog); or, as Suzanne said, by having "Calculate on exit" checked
for a form field in a protected form when that form field is the
source for the Ref field.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org