Looks good Sandi, but I have a question and a couple of tips for you.
Question:
In Re-
Selection.GoTo What:=wdGoToBookmark, Name:="bkBorrower1"
Is there a particular reason for moving the Selection to the Bookmark after
writing the text into it and then re-inserting it? If so, you can do this
outside of the If statement to save repeating the code. And if not, I'd
recommend taking it out, as it will just slow things down and make the screen
jump around annoyingly. (That's the first tip. <g>)
More Tips:
In Re-
oRng.Text = Me.txtBorrower1 & " and " & Me.txtBorrower2 and
oRng.Text = Me.txtBorrower1
It's just good practice to explicitly state the property of an object that
you're working with rather than just relying on the default property. It
works OK in the examples above because the default property of a TextBox is
.Value, but this can be risky if MS desides to change the default (not
unheard of) and then your code breaks without warning. It also gets you in
the habit of thinking about which property you want to use so you remember to
specify the correct property when you want something other than the default.
This good practice would result in:
oRng.Text = txtBorrower1.Value & " and " & txtBorrower2.Value
and
oRng.Text = txtBorrower1.Value
(Note that the use of 'Me.' is also not required - although it does make
finding a particular Control on a UserForm easier.)
In Re-
With .Variables("varBorrower") ***
End With
The use of a With statement is not required in this instance as you are only
working with one property of the "varBorrower" variable - the .Value
property. (Well done on specifying the property in this instance though.)
Accordingly, you could just use:
.Variables("varBorrower").Value = [etc.]
Generally-
I see a lot of repetition in you code, which could be eliminated quite
easily, as follows:
Sub SandiRevised()
Dim oRng As Range
Dim myBookmarkText As String
Dim myVariableValue As String
With ActiveDocument
If Len(txtBorrower2.Text) > 0 Then
myBookmarkText = txtBorrower1.Value & " and " & txtBorrower2.Value
myVariableValue = ", the Borrowers. 'Borrower' and any reference
to 'Borrower'" _
& " singly or 'Borrowers' collectively means each as well of
them in their" _
& " individual, and joint and several capacities."
Else
myBookmarkText = txtBorrower1.Value
myVariableValue = ", the Borrower."
End If
Set oRng = .Bookmarks("bkBorrower1").Range
oRng.Text = myBookmarkText
.Bookmarks.Add Name:="bkBorrower1", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkBorrower1"
.Variables("varBorrower").Value = myVariableValue
.Range.Fields.Update
End With
End Sub
I'm not quite sure why you're using both a bookmark and a document variable,
but I'm assuming that you (possibly?) have several 'bkBorrower' bookmarks
that need only display the borrower names, and then one particular point that
requires the full definition of "Borrower" - which is displayed through the
use of a DOCVARIABLE field. You could probably use just one or the other
exclusively, and I believe there may be certain advantages to the use of
fields (e.g. REF fields?). Otherwise, if you're happy, I'm happy! :-D
--
Cheers!
Gordon
Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
Sandi V said:
Many thanks to you both! Sorry for the late response - I always forget to
click "Notify me of replies". I was coming back to post the solution for
posterity. I went with Doug's method; mainly -- but got some good info from
both of you & learned some new things! Here's what worked out the end:
With ActiveDocument
If Len(txtBorrower2.Text) > 0 Then
Set oRng = ActiveDocument.Bookmarks("bkBorrower1").Range
oRng.Text = Me.txtBorrower1 & " and " & Me.txtBorrower2
ActiveDocument.Bookmarks.Add Name:="bkBorrower1", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkBorrower1"
With .Variables("varBorrower")
.Value = ", the Borrowers. 'Borrower' and any reference to
'Borrower' singly or 'Borrowers' collectively means each as well of them in
their individual, and joint and several capacities."
End With
Else
Set oRng = ActiveDocument.Bookmarks("bkBorrower1").Range
oRng.Text = Me.txtBorrower1
ActiveDocument.Bookmarks.Add Name:="bkBorrower1", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkBorrower1"
With .Variables("varBorrower")
.Value = ", the Borrower."
End With
End If
.Range.Fields.Update
End With
Thanks very much for your help!