Re: VBA shutdown error Outlook



In Outlook VBA there is an intrinsic Application object. You need to do nothing to have it available. An example:

Dim oExpl As Outlook Explorer
Set oExpl = Application.ActiveExplorer

The only legitimate way to compare EntryID's or StoreID's is to use a lower level API such as CDO to call CompareIDs. You might get a short-term EntryID or a long-term EntryID and in that case CompareIDs recognizes them as the same but an equality comparison would fail.

I know the error and warning messages, those only tell me that you crashed Outlook.

You need to run in debug mode and see what errors are fired when you perform the actions that cause the errors.

You should always create any buttons, etc. using a unique Tag string. That way you can find your button by iterating the Controls collection of your toolbar.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"BartH_NL" <barth@xxxxxxxxx> wrote in message news:1163690117.205410.243470@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello Ken, thank you for your answer. I have some questions though, I
hope you have a little time to explain:

If this is Outlook VBA code then always use the intrinsic Application object
and never use CreateObject to get an instance of the
Outlook.Application
object.
- I don't understand - how can I call the Outlook.Application in an
other way than CreateObject("Outlook.Application")?

Always handle all errors and release all local objects at the end of the
procedure they are declared in.
- I had all the objects seperatly released per subroutine and received
the same error - I shortened my code by releasing the objects by
exitHandler which releases all options at once at the end of each
subroutine. Isn't this a good way to handle this? I learned this from
the ItemCB example, mentioned elsewhere.

Application_Quit - I understand

Temporary parameter set to True - I understand

Never compare EntryID or StoreID properties for equality
- where do I do this? If you mean the myPos I look for to check the
button - what other way would you suggest (users may move or rename the
button and it has no ID so I ask for a confirmation if it is not on the
3rd position).

In the meanwhile I have reviewed my code again, but still receive the
same error message...

All the functionalty I want is there but the error remains.

The error occurs at Outlook shutdown after I have sent a new message or

after I hit the button. When I change nothing and I send no e-mail, the

error does not occur. The error message pops up about 8 seconds after
Outlook is shut down. To see the message, check
http://www.nedcom.nl/images/outlook_error.jpg.

When I reopen Outlook, I get a message that a severe VBA error has
occured and I am invited to disable the code. Check
http://www.nedcom.nl/images/startup_message.jpg.

