Re: Insert sentence only if text field is not null by SandiV
SandiV
Tue Jul 22 12:38:16 PDT 2008
Thanks very much for your feedback & tips! I really appreciate your time.
First:
> 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?
Nope! None at all! I've been copying, pasting & modifying from my original
VBA class materials, which had this line (for a purpose or not, I don't
know). I commented all of those out with no detriment & will delete them
soon. The only one I need to keep is Selection.GoTo What:=wdGoToBookmark,
Name:="bkStartHere"; which lands the cursor at the place where user starts to
type.
Two:
> This good practice would result in:
> oRng.Text = txtBorrower1.Value & " and " & txtBorrower2.Value
> and
> oRng.Text = txtBorrower1.Value
Gotcha. Tested and good. I asked myself "what else is there besides
.value?" Wow. So much to learn.
Three:
> 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.
I understand that in theory. Am having trouble making it work. I'll post
separately if I can't figure it out.
Four:
> Generally-
> I see a lot of repetition in you code, which could be eliminated quite
> easily, as follows:
Ditto above. I know, I'll try!
Thanks again very much! I see your second post following this one; I will
f/u on that tomorrow.
Sandi
"Gordon Bentley-Mix" wrote:
> 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" wrote:
>
> > 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!
> >
> > "Gordon Bentley-Mix" wrote:
> >
> > > Sandi,
> > >
> > > I agree with Doug. However, I think you could probably make my code work if
> > > you just made a call to the "InsertBKBorrower" procedure in your OK button
> > > code. (In fact, I'd recommend making your code as modular as possible anyway.
> > > For example, the code for inserting the Guarantor info could be placed in a
> > > separate sub, which is then called in the OK button code.)
> > >
> > > I'd also be interested to know what's going on in in the InsertBKBorrower
> > > code that's making it fail. Have you tried inserting a break point at the
> > > start of this sub and then stepping the code to see what's going on? If all
> > > else fails you can send me the template and I'll take a look.
> > > --
> > > Cheers!
> > > Gordon
> > >
> > > Uninvited email contact will be marked as SPAM and ignored. Please post all
> > > follow-ups to the newsgroup.
> > >
> > >
> > > "Doug Robbins - Word MVP" wrote:
> > >
> > > > If you only dable in vba every six months or so, I would suggest that you
> > > > take the simpler approach that I suggested.
> > > >
> > > > --
> > > > 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
> > > >
> > > > "Sandi V" <SandiV@discussions.microsoft.com> wrote in message
> > > > news:2E2285FA-0AA0-4651-86FF-8271C779CD9D@microsoft.com...
> > > > > Sorry to be dense: but where does this go?
> > > > >
> > > > > I've got
> > > > >
> > > > > Private Sub btnCancel_Click()
> > > > > Me.Hide
> > > > > ActiveDocument.Close wdDoNotSaveChanges
> > > > > End Sub
> > > > >
> > > > > Private Sub btnOK_Click()
> > > > > On Error Resume Next
> > > > > Dim oRng As Word.Range
> > > > >
> > > > > 'Populating Guarantor bookmarks:
> > > > >
> > > > > Set oRng = ActiveDocument.Bookmarks("bkGName").Range
> > > > > oRng.Text = Me.txtGName
> > > > > ActiveDocument.Bookmarks.Add Name:="bkGName", Range:=oRng
> > > > > Selection.GoTo What:=wdGoToBookmark, Name:="bkGName"
> > > > > (etcetera)
> > > > >
> > > > > Me.Hide
> > > > > ActiveDocument.PrintPreview
> > > > > ActiveDocument.ClosePrintPreview
> > > > > Selection.GoTo What:=wdGoToBookmark, Name:="bkStartHere"
> > > > >
> > > > > End Sub
> > > > >
> > > > > If I try to include it under the OK button, I get "expected end sub"
> > > > > error.
> > > > >
> > > > > This is the code I modified from yours, so far no decompile or run errors;
> > > > > but the bkBorrowerName does not populate in the document.
> > > > >
> > > > > Private Sub InsertBKBorrower()
> > > > > Dim myRange As Range
> > > > > With ActiveDocument
> > > > > If .Bookmarks.Exists("bkBorrowerName") Then
> > > > > myRange = .Bookmarks("bkBorrowerName").Range
> > > > > myRange.Text = fcnBuildBKBorrower
> > > > > .Bookmarks.Add "BkBorrowerName", myRange
> > > > > End If
> > > > > End With
> > > > > End Sub
> > > > >
> > > > > Private Function fcnBuildBKBorrower() As String
> > > > > Dim Temp As String
> > > > > Temp = txtBorrower1.Value
> > > > > If Len(txtBorrower2.Value) > 0 Then
> > > > > Temp = Temp & " and " & txtBorrower2.Value & ", each, a '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 If
> > > > > fcnBuildBKBorrower = Temp
> > > > > End Function
> > > > >
> > > > >
> > > >
> > > >
> > > >