How to Write Error Trap

  • Thread starter Thread starter GavinS
  • Start date Start date
G

GavinS

I have the following code to toggle between print preview and normal
view but I need to add an error statement just in case there is no
sheet open.

Can someone please suggest how I might write this.

Thanks

Sub TogglePrintView()

If ActiveWindow.View = xlPageBreakPreview Then
ActiveWindow.View = xlNormalView
ElseIf ActiveWindow.View = xlNormalView Then
ActiveWindow.View = xlPageBreakPreview
End If
End Sub
 
Sub TogglePrintView()
If Not Activesheet is Nothing Then ActiveWindow.View = 3- ActiveWindow.View
End Sub


If this post helps click Yes
 
Sub TogglePrintView()
If Not Activesheet is Nothing Then ActiveWindow.View = 3- ActiveWindow.View
End Sub

If this post helps click Yes
---------------
Jacob Skaria







- Show quoted text -

Nope, pasted that before the first line of code and it returns an error
 
How can you have a workbook with no sheet open?
Does the code below ever stop by error?

Keiji
 
if there's no workbook open (1) where would you put the code and (2) print /
print preview isn't available
 
Then, How can you run the macro?

Keiji

Patrick said:
if there's no workbook open (1) where would you put the code and (2)
print / print preview isn't available
 
It can even be a procedure within an Add-In..

GavinS

'"Nope, pasted that before the first line of code and it returns an error"

As mentioned above you should not be pasting the below code to the other
procedure. The procedure itself is re-written to suit your requirement. So
try with the new procedure.

If this post helps click Yes
 
Even if that macro is within an Add-in, Is there a situation that there
is no workbook and no active window? if there is such a situation, that
macro surely would fails. but i can't imagine such a situation.

Keiji
 
Try saving a workbook as .xla and open it. From VBE check out your project
explorer.

If this post helps click Yes
 
I understand what you are saying at last, but is this usual way of using
add-in? How can i use this macro in this case? I think i need to write
some macro to add a menu to use this macro into Workbook_Open procedure.

Keiji
 
I'd add something like this to the top of the procedure:

If ActiveWindow Is Nothing Then
Exit Sub
End If
 
What do you want to do when error occur?
if you want to just ignore, one way is like this.

Sub TogglePrintView()
on error goto ex
If ActiveWindow.View = xlPageBreakPreview Then
ActiveWindow.View = xlNormalView
ElseIf ActiveWindow.View = xlNormalView Then
ActiveWindow.View = xlPageBreakPreview
End If
exit sub
ex:
msgbox "there is no sheet open" <<==if not need, delete this
End Sub

By the way, are you using this macro in Add-in?

Keiji
 
Thanks for all the interesting discussion.

The macro is called from a button or shortcut so can be called when
ActiveWindow is nothing.

I used Dave Peterson's code and that seems to work fine

If ActiveWindow Is Nothing Then
Exit Sub
End If

Thanks again
 
I realize that i had a big misunderstanding. there is a situation with
no sheet open in a ordinary use. just to hide the Workbook that your
macro is written is enough for your macro without error trap to fail.
Sorry for useless discussion.

Keiji
 
Option Explicit

Sub TogglePrintView()
Dim wndw As Window
Set wndw = ActiveWindow
If Not wndw Is Nothing Then
If ActiveWindow.View = xlPageBreakPreview Then
ActiveWindow.View = xlNormalView
ElseIf ActiveWindow.View = xlNormalView Then
ActiveWindow.View = xlPageBreakPreview
End If
End If
End Sub
 
the code checks to see if there is a workbook open before attempting to
toggle the print view --- as per your question - this thread
 
I'm not asking the code but asked whether there is such a situation with
no window open when you run the macro. About this question, Jacob said
if the macro is in add-in there could be. i said even if the macro is
add-in, i don't think there is such situation. Jacob suggested that you
open .xla file directly and check. In this case, Excel surely open with
no window open, but you can't run the macro without automatically
loading command button or menu connected to the macro when you open
..xla. I think this is not usual way of using add-in. But i made big
misunderstanding as i said, because i realized just hiding window can
produce such a situation easily. Dave and Jacob already gave a code to
avoid the error. It might be not relevant comment to the subject but I
think these are error avoid code, not error trap code.

P.S.
I think Jacob wrote a smart one as below.

Sub TogglePrintView()
If Not Activesheet is Nothing Then ActiveWindow.View = 3- ActiveWindow.View
End Sub

Keiji
 

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

Back
Top