Solved

Application_Quit() not firing?

Posted on 2004-10-27
722 Views
Last Modified: 2008-01-09
Hi I'm doing a bit of a tutorial - the super-easy guide to outlook object model - from MS
One of the "hands on" examples is supposed to fire off an email when the application_quit() event occurs.

The first time I tried to quit outlook I got an error stating that (ummm can't remember error and can't reproduce (sorry!)) but the debug button took me to the Set NewMail = ThisOutlookSession.CreateItem(olMailItem) line.

I changed nothing. Now I get no error but the sub is not run!
I've pasted the very simple sub below together with the _quit() call to the sub.
Outlook 2000 SR1 (Win2k) Exchange Client.
Thanks

Private Sub Application_Quit()
PollRestaurant
End Sub
-----
Sub PollRestaurant()
    Set NewMail = ThisOutlookSession.CreateItem(olMailItem)
    NewMail.Subject = "please open this email and make your lunch choice"
    NewMail.Body = "use the voting buttons to make your selection"
    NewMail.VotingOptions = "Subway; McDonalds; Kebab; KFC"
    Set receiverOfMyMail = NewMail.Recipients.Add("removed to fight spam")
    NewMail.Send
End Sub

the email address above is set to a valid account in real life.
the macro works fine if not invoked via the quit() event.
0
Question by:QPR
    6 Comments
     
    LVL 29

    Author Comment

    by:QPR
    have rediscovered the error dialogue when I quit Outlook:
    "Internal application error"

    Also forgot to mention that I click "enable" when I start Outlook and it warns me of resident macros
    0
     
    LVL 3

    Expert Comment

    by:brd24gor
    What is your security setting set to in Outlook (Tools > Macros > Security)?  If it is set to High, you may want to try and bump it down to Medium or even Low if you have a virus scanner running.  Possibly this could be keeping Application_Quit() from running?
    0
     
    LVL 3

    Expert Comment

    by:brd24gor
    I recreated your problem and the security setting does appear to be the problem.  If you have your security set to High, it won't run the macro and won't prompt you at all.  If you have it set to Medium, Outlook will tell you there are macros and ask whether you want to disable or enable them, and if you click Enable, your Application_Quit() will run just fine.  However, Outlook will prompt you EVERY time you open it.  The Low security setting works like Medium except that you aren't prompted to enable macros.  They are automatically enabled.

    Hope this helps!
    0
     
    LVL 29

    Author Comment

    by:QPR
    if I set secuirty to "low" then it will fire *sometimes*
    If I click the 'x' at the top right to exit Outlook then nothing. If I choose file-exit & log off then it will.
    However it causes an "internal application error" and spews on this line:
     Set NewMail = ThisOutlookSession.CreateItem(olMailItem)

    is the ThisOutlookSession session invalid for the application_quit event?
    Meaning am I trying to refer to a session that has been closed/destroyed?
    0
     
    LVL 3

    Accepted Solution

    by:
    http://support.microsoft.com/default.aspx?scid=kb;en-us;292796

    If you have any global variables, they will not work in the Application_Quit() event as described above in the Microsoft KB article.  You may be trying to refer to a variable that has been destroyed.  If you have any global variables, try declaring them locally in the Application_Quit() event.  The article also suggests making a COM add-in, which I don't have much experience in.
    0
     
    LVL 29

    Author Comment

    by:QPR
    thanks.
    With this in mind I put the whole of the code in the quit() event rather than calling it.

    Private Sub Application_Quit()
    Set NewMail = ThisOutlookSession.CreateItem(olMailItem)
        NewMail.Subject = "please open this email and make your lunch choice"
        NewMail.Body = "use the voting buttons to make your selection"
        NewMail.VotingOptions = "Subway; McDonalds; Kebab; KFC"
        Set receiverOfMyMail = NewMail.Recipients.Add("me@home.com")
        NewMail.Send
    End Sub

    other than the reference to ThisOutlookSession everything is pretty much self contained yet the same error occurs "internal application error"
    Outlook 2000
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    Learn more about how the humble email signature can be used as more than just an electronic business card. When used correctly, a signature can easily be tailored for different purposes by different departments within an organization.
    Learn more about the importance of email disclaimers with our top 10 email disclaimer DOs and DON’Ts.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now