Modal vs Modless Forms

M

m rafala

I can find lots of info on Modal vs Modeless forms in Word, but none answers
my question.

My biggest reason for wanting to use a modeless form is so that the user can
click out of the form and scroll around their document without closing the
form.

The problem is that once a form is set to modeless, the macro no longer
waits for the form to be dismissed before continuing. The macro just
continues to run.

Without restructuring my whole macro, how can I get a macro to call a form
and wait until it is dismissed, but still retain the ability to click and
scroll outside the form?
 
J

Jean-Guy Marcil

m rafala said:
I can find lots of info on Modal vs Modeless forms in Word, but none answers
my question.

My biggest reason for wanting to use a modeless form is so that the user can
click out of the form and scroll around their document without closing the
form.

The problem is that once a form is set to modeless, the macro no longer
waits for the form to be dismissed before continuing. The macro just
continues to run.

Without restructuring my whole macro, how can I get a macro to call a form
and wait until it is dismissed, but still retain the ability to click and
scroll outside the form?

Keep the code for "before" the form and "after" the form in different subs.

Try this for fun:
(You need a document with some text, a regular module called "Module1" and a
Userform called "frmCountCharacter" that contains a label and two buttons.
For the name of the three controls, refer to the code below to get them.

Code in "Module1":


Option Explicit

Sub CallForm()

Dim frmTemp As frmCountCharacter

'Do whatever you need to before displaying the form

Set frmTemp = New frmCountCharacter

With frmTemp
'Prepare the form, if needed
.lblCount.Caption = "Make a selection and click on ""Count""."
.Show vbModeless
End With

End Sub

Sub AfterForm()

With ActiveDocument
.Range.InsertAfter "Text inserted after form is closed."
End With

'Do whatever else you need to do after closing the form

End Sub


Code behind the Userform:

Option Explicit

Private Sub cmdCount_Click()

With Me
.lblCount.Caption = "The selection has " _
& Selection.Characters.Count & _
" characters."
End With

End Sub

Private Sub cmdQuit_Click()

Me.Hide

Unload Me

'Call the stuff to do "after" the form...
Module1.AfterForm

End Sub

To make sure that the code after gets executed, you might want to disable
the "X" on the userform, see:
http://word.mvps.org/FAQs/Userforms/DisableClose.htm
or, for a simpler version, which I do not like becasue it leaves the "X"
visible, so the user migth click expecting a standard behaviour, which has
been disabled...
http://word.mvps.org/FAQs/Userforms/InterceptCloseButton.htm
 
M

m rafala

Thanks. The problem is that the desired form would act like a messagebox
with extra functionality. It is called over and over again as needed. Your
technique makes the form responsible for initiating the rest of the code, so
I would have to restructure the whole macro to make the form the primary
caller of code.

You offer probably the only solution. But I was hoping someone might know a
simple way around it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top