Qn: vbOKCancel -- Cancel doesn't work??

M

Michael Vaughan

Hello Everyone,

I have installed a print command to print a page. I also added a msgbox to
remind the person to turn on the printer. See below:

Sub PrintFP()
'
' Print Flight Plan Info Macro
' Macro recorded 8/21/2004 by Michael Vaughan
'

MsgBox "Be Sure Your Printer Is On!", vbInformation + vbOKCancel,
"Printer Alert"
Sheets("Print").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("Flight Plan").Select
Range("B4").Select

Well, the msgbox comes up ok.. but when I click on "Cancel" to NOT print..
it prints anyway??? What do I need to do to activate the cancel portion of
the OKCancel??

Thanks.. mv
 
M

mangesh_yadav

You need to tell what button is pressed, a little modification to you
code does the trick

Sub PrintFP()
'
' Print Flight Plan Info Macro
' Macro recorded 8/21/2004 by Michael Vaughan
'

button_pressed = MsgBox("Be Sure Your Printer Is On!", vbInformation
vbOKCancel, "Printer Alert")

if button_pressed = 1 then ' if ok is pressed, the value is 1

Sheets("Sheet2").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("Sheet1").Select
Range("B4").Select

else

msgbox("You cancelled the print command") ' if cancel presse
value is 2

end if

End Sub

- Manges
 
N

Norman Jones

Hi Michael,

Try something like:

Sub PrintFP()
Dim res As Long

res = MsgBox("Be Sure Your Printer Is On!", _
vbInformation + vbOKCancel, "Printer Alert")
If res = vbCancel Then
MsgBox "You cancelled!"
Exit Sub
End If
Sheets("Print").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("Flight Plan").Select
Range("B4").Select

End Sub
 
N

Nikos Yannacopoulos

Michael,

You are using the OK/Cancel message box type, but you are not checking what
the user has clicked. Modify your code as follows:

Sub PrintFP()
'
' Print Flight Plan Info Macro
' Macro recorded 8/21/2004 by Michael Vaughan
'

vConfirm = MsgBox("Be Sure Your Printer Is On!", vbInformation +
vbOKCancel, _
"Printer Alert")
If vConfirm = vbCancel Then Exit Sub
Sheets("Print").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("Flight Plan").Select
Range("B4").Select

HTH,
Nikos
 

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