Group: microsoft.public.word.vba.general
From: =?Utf-8?B?SmVhbi1HdXkgTWFyY2ls?=
Date: Friday, April 04, 2008 7:17 AM
Subject: RE: Code Hanging - Part II

"NZ VBA Developer" wrote:

> That's got it, Jean-Guy! Thanks so much for the help.
>
> It was a little tricky trying to figure out how to pass the values from the
> UserForm back to the calling module so I could use them to update the
> document, but I managed to do it. And determining how the UserForm was closed
> also presented a bit of a challenge, as I only wanted the 'Envelopes and
> Labels' dialog to be displayed if the user clicked the 'OK' button. In the
> end I used the following:
>
> Private Sub AddEnvelope()
> Dim frmMyForm As frmEnvelopeDetails
> bAddEnvelope = False
> Set frmMyForm = New frmEnvelopeDetails
> frmMyForm.Show
> Unload frmMyForm
> Set frmMyForm = Nothing
> If bAddEnvelope = True Then CreateEnvelope
> Tools.SetToolbar
> End Sub
>
> Sub CollectUserFormValues()
> bAddEnvelope = True
> RecipName = frmEnvelopeDetails.txtRecipientName
> RecipAddressL1 = frmEnvelopeDetails.txtRecipientAddressL1
> RecipAddressL2 = frmEnvelopeDetails.txtRecipientAddressL2
> RecipSuburb = frmEnvelopeDetails.txtRecipientSuburb
> RecipCity = frmEnvelopeDetails.txtRecipientCity
> RecipPostCode = frmEnvelopeDetails.txtRecipientPostCode
> RecipAttention = frmEnvelopeDetails.txtRecipientAttention
> End Sub
>
> To explain:
>
> bAddEnvelope is a Boolean variable that gets initialised to 'False' in the
> 'AddEnvelope' module. The 'CollectUserFormValues' module is called in the
> Click event for the 'OK' button. This module not only collects the values
> from the UserForm for use in the 'Envelopes and Labels' dialog, but also sets
> the bAddEnvelope flag to 'True'. (Clicking the 'Cancel' button just hides the
> form and leaves the flag set to 'False'.) This flag is then evaluated to see
> if the built-in dialog should be displayed, which is done in the
> 'CreateEnvelope' module. (This module also does a few other things: unlocks
> the document if it's protected, updates a toolbar button, etc.)
>
> Now I just need to clean up the code that's used to create the document in
> the first place to follow the 'minimal code in the UserForm' rule and I'll be
> away laughing. Thanks again!

Glad to see that you sorted it out...
Just a quick comment on your code.. You do not need a separate module to
collect the info...

For example:

Private Sub AddEnvelope()

Dim frmMyForm As frmEnvelopeDetails
Dim boolGoAhead As Boolean

'This could be a public global variable in the Userform module _
set it to false in the Initialize event, then if OK is clicked, set it
to true
'bAddEnvelope = False

boolGoAhead = False

Set frmMyForm = New frmEnvelopeDetails

With frmMyForm
.Show
boolGoAhead = .bAddEnvelope
If boolGoAhead Then
RecipName = .txtRecipientName
RecipAddressL1 = .txtRecipientAddressL1
RecipAddressL2 = .txtRecipientAddressL2
RecipSuburb = .txtRecipientSuburb
RecipCity = .txtRecipientCity
RecipPostCode = .txtRecipientPostCode
RecipAttention = .txtRecipientAttention
End If
End With

Unload frmMyForm
Set frmMyForm = Nothing

If boolGoAhead Then CreateEnvelope
Tools.SetToolbar

End Sub

Safety Articles | Usenet Groups | Usenet News | Bluegrass