Results 1 to 12 of 12

Thread: [RESOLVED] Stop / Starting Outlook via VBScript

  1. #1

    Thread Starter
    New Member ksor's Avatar
    Join Date
    Oct 2022
    Posts
    13

    Resolved [RESOLVED] Stop / Starting Outlook via VBScript

    I can STOP Outlook nicely, but why is it so damn hard to get it up running again !

    This snip of code says Outlook is already in use .... and it is NOT - I've seen it stop by stepping through the code !

    Code:
    Set objOutlook = CreateObject("Outlook.Application")
    objOutlook.Quit
    .
    (some more code for copying !)
    .
    .
    .
    
    Msgbox "Start Outlook again !"
    OpenOutlook
     
    
    Sub OpenOutlook()
    
      Dim oApp
      Dim oName
      Dim oFolder
    
      Set oApp = CreateObject("Outlook.Application") 
      Set oName = oApp.GetNamespace("MAPI")
    '  OName.Logon "Default Outlook Profile",, False, True
    '  Set oFolder = oName.GetDefaultFolder(6)
    '  oFolder.Display
      OApp.ActiveExplorer.Activate
    
      Set oApp = Nothing
      Set oName = Nothing
      Set oFolder = Nothing
    
    End Sub
    What do I do wrong ?

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Stop / Starting Outlook via VBScript

    Is there a reason for not keeping the Outlook object alive?

  3. #3

    Thread Starter
    New Member ksor's Avatar
    Join Date
    Oct 2022
    Posts
    13

    Re: Stop / Starting Outlook via VBScript

    I want to COPY the Outlook data file and to do that I am normaly closing Outlook ... but I've build another more simple Sub for starting Outlook and it WORKS ...

    except

    it leaves a CMD-window open and that's not good !

    The sub code is:

    Code:
    Sub StartOutlook()
        Dim oShell
        set oshell = CreateObject("Wscript.Shell") 
        oShell.run "cmd.exe /K ""C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"" "
    End Sub
    I thought the /K should close the CMD after running the command - but it don't !

    Why ?

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Stop / Starting Outlook via VBScript

    Have you tried directly starting Outlook from the oShell.Run command?

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Stop / Starting Outlook via VBScript

    cmd.exe /C ??
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Stop / Starting Outlook via VBScript

    Does cmd.exe /c notepad.exe close notepad?

    Does cmd.exe /k notepad.exe close it too?

    Why would it close Outlook?

    Anyway, try this snippet

    Code:
    On Error Resume Next 
    Set oApp = GetObject(, "Outlook.Application")
    If oApp Is Nothing Then
        Set oApp = CreateObject("Outlook.Application")
        If oApp Is Nothing Then
            Wscript.echo "Failed"
        Else
            Wscript.echo "Created new instance"
        End If
    Else
        Wscript.echo "Retrieved current running instance"
    End If
    Note that you *have to* run this script (and Outlook) non-elevated. Running your script elevated can be problematic and will most probably fail retrieving/starting Outlook.

    cheers,
    </wqw>

  7. #7

    Thread Starter
    New Member ksor's Avatar
    Join Date
    Oct 2022
    Posts
    13

    Re: Stop / Starting Outlook via VBScript

    Quote Originally Posted by wqweto View Post
    Does cmd.exe /c notepad.exe close notepad?

    Does cmd.exe /k notepad.exe close it too?

    Why would it close Outlook?

    Anyway, try this snippet

    Code:
    On Error Resume Next 
    Set oApp = GetObject(, "Outlook.Application")
    If oApp Is Nothing Then
        Set oApp = CreateObject("Outlook.Application")
        If oApp Is Nothing Then
            Wscript.echo "Failed"
        Else
            Wscript.echo "Created new instance"
        End If
    Else
        Wscript.echo "Retrieved current running instance"
    End If
    Note that you *have to* run this script (and Outlook) non-elevated. Running your script elevated can be problematic and will most probably fail retrieving/starting Outlook.

    cheers,
    </wqw>
    ???????? - I don't get you !

    Why do you think your samples would close OUTLOOK ... Outlook is not even mentioned there !

    The code snip I showed - does in fact start Outlook but it DON'T close the CMD-window - that's my problem !

    Code:
    Sub StartOutlook()
        Dim oShell
        set oshell = CreateObject("Wscript.Shell") 
        oShell.run "cmd.exe /K ""C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"" "
    End Sub

  8. #8
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: Stop / Starting Outlook via VBScript

    Quote Originally Posted by ksor View Post

    The code snip I showed - does in fact start Outlook but it DON'T close the CMD-window - that's my problem !
    HELP; You need to be more specific in your post titles and in the initial description of your problem. Be terse but precise and get straight to the point. Too many distracting words only confuse.

    The last line should summarise the help you require as a statement/question.

    You'll get it in the end and it will assist you immensely getting help here and elsewhere.

    Top tip: Write it out beforehand somewhere else and be sure it makes sense before posting.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Stop / Starting Outlook via VBScript

    Quote Originally Posted by ksor View Post
    ???????? - I don't get you !

    Why do you think your samples would close OUTLOOK ... Outlook is not even mentioned there !
    Where is Outlook not mentioned? In GetObject(, "Outlook.Application") or in CreateObject("Outlook.Application")?

    Quote Originally Posted by ksor View Post
    The code snip I showed - does in fact start Outlook but it DON'T close the CMD-window - that's my problem !
    You cannot close Outlook because you *start* Outlook incorrectly.

    No, you cannot use Shell.Run to start Outlook, you have to use GetObject/CreateObject and when finished dealing with whatever you are doing w/ it just call Quit method.

    You can even fine tune it to call Quit only if you started Outlook with CreateObject so not to infuriate the end-user by closing their actively running Outlook you snatched w/ GetObject.

    Edit: Your topic is "Stop / Starting Outlook via VBScript" so you *are* actually asking about starting Outlook though obviously stopping is more problematic when using Shell.Run

    JFYI, I'm unsubscribing from this topic.

    cheers,
    </wqw>

  10. #10

    Thread Starter
    New Member ksor's Avatar
    Join Date
    Oct 2022
    Posts
    13

    Re: Stop / Starting Outlook via VBScript



    I don't think we are on the same frequence or some jamming is going on !

  11. #11

    Thread Starter
    New Member ksor's Avatar
    Join Date
    Oct 2022
    Posts
    13

    Re: Stop / Starting Outlook via VBScript

    Quote Originally Posted by Arnoutdv View Post
    Have you tried directly starting Outlook from the oShell.Run command?
    Ofcause ... and it works too - nice thx !

  12. #12
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Stop / Starting Outlook via VBScript

    Quote Originally Posted by ksor View Post
    Ofcause ... and it works too - nice thx !
    This is wrong on so many levels. . . just don't!

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width