Cancel button doesn't cancel

A

Angie M.

Hello, I'm using Word '03. I have a macro that opens a form where the users
have a choice of style options (which are macros). My problem is, the Cancel
button (btnCancel) does not work. It successfully hides the form, but if a
user clicks an option button before hitting cancel, it still carries out the
macro. I would like to be able to cancel the whole thing if I hit cancel.
Here is the macro that is stored in the global template:

Sub CallNumForm()
With frmNumber
.Show
If frmNumber.btnCancel = True Then
Exit Sub
End If
End With
Unload frmNumber
Set frmNumber = Nothing
End Sub

BEHIND THE FORM I HAVE THE FOLLOWING CODE:

Public Sub UserForm_Initialize()

With frmNumber
..optNone = True
..optNone.SetFocus
End With
End Sub

Sub btnOK_Click()
btnOKclicked = True
Me.Hide
End Sub

Sub btnCancel_Click()
btnOKclicked = False
Me.Hide
End Sub

Sub opt1_click()
Call NumOut1
End Sub

Sub opt2_Click()
Call NumOut2
End Sub


Sub NumOut1()
' ActiveDocument.CopyStylesFromTemplate Template:= _
"c:\Docs\Demo\Style Sheets\#1 Style Sheet.dot"
CommandBars("Paragraph Numbering").Visible = True
End Sub

Sub NumOut2()
ActiveDocument.CopyStylesFromTemplate Template:= _
"c:\Docs\Demo\Style Sheets\#2 Style Sheet.dot"
CommandBars("Paragraph Numbering").Visible = True
End Sub

ANY SUGGESTIONS WOULD BE APPRECIATED!
 
H

Helmut Weber

Hi Angie,
if a user clicks an option button before hitting cancel,
it still carries out the macro.

That's the way VBA was designed.

Use a flag, a boolean variable which is set to true,
if a button was clicked. You may change
the appearance of that button in addition.

If a user hits the cancel button, exit.
If a user hits the big "GO" or "OK" button,
then loop through your array of booleans
and execute every sub associated with them.

Like:
if bBtn(1) = true then Macro1
if bBtn(2) = true then Macro2

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Angie M.

Thank you Helmut. I'm confused because I do address this:

Sub CallNumForm()
With frmNumber
.Show
If frmNumber.btnCancel = True Then
Exit Sub
End If
End With
Unload frmNumber
Set frmNumber = Nothing
End Sub

but this is not working. Do I have to tie every other macro to the click
event of the OK button? thanks
 
H

Helmut Weber

Hi Angie,

yes, IMHO.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jonathan West

Hi Angie,

You need to understand better how events work in UeerForms.

The code in the opt1_click event runs as soon as you click opt1 - and runs
again every time you click it.

Therefore, if you want that code to run only when the dialog is closed using
the OK button, you ned to change things round like this.

Sub CallNumForm()
frmNumber.Show
End Sub

BEHIND THE FORM I HAVE THE FOLLOWING CODE:

Public Sub UserForm_Initialize()

With frmNumber
.optNone = True
.optNone.SetFocus
End With
End Sub

Sub btnOK_Click()
If opt1 Then Call NumOut1
If opt2 Then Call NumOut2
Unload Me
End Sub

Sub btnCancel_Click()
Unload Me
End Sub


Sub NumOut1()
' ActiveDocument.CopyStylesFromTemplate Template:= _
"c:\Docs\Demo\Style Sheets\#1 Style Sheet.dot"
CommandBars("Paragraph Numbering").Visible = True
End Sub

Sub NumOut2()
ActiveDocument.CopyStylesFromTemplate Template:= _
"c:\Docs\Demo\Style Sheets\#2 Style Sheet.dot"
CommandBars("Paragraph Numbering").Visible = True
End Sub
 
H

Helmut Weber

hm...

ok, if it helps,

you can create arrays of controls in VBA at runtime.
In VB, you can create them at design time, too.

Google for "array" "controls" and my decent name.

Jonathan, I think, just wanted to spare you this advanced exercise.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

Angie M.

THANK YOU THIS WAS VERY HELPFUL.

Jonathan West said:
Hi Angie,

You need to understand better how events work in UeerForms.

The code in the opt1_click event runs as soon as you click opt1 - and runs
again every time you click it.

Therefore, if you want that code to run only when the dialog is closed using
the OK button, you ned to change things round like this.

Sub CallNumForm()
frmNumber.Show
End Sub

BEHIND THE FORM I HAVE THE FOLLOWING CODE:

Public Sub UserForm_Initialize()

With frmNumber
.optNone = True
.optNone.SetFocus
End With
End Sub

Sub btnOK_Click()
If opt1 Then Call NumOut1
If opt2 Then Call NumOut2
Unload Me
End Sub

Sub btnCancel_Click()
Unload Me
End Sub


Sub NumOut1()
' ActiveDocument.CopyStylesFromTemplate Template:= _
"c:\Docs\Demo\Style Sheets\#1 Style Sheet.dot"
CommandBars("Paragraph Numbering").Visible = True
End Sub

Sub NumOut2()
ActiveDocument.CopyStylesFromTemplate Template:= _
"c:\Docs\Demo\Style Sheets\#2 Style Sheet.dot"
CommandBars("Paragraph Numbering").Visible = True
End Sub

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 

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