Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook and RunAs

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Hi

I have a (rather groovy) systems checks script which we run every morning and every evening. This script collates the results into an email using
Code:
Set objEmailClient = CreateObject("Outlook.Application")
Following a review of security we decided to remove Domain Admin rights from our normal accounts and create secondary admin accounts with which we run things like AD Users And Computers using the RunAs command.

That's all good EXCEPT that now the checking script fails when run under our normal accounts because of lack of permissions to the resources/services being queried.

SO, we just run the script using RunAs under the context of our admin account.

This would be fine EXCEPT that the
Code:
Set objEmailClient = CreateObject("Outlook.Application")
line fails because it's already running.

There's got to be an easy way to either
1) Open a second instance of Outlook under the Admin account or
2) better still, use the existing instance of Outlook.

Is there?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
I should add, the script also initiates the following object that are associated with the original objEmailClient object.

Code:
Set objChecksEmail = objEmailClient.CreateItem(0)
Set objNameSpace = objEmailClient.GetNameSpace("MAPI")
Set objGAL = objNameSpace.AddressLists("Global Address List")
...and probably one or two more besides.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
What about GetObject instead of CreateObject ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Never heard of it, but will investigate. Ta.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
OK, so I've edited the script as follows:
Code:
Set objEmailClient          = GetObject(,"Outlook.Application")
wscript.echo " Trying to use existing Outlook session (if it's running)..."
If Err.Number <> 0 Then
	wscript.echo "  Err = '" & Err.Number & "'" & vbCrLf &_
                 "  Err.Source       = '" & Err.Source & "'" & vbCrLf &_
                 "  Err.Description  = '" & Err.Description & "'"
	err.clear
	wscript.echo " Outlook doesn't seem to be running so creating new instance of application instead..."
	Set objEmailClient      = CreateObject("Outlook.Application")
	If Err.Number <> 0 Then
		wscript.echo "  Err = '" & Err.Number & "'" & vbCrLf &_
                     "  Err.Source       = '" & Err.Source & "'" & vbCrLf &_
                     "  Err.Description  = '" & Err.Description & "'"
		On Error GoTo 0
		Msg = "Failed to instantiate Outlook." & vbCrLf & vbCrLf &_
			  "Please ensure there isn't a failed OUTLOOK.EXE service running," & vbCrLf &_
			  "then re-run this script."
		MsgBox Msg,vbExclamation,strAppDescription & " - Can't open/start Outlook"
		wscript.quit
	End If
End If
On Error GoTo 0

And that works when run under the context of my normal account, with Outlook running, or with Outlook not running.

But, when I run the script using RunAs it errors as follows:
Trying to use existing Outlook session (if it's running)...
Err = '429'
Err.Source = 'Microsoft VBScript runtime error'
Err.Description = 'ActiveX component can't create object'
Outlook doesn't seem to be running so creating new instance of application instead...
Err = '429'
Err.Source = 'Microsoft VBScript runtime error'
Err.Description = 'ActiveX component can't create object'
Is this just a permissions/Outlook profile issue or do I need to correct the code at all?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
This is so frustrating. Does anyone have any ideas how to get this to work?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top