-
Oct 3rd, 2022, 05:54 PM
#1
Thread Starter
New Member
[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 ?
-
Oct 5th, 2022, 08:08 AM
#2
Re: Stop / Starting Outlook via VBScript
Is there a reason for not keeping the Outlook object alive?
-
Oct 5th, 2022, 08:44 AM
#3
Thread Starter
New Member
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 ?
-
Oct 5th, 2022, 08:51 AM
#4
Re: Stop / Starting Outlook via VBScript
Have you tried directly starting Outlook from the oShell.Run command?
-
Oct 5th, 2022, 08:52 AM
#5
Re: Stop / Starting Outlook via VBScript
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 5th, 2022, 10:35 AM
#6
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>
-
Oct 6th, 2022, 12:06 AM
#7
Thread Starter
New Member
Re: Stop / Starting Outlook via VBScript
Originally Posted by wqweto
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
-
Oct 6th, 2022, 04:41 AM
#8
Re: Stop / Starting Outlook via VBScript
Originally Posted by ksor
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.
-
Oct 6th, 2022, 04:56 AM
#9
Re: Stop / Starting Outlook via VBScript
Originally Posted by ksor
???????? - 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")?
Originally Posted by ksor
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>
-
Oct 6th, 2022, 08:52 AM
#10
Thread Starter
New Member
Re: Stop / Starting Outlook via VBScript
I don't think we are on the same frequence or some jamming is going on !
-
Oct 6th, 2022, 08:58 AM
#11
Thread Starter
New Member
Re: Stop / Starting Outlook via VBScript
Originally Posted by Arnoutdv
Have you tried directly starting Outlook from the oShell.Run command?
Ofcause ... and it works too - nice thx !
-
Oct 6th, 2022, 09:39 AM
#12
Re: Stop / Starting Outlook via VBScript
Originally Posted by ksor
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|