Message box needed or if/then?

D

Dawn Rhoads

I would like to make some custom message boxes in response to some particular
errors. One error is '4605' and one is '5894'. I believe there is a way to
look for a particular error code and then substitute your own custom message
box?

The background: We have several fill-in forms that use various macros. In
order for the macros to run correctly two things must be true 1) must be
unprotected (the start out locked for filling in form fields) This is the
one that generates the 4605 error 2) must be in "print" view (the macro
inserts things into the header and footer, so it needs to be able to "see"
those). This one generates the 5894 error.

These were recorded macros, so the code is certainly not the cleanest in the
world, and I'm definitely no programmer! I'm sure something could be added
to the macro to change the protected setting and the view setting, but I'm
not sure how to make sure it is returned to whatever settings the user
originally had. I wouldn't want to have the macro change the settings on
them without returning to the original values. I'm also nervous about
unintended consequences -- for instance, it used to be that when you unlocked
and re-locked a form, all the information was erased. That problem has been
corrected, but who knows what the future might bring! That's why it seemed
like the dialog box approach might be better. Most of our users actually
already have the correct settings when they run the macro, it's just a
handful who occasionally have to be reminded.

If anyone can advise me on either the message box approach, or some fancy
way to detect and change the settings as appropriate, I'd sure appreciate the
help. Thanks for your time!
 
G

Greg Maxey

Dawn,

Maybe something like:

Sub ScratchMacro()
Dim Protection As Long
Dim View As Long

'Get the current protection and view type
Protection = ActiveDocument.ProtectionType
View = ActiveWindow.View

'Unprotect
On Error GoTo Ignore
ActiveDocument.Unprotect
Ignore: Err.Clear
'Set the view to PrintPreview
ActiveWindow.View.Type = wdPrintView

'Your code here

'Reapply Users preferred view
ActiveWindow.View.Type = View
'Reapply users protection type
On Error GoTo IgnoreProtect
ActiveDocument.Protect Type:=Protection, NoReset:=True
IgnoreProtect: Err.Clear

End Sub
 
D

Dawn Rhoads

Thanks Greg, I will try that. Way more complicated than anything I can
accomplish recording macros!

Would you by any chance have any advice about the alternate method of
detecting the particular error code and prompting a custom message box? I
know I've seen posts about that before, but they are so old I can no longer
find them in the archives.

Thanks for your help, I really appreciate it!
 
G

Greg

Dawn,

Maybe something like:

Sub ScratchMacro()

On Error Resume Next 'Defer error handling.
Err.Raise 5622 'Generate an "Overflow" error.
If Err.Number = 5622 Then
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
End If
Err.Clear
End Sub
 
D

Dawn Rhoads

Thanks, Greg. I can almost get this to work, but I must be doing something
wrong. Here's the code I tried based on your suggestion, including a part of
my macro that generates the error requiring the document to be in "print
layout" view before it will run:

Sub MessageBox()

On Error Resume Next 'Defer error handling.
Err.Raise 5622 'Generate an "Overflow" error.
If Err.Number = 5894 Then
MsgBox "You must change your page view. Go to the View menu and choose
'Page Layout'. Then run the macro again."

End If
Err.Clear

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.WholeStory
Selection.Delete
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub

This however, seems to generate the custom dialog box regardless of what
error Word generates, and regardless of whether an error was generated at
all. Even if I am in a document that has the correct view settings to run
the main part of the macro, I get the error message. And even if my form is
locked, which normally produces a different error message number, I get this
same custom dialog box.
 
G

Greg Maxey

Dawn

Well, you where generating the error after the fact :)

Consider:

Sub MessageBox()

On Error GoTo Handler
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.WholeStory
Selection.Delete
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Exit Sub
Handler:
If Err.Number = 5894 Then
MsgBox "You must change your page view. Go to the View menu and choose
" _
& """Page Layout"". Then run the macro again."
End If
Err.Clear
End Sub

Please remember that I am not a Err handler wizard. What I proposed will do
what you ask, but there is probably room for improvement.

Again, there is no need for generating an error. How about:

Sub NoNeedForMessageBox()

Dim Protection As Long
Dim View As Long

'Get the current protection and view type
Protection = ActiveDocument.ProtectionType
View = ActiveWindow.View

'Unprotect the document
'On Error GoTo Ignore
'ActiveDocument.Unprotect
'Ignore: Err.Clear
'Set the view to PrintPreview
ActiveWindow.View.Type = wdPrintView

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.WholeStory
Selection.Delete
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

'Reapply Users preferred view
ActiveWindow.View.Type = View
'Reapply users protection type
'On Error GoTo IgnoreProtect
'ActiveDocument.Protect Type:=Protection, NoReset:=True
'IgnoreProtect: Err.Clear

End Sub
 

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

Similar Threads


Top