none
VBScript Help! Ending Outlook.exe RRS feed

  • Question

  • Hi, I am somewhat familiar with VBS, but not very good at making my own scripts.

    I need to be able to close outlook regardless of there being other windows open like new e-mails with unsaved data in them.

    I want it to go something like this:

    If Outlook.exe is running
    do exit outlook.exe
    If outlook.exe is NOT running
    End this script

    I dont want my script to continue running if Outlook is completely closed.

    I have two scripts that help me accomplish this, but they both continue to run even though Outlook is not there anymore.

    Dim oOL 'As Outlook.Application
    Set oOL = GetObject(, "Outlook.Application")
    If oOL Is Nothing Then
    Else
    Do
    'Outlook running
    oOL.Session.Logoff
    oOL.Quit
    Loop
    End If
    Set oOL = Nothing

    __________________

    WScript.Sleep 300
    Set wshShell = CreateObject("WScript.Shell")
    Do
        ret = wshShell.AppActivate("Microsoft Outlook")
        If ret = True Then
            wshShell.SendKeys "{Y}"
    WScript.Sleep 300
        End If
    Loop

    Thursday, March 6, 2014 3:31 PM

Answers

  • Below is the scrip that I made to answer my own question:

    Set wshShell = CreateObject("WScript.Shell")
     ret = wshShell.AppActivate("Microsoft Outlook")
    Do Until ret = False
     ret = wshShell.AppActivate("Microsoft Outlook")
            wshShell.SendKeys "{Y}"
        WScript.Sleep 300
    Loop

    ________________________________________________________

    I am not sure if the second line is neccessary or not, but it works as is, so I am not changing it.

    My fix is that, I have two VBS files that run on a schedual one right after the other.

    First I run the script provided by Bill, simply because it is shorter than the script I had that was working.

    On Error Resume Next
    Set Outlook = GetObject(, "Outlook.Application")
    If Err = 0 Then
    Outlook.Quit()
    End If

     Then I run the script I made

    Set wshShell = CreateObject("WScript.Shell")
     ret = wshShell.AppActivate("Microsoft Outlook")
    Do Until ret = False
     ret = wshShell.AppActivate("Microsoft Outlook")
            wshShell.SendKeys "{Y}"
        WScript.Sleep 300
    Loop

    And Outlook is successfully closed with all data progress saved, and no Errors, or corruption.

    Congratulations on being the most complicated people to talk to and only having half an answer.

    • Marked as answer by Josh Ragan Friday, March 7, 2014 3:35 PM
    Friday, March 7, 2014 3:33 PM

