Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

Message box needed or if/then?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dawn Rhoads - 26 Apr 2005 01:59 GMT
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!
Greg Maxey - 26 Apr 2005 02:28 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> 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
[quoted text clipped - 26 lines]
> fancy way to detect and change the settings as appropriate, I'd sure
> appreciate the help.  Thanks for your time!
Dawn Rhoads - 28 Apr 2005 19:14 GMT
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!

> Dawn,
>
[quoted text clipped - 56 lines]
> > fancy way to detect and change the settings as appropriate, I'd sure
> > appreciate the help.  Thanks for your time!
Greg - 28 Apr 2005 19:41 GMT
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
Dawn Rhoads - 28 Apr 2005 22:48 GMT
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.

> Dawn,
>
[quoted text clipped - 10 lines]
> Err.Clear
> End Sub
Greg Maxey - 29 Apr 2005 01:50 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> 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,
[quoted text clipped - 43 lines]
>> Err.Clear
>> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.