(These messages are in Dutch, but I bet you'll recognise them.)

Thanks and regards,
BartH

Ken Slovak - [MVP - Outlook] wrote:
If this is Outlook VBA code then always use the intrinsic Application object
and never use CreateObject to get an instance of the Outlook.Application
object.

Always handle all errors and release all local objects at the end of the
procedure they are declared in.

By the time Application_Quit fires all Outlook objects are already out of
scope. Any attempt to access them will fire an error.

Never compare EntryID or StoreID properties for equality.

If you create toolbars, menus or buttons always create them with the
Temporary parameter set to True.

Instead of firing errors on purpose try testing for things before accessing
them. One example:

Set myInspector = myolapp.ActiveInspector
Set myBar = myInspector.CommandBars("Standard")

Instead use code like this:

If Not (myolapp.ActiveInspector Is Nothing) Then
' instantiate
Else
' whatever
End If

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"BartH_NL" <barth@xxxxxxxxx> wrote in message
news:1163629854.191845.61550@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hello, like so many code cats before me I now have a persistant
> shutdown error in my Outlook VBA code. I do know I have to clean up my
> object declarations and have a lot of cleaning going on but stil can't
> find the error. Maybe somebody is more awake that I? (Isn't there an
> code cleaner / debugger app?)
>
> The code is meant to add an on/off commandbarbutton to a Send and File
> script.
>
> Here is the code:
>
> Public myFlag As Boolean
> Public myPos As Integer
> Dim myolapp As Outlook.Application
> Dim myInspector As Outlook.Inspector
> Dim myExplorer As Outlook.Explorer
> Dim myBar As CommandBar
> Dim myButton As CommandBarButton
>
> Sub installSendAndFile()
>
> On Error Resume Next
> Set myolapp = CreateObject("Outlook.Application")
> Set myInspector = myolapp.ActiveInspector
> Set myBar = myInspector.CommandBars("Standard")
>
> If myInspector Is Nothing Then
> MsgBox "Please activate a 'New message' window and run this macro
> again.", vbExclamation, "Not ready"
> Exit Sub
> End If
>
> If myBar.Controls("Send &and save").Index > 0 Then myPos =
> myBar.Controls("&Send and save").Index
> If myBar.Controls("Send &not save").Index > 0 Then myPos =
> myBar.Controls("&Send not save").Index
>
> If myPos = 0 Then
> Set myButton = myBar.Controls _
> .Add(msoControlButton, , , 3)
> With myBar.Controls(3)
> .OnAction = "Project1.ThisOutlookSession.setFlag"
> .FaceId = 7267
> .Style = msoButtonIconAndCaption
> End With
> setFlag
> Else
> MsgBox "The button '" & myBar.Controls(MyPos).Caption & "' alread
> exists."
> End If
>
> ' CLEAN-UP
>
> exitHandler
>
> End Sub
>
> Sub setFlag()
>
> On Error Resume Next
> Set myolapp = CreateObject("Outlook.Application")
> Set myInspector = myolapp.ActiveInspector
> Set myBar = myInspector.CommandBars("Standard")
>
> If myBar.Controls("Send &and save").Index > 0 Then myPos =
> myBar.Controls("Send &and save").Index
> If myBar.Controls("Send &not save").Index > 0 Then myPos =
> myBar.Controls("Send &not save").Index
> If myPos = 0 Then
> myPos = 3
> msgVraag = MsgBox("The button 'Send &and save' or 'Send &not
> save' seems not to exist." & vbCr & "Is this the button on position " &
> myPos & "?", vbYesNo)
> End If
> If msgVraag = vbNo Then
> MsgBox "Because of an unexpected event this procedure is ended." &
> vbLf & "Please contact the programmer or remove and reinstall the
> commandbarbutton."
> Exit Sub
> End If
>
> If myFlag = True Then
>
> myFlag = False
>
> With myBar.Controls(MyPos)
> .FaceId = 2617
> .TooltipText = "Send and save is OFF," & vbLf & "click to
> ENABLE save the file to a folder"
> .Caption = "Send &and save"
> End With
>
> Else
>
> myFlag = True
>
> With myBar.Controls(MyPos)
> .FaceId = 7267
> .TooltipText = "Send and save is ON," & vbLf & "click to
> DISABLE save the file to a folder"
> .Caption = "Send &not save"
> End With
>
> End If
>
> ' CLEAN-UP
>
> exitHandler
>
> End Sub
>
> Private Sub Application_ItemSend(ByVal Item As Object, Cancel As
> Boolean)
>
> On Error Resume Next
> ' Check for myFlag state to enable or disable Send and File If myFlag
> = False Then Exit Sub
>
> If Item.Class < 50 Then ' check to see if item type is appointment, if
> so don't file
> Dim objNS As NameSpace
> Dim objFolder As MAPIFolder
> Set objNS = Application.GetNamespace("MAPI")
> Set objFolder = objNS.PickFolder
> If TypeName(objFolder) <> "Nothing" And _
> IsInDefaultStore(objFolder) Then
> Set Item.SaveSentMessageFolder = objFolder
> End If
> Set objFolder = Nothing
> Set objNS = Nothing
> End If
>
> ' CLEAN-UP
>
> exitHandler
>
> End Sub
>
> Public Function IsInDefaultStore(objOL As Object) As Boolean Dim objApp
> As Outlook.Application Dim objNS As Outlook.NameSpace Dim objInbox As
> Outlook.MAPIFolder
>
> On Error Resume Next
> Set objApp = CreateObject("Outlook.Application")
> Set objNS = objApp.GetNamespace("MAPI")
> Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
> Select Case objOL.Class
> Case olFolder
> If objOL.StoreID = objInbox.StoreID Then
> IsInDefaultStore = True
> End If
> Case olAppointment, olContact, olDistributionList, _
> olJournal, olMail, olNote, olPost, olTask
> If objOL.Parent.StoreID = objInbox.StoreID Then
> IsInDefaultStore = True
> End If
> Case Else
> MsgBox "This function isn't designed to work " & _
> "with " & TypeName(objOL) & _
> " items and will return False.", _
> , "IsInDefaultStore"
> End Select
>
> ' CLEAN-UP
>
> exitHandler
>
> End Function
>
> Public Sub exitHandler()
>
> On Error Resume Next
>
> Set myolapp = Nothing
> Set myExplorer = Nothing
> Set myInspector = Nothing
> Set myBar = Nothing
> Set myButton = Nothing
> Set objApp = Nothing
> Set objNS = Nothing
> Set objInbox = Nothing
>
> End Sub
>
> Private Sub Application_Quit()
> ' CLEAN-UP
> exitHandler
> End Sub
>
> Private Sub Application_Startup()
> myFlag = True
> End Sub
>
> Thanks,
> BartH
>


.


Loading