All replies

  • You cannot close a copy of Outlook that you didn't start.

    For the copy you started just Quit.

    It is a bad idea to force a users session of Outlook closed.  Why do you think you need to do this?

    You can kill a process at the command line.  You cannot use other methods with outlook.

     tskill outlook /v


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 3:46 PM
  • The point here is to close outlook with an automated process.

    Something that can be set in a Scheduled Task so that something that requires outlook to be closed can run after the fact without any error.

    If a user left outlook running, and had opened items, and an opened item had unsaved changes, then outlook will prompt to save those changes.

    Preferably, I want to be able to have outlook close, and save the changes automatically.

    If not, we can still just flat out close outlook without saved changes.

    But there is no way to tell how many windows will be open at the time it is scheduled to run. 

    Thursday, March 6, 2014 4:43 PM
  • Thanks Bill,

    This looks promising.

    I will let you know the outcome.

    EDIT:

    I did try this, and the script does work, however I still need the save changes window to close in order for outlook to finish closing.

    My script above does this for me, but again, it continues to run after its closed. Please help me stop my script from running after the job is finished.

    Thanks,


    • Edited by Josh Ragan Thursday, March 6, 2014 4:50 PM
    Thursday, March 6, 2014 4:44 PM
  • The point here is to close outlook with an automated process.

    Something that can be set in a Scheduled Task so that something that requires outlook to be closed can run after the fact without any error.

    If a user left outlook running, and had opened items, and an opened item had unsaved changes, then outlook will prompt to save those changes.

    Preferably, I want to be able to have outlook close, and save the changes automatically.

    If not, we can still just flat out close outlook without saved changes.

    But there is no way to tell how many windows will be open at the time it is scheduled to run. 

    You cannot do all that you want with Outlook if you didn't start it.  If you started it and retain a variable for outlook you can close and save,

    With Office, when we want to do this kind of thing, we use VBA and an Outlook Project file that is loaded at startup.  The file can sit and monitor the time, the user or anything else we want and can close and save anything in Outlook.

    This is how Outlook is designed to be automated at the level you require.  Most of this is not possible from outside of Outlook.

    Outlook 2010 and later have relaxed security and may actually allow an attached session to save and close windows.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 4:49 PM
  • Nope. Outlook 2013 still alerts when closed by VBScript if any window is open or if it needs to empty the trash.

    This script does close outlook silently but cannot save any open forms or messages. Any messages being edited will cause the code to fail.

    while(Get-Process outlook|   Foreach-Object { $_.CloseMainWindow()} ){}

    Only TSKILL can kill completely and easily.

    This, too, can kill:

    Get-Process outlook | stop-process –force


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 5:03 PM
  • How bout we stop talking about what you think can't be done with outlook and get to how I can make a script that can send a key to a window(s) with a specific Title and then end the script upon it not finding anymore widows with that Title.

    Below is a part of a script that can send a key to a window with the title "Microsoft Outlook".

    Set wshShell = CreateObject("WScript.Shell")
    Do
        ret = wshShell.AppActivate("Microsoft Outlook")
        If ret = True Then
            wshShell.SendKeys "{Y}"

    Any real help is greatly apreciated.

    Thursday, March 6, 2014 5:13 PM
  • SendKeys will never reliably work. You can try, but it will randomly fail on you.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Thursday, March 6, 2014 5:17 PM
  • Good to know.

    But, is the Sleep and loop feature not a quick fix for that?

    If it waits and trys again, it will succeed one of the times it trys right?

    Thursday, March 6, 2014 5:24 PM
  • Good to know.

    But, is the Sleep and loop feature not a quick fix for that?

    If it waits and trys again, it will succeed one of the times it trys right?

    Perhaps, but also perhaps not. I've wasted plenty of time in the past fighting with stupid SendKeys...

    Also, I looked at your original loops and you don't have any sort of way to break out of them (no until, while, or exit):

    http://ss64.com/vb/do.html


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Thursday, March 6, 2014 5:36 PM
  • How bout we stop talking about what you think can't be done with outlook and get to how I can make a script that can send a key to a window(s) with a specific Title and then end the script upon it not finding anymore widows with that Title.

    Below is a part of a script that can send a key to a window with the title "Microsoft Outlook".

    Set wshShell = CreateObject("WScript.Shell")
    Do
        ret = wshShell.AppActivate("Microsoft Outlook")
        If ret = True Then
            wshShell.SendKeys "{Y}"

    Any real help is greatly apreciated.

    Sorry guy but we don't do keys and that will not work with Outlook anyway.  That is why you aren't finding any scripts.

    I have programmed Outlook for many years.  I am trying to tell you how to do this with VBA because that is th only reliable way to do what yo aree trying to do.  Second comes killing the task directly.

    Here is the problem you have.  Outlook dialog boxes are not Windows Dialogs but are special Outlook dialogs owned by the task running the Window that is the parent.  When you send keys the keys go to the parent Window or to the main Outlook Window and are no passed to the dialog.  THis is by design and is true throughout Windows and has always been true.

    A macro in Outlook can find open Windows and kill the edit flag so the window will close without a prompt.  It can also Quit outlook without any annoying alerts.  This cannot be done from outside of Outlook by design.  If you want I can find and post numerous add-ins that you can purchase that can change this behavior.

    So try and understand why you are having issues.  You cannot get blood from a stoney Outlook.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 5:38 PM
  • Good to know.

    But, is the Sleep and loop feature not a quick fix for that?

    If it waits and trys again, it will succeed one of the times it trys right?

    Perhaps, but also perhaps not. I've wasted plenty of time in the past fighting with stupid SendKeys...

    Also, I looked at your original loops and you don't have any sort of way to break out of them (no until, while, or exit):

    http://ss64.com/vb/do.html


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    The SendKeys cannot answer a dialog no matter how long you wait,


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 5:39 PM
  • More info.  When we are building test software to exercise input forms we have to use a test harness that can get the window ID.  It also has to send an appropriate dialog message to address the dialog process.  This si always done with a hook in the program because things like sendkeys cannot do this.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 5:41 PM
  • There are also add on utilities that can hook the program.  Unfortunately, because Microsoft has added extra security to Outlook,  none of them work with Outlook in the way you need.

    Just kill the program.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 5:43 PM
  • The SendKeys cannot answer a dialog no matter how long you wait,
    See, that's why I cover myself with 'perhaps not'. =]

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Thursday, March 6, 2014 6:15 PM
  • The SendKeys cannot answer a dialog no matter how long you wait,

    See, that's why I cover myself with 'perhaps not'. =]

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Actually, to be more accurate, there may be an occasion where it does hit the dialog.  I have never figured out why that is.  It is just extremely unreliable if there is a dialog showing and still unreliable without it.

    Hey it worked great in Windows 98.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 6:29 PM
  • The larger question is: Why do you need to close Outlook?

    Bill


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 6, 2014 6:46 PM
    Moderator
  • We have a signature manager that pushes the signature from the server to the PC.

    Outlook will not recognize the updated signature if a change is made, untill we shut it down and start it back up.

    This goes across many computers in our office.

    We wanted to have a way for outlook to close if running and then push the updated signature, without checking everyones computer one by one.

    @jrv

    You said: "The SendKeys cannot answer a dialog no matter how long you wait,"

    I have proof on mutiple computers that my script above does in fact answer the dialog.

    So regardless of what you may know about it, it does work in my case. We have Windows 7 computers with Office 2010 and 2013.

    Mike said: "Also, I looked at your original loops and you don't have any sort of way to break out of them (no until, while, or exit):"

    And, I know this, but I do not know how to properly go about adding them.

    That is exactly what I am asking this forum for help with.

    I know for a fact that my scripts above work. Im not looking for alternatives. I just want them to stop running when the job is complete.

    Can anybody help me make this happen?


    • Edited by Josh Ragan Thursday, March 6, 2014 7:42 PM
    Thursday, March 6, 2014 7:34 PM
  • Are you asking about this loop?

    while(Get-Process outlook|   Foreach-Object { $_.CloseMainWindow()} ){}

    It exits when all copies of Outlook have been successfully terminated.

    It is PowerShell which is more complete than VBScript.

    You may have luck with closing dialogs until it doesn't work then you are lost in unknown territory.

    Ask everyone else here how this goest

    Exchange can create signatures for users and they are updated immediately whenever the template is changed.

    If you replace a local signature file you do not have to shut Outlook down. The change is immediate assuming you are doing it correctly.

    If this is all about updating a signature file then you are really doing it the complete hardest way.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 7:48 PM
  • You also said: "Also, I looked at your original loops and you don't have any sort of way to break out of them (no until, while, or exit):"

    And, I know this, but I do not know how to properly go about adding them.

    That is exactly what I am asking this forum for help with.

    Doesn't the syntax link I posted clear this up for you? If not, what do you need clarification on?


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Thursday, March 6, 2014 7:49 PM
  • Thanks Mike, I will have to learn more about these and make up my own script.

    I honestly don't see why somebody in the script center here cannot take what I have and make it for me, but I will use more of my time up and do it myself.

    Mike and Bill, you have been the most help here so far, but I just don't think I will be asking anymore question in this forum.

    HUGE WAIST OF TIME.

    Thursday, March 6, 2014 8:17 PM
  • I honestly don't see why somebody in the script center here cannot take what I have and make it for me, but I will use more of my time up and do it myself.

    Be aware that this sounds like: "Other forum participants' time is far less valuable than mine."

    Bill


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 6, 2014 8:25 PM
    Moderator
  • It saves everybody time, if the person who already knows what they are doing does it in a short period of time and posts it for everyone to reference rather than the noob who is the one actually asking for the help to figuer it all out on his own in a long time consuming and rather frustrating period.

    Some see this as being nice. Polite. Perhaps even giving.

    I can clearly see where this forums principles are.

    Thursday, March 6, 2014 8:56 PM
  • Thanks Mike, I will have to learn more about these and make up my own script.

    I honestly don't see why somebody in the script center here cannot take what I have and make it for me, but I will use more of my time up and do it myself.

    Mike and Bill, you have been the most help here so far, but I just don't think I will be asking anymore question in this forum.

    HUGE WAIST OF TIME.

    So you are admitting that the sendkeys can't be made to work which is what we told you to begin with.  You were the one wasting time by refusing to accept what many people more experienced than you all agreed about.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 8:57 PM
  • It saves everybody time, if the person who already knows what they are doing does it in a short period of time and posts it for everyone to reference rather than the noob who is the one actually asking for the help to figuer it all out on his own in a long time consuming and rather frustrating period.

    Some see this as being nice. Polite. Perhaps even giving.

    I can clearly see where this forums principles are.

    The script you want someone to write can't be written.   You have all of the scripts that might work.  Yo have seen that none of them do what you want.  Why do you keep insisting that someone can do this for you? Help me understand why you think this can be done.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 9:01 PM
  • It can be written, and when I do it, I will post it here and maybe you can learn something.

    I never admitted to your above accusation, I simply do not believe that you people responding to my post in this one forum are in any way helpfull in making it happen.

    It is simply a matter of you not trying to add a Do While statement or something of the like into, my above code.

    Thursday, March 6, 2014 9:23 PM
  • Sorry you seem to be frustrated, but writing completed scripts for others to spec is not really the purpose of this forum. It is, at the very least, rather impolite to suggest that your time is more valuable and important than the other participants here. (I would suggest that perhaps this isn't the best way to ask for free help.)

    I would also note that a free peer-to-peer discussion forum, like this one, does not have a service-level agreement. If you need a higher level of support, then you will probably need to hire someone to help you.

    Bill


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 6, 2014 9:59 PM
    Moderator
  • It can be written, and when I do it, I will post it here and maybe you can learn something.

    I never admitted to your above accusation, I simply do not believe that you people responding to my post in this one forum are in any way helpfull in making it happen.

    It is simply a matter of you not trying to add a Do While statement or something of the like into, my above code.

    YOu keep asking for a loop for your code.  You already have a loop in your code.  It is right in the middle starting with the 'Do" and ending with the "Loop".

    Read this on loops and how to use them: http://technet.microsoft.com/library/ee692766.aspx

    Other posts have shown you various ways to use loops and code but you didn't like any of it.  Non of the suggestions worked the way wanted.

    We cannot read your mind.  If you know of some other way to do this then, by all means, post what you know.  Don't just demand a solution and stamp your feet.  That is not going to change reality.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 10:55 PM
  • It can be written, and when I do it, I will post it here and maybe you can learn something.

    As others have mentioned several times, you cannot reliably close an Outlook session that you have not started yourself. You can comply with this restriction by ensuring that you do start each Outlook session yourself. In other words, instead of launching outlook.exe directly, you launch it with your script. The script remains resident until some scheduled task creates a shutdown-semaphore file or until the user closes Outlook. The script checks once every 10 seconds if the semaphore file exists.

    If there is unsaved data the Quit method will not work. In this case the script uses taskkill.exe to shut down Outlook. This is ugly and could cause corruption in your .PST files.

    const olFolderInbox = 6
    Const sProfileName = "Webmaster"
    Const sSemaphore = "d:\CloseOutlook.txt"
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oWshShell = CreateObject("WScript.Shell")
    Set oOutlook = CreateObject("Outlook.Application")
    Set oNamespace = oOutlook.GetNamespace("MAPI")
    oNamespace.Logon sProfileName,, False, True    
    Set oFolder = oNamespace.GetDefaultFolder(olFolderInbox)
    oFolder.Display
    
    Do
        If Not OutlookIsRunning Then 
            if oFSO.FileExists(sSemaphore) then oFSO.DeleteFile(sSemaphore)
            WScript.quit
        End If
    
        If oFSO.FileExists(sSemaphore) Then 
            oOutlook.Session.Logoff
            oOutlook.Quit
            WScript.Sleep 3000
            If OutlookIsRunning _
            Then oWshShell.Run "taskkill.exe /f /im Outlook.exe", 0, False
            oFSO.DeleteFile(sSemaphore)
            WScript.quit
        End If
        WScript.Sleep 10000
    Loop
    
    Function OutlookIsRunning
        On Error Resume Next
        Set oAux = oOutlook.Session
        OutlookIsRunning = (Err.number = 0)
        On Error Goto 0
    End Function
    

    Friday, March 7, 2014 12:15 AM
  • Below is the scrip that I made to answer my own question:

    Set wshShell = CreateObject("WScript.Shell")
     ret = wshShell.AppActivate("Microsoft Outlook")
    Do Until ret = False
     ret = wshShell.AppActivate("Microsoft Outlook")
            wshShell.SendKeys "{Y}"
        WScript.Sleep 300
    Loop

    ________________________________________________________

    I am not sure if the second line is neccessary or not, but it works as is, so I am not changing it.

    My fix is that, I have two VBS files that run on a schedual one right after the other.

    First I run the script provided by Bill, simply because it is shorter than the script I had that was working.

    On Error Resume Next
    Set Outlook = GetObject(, "Outlook.Application")
    If Err = 0 Then
    Outlook.Quit()
    End If

     Then I run the script I made

    Set wshShell = CreateObject("WScript.Shell")
     ret = wshShell.AppActivate("Microsoft Outlook")
    Do Until ret = False
     ret = wshShell.AppActivate("Microsoft Outlook")
            wshShell.SendKeys "{Y}"
        WScript.Sleep 300
    Loop

    And Outlook is successfully closed with all data progress saved, and no Errors, or corruption.

    Congratulations on being the most complicated people to talk to and only having half an answer.

    • Marked as answer by Josh Ragan Friday, March 7, 2014 3:35 PM
    Friday, March 7, 2014 3:33 PM
  • Congratulations on being the most complicated people to talk to and only having half an answer.

    Feel better now?

    This is free forum where people spend their time trying to help others learn. We're not Microsoft employees, we don't get paid for this. We also don't do other people's work for them.

    The good result from all of this is that you learned something. I see you're breaking out of your loop now. Perhaps next time you have a problem you'll now be able to look at some documentation and figure out what to do about your issues rather than posting a plea in a forum and then getting mad when people won't do all of the heavy lifting for you.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Friday, March 7, 2014 3:42 PM
  • It is interesting that the same script you complained wouldn't work you now insist works.  That I what we are trying to tell you.  It will work sometimes and when it doesn't there is no fix.

    I am pretty sure we all knew you were going to come back and insist that your method worked as sonn as you could get it to ork once.

    There is a reason why so many people are trying to tell you that you are doingall of thi backwards.

    Setting up a central signature system on Exchange or through Office groups and sharing is far better than trying to kludge your way around the issue because you do notunderstand the technology.  Here is a chane to really learn something.  Ask "What is the best way to do this".  Instread you are saying "I want only my incvented solution to work no matter what anyone with more training might say"

    You can understand why people may get very frustrated.

    Good Luck and try to learn a bit about how Outlook and Office work in a corporate network.  It should never be that difficult.


    ¯\_(ツ)_/¯

    Friday, March 7, 2014 3:56 PM
  • Agreed. There is absolutely no way I would rely on a script that uses SendKeys to work consistently, particularly in a production environment. I highly recommend a supported way of configuring Outlook signatures rather than a SendKeys script.

    Bill


    -- Bill Stewart [Bill_Stewart]

    Friday, March 7, 2014 4:47 PM
    Moderator
  • It is not the exact same script. There are key differences that makes my answer work for me as apposed to what I had before.

    Also, my question and reason for the first post to get help creating a VBScript to end Outlook in any case, wether there be opened items with unsaved changes or not.

    I did not ask you for alternatives to configuring my signatures.

    You pressed me for that unneccessarily.

    You ignored the point of the initial post and came up with this waist of time talking about your oppinions.

    Regardless of your oppinions somewhat relation to my main post, they are offtopic.

    The only thing I see that related to it was Bills first post and Forest Brooks first post.

    Thank you for those two helpfull replys.

    Friday, March 7, 2014 6:51 PM