GetObject - CreateObject - I Object

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,124
Right - this is doing my head in.

Please see the below code - the aim of which is to set up the required properties in order to send an email:


Dim aOutlook As Outlook.Application, aEmail As Outlook.MailItem

Set aOutlook = GetObject(, "Outlook.Application")

If aOutlook Is Nothing Then Set aOutlook = New Outlook.Application

Set aEmail = aOutlook.CreateItem(olmailitem)


Now the above is all hunky dory when I have Outlook open. However, if I close Outlook and then try to run it I get an error - surely the If aOutlook Is Nothing line should create the Outlook Application? But my syntax fails on the previous line ("ActiveX Components can't create object")...

Dan came up with the CreateObject syntax so I tried:

Set aOutlook = CreateObject("Outlook.Application")

which gets past until I try to create the mail item:

Set aEmail = aOutlook.CreateItem(olmailitem)

which then gives me the following error:

"Internal Application Error" which implies to me that Outlook has not been created?

Can someone explain what's going on - I am clueless on this stuff.

Or better still - can someone give me some code that will check to see if Outlook is open if not create outlook - prior to me trying to set my Dims etc and creating the email.

Thanks
LASW10

The smile is wearing thin...
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hello,

You don't need to bother with GetObject when you're working with Outlook. It can only exist once so you can just use CreateObject. If it's already running, then it will just return a reference to the running application.

Anyway, does this code work?

Code:
Sub test()
Dim oApp As Object
Dim oMailItem As Object

Set oApp = CreateObject("Outlook.Application")
Set oMailItem = oApp.CreateItem(0)

oMailItem.Display

End Sub
 
Upvote 0
Didn't answer your question did I? :)

GetObject will error if the application isn't running. You need to check for this e.g.

On Error Resume Next
Set aOutlook = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then Set aOutlook = New Outlook.Application


However, like I said above, just use CreateObject instead of farting around with GetObject.
 
Upvote 0
Thanks for this Dan...

Using your test() code, with Outlook closed I still get the Internal Application Error on

Set oMailItem = oApp.CreateItem(0)

does this mean that Outlook Appl. has not been created?

Any ideas?

Thanks again,
Luke
 
Upvote 0
Hello mate,

Which version of Excel/Outlook are you using?

I just tried the code on my system (Excel 2000, Outlook 2002) and it worked fine.

Try this and see what happens...

Code:
Sub Test2()
Dim oApp As Object
Dim oMailItem As Object

Set oApp = CreateObject("Outlook.Application")

MsgBox oApp.Version
End Sub
 
Upvote 0
OK,

So did the above code work? Did it show a message box with the version? If so, then the code is definitely creating the Outlook application.

If you run this code, does it fail on the CreateItem line? If so, post the exact error message you get.

Code:
Sub Test3()
Dim oApp As Object
Dim oMailItem As Object

Set oApp = CreateObject("Outlook.Application")

Set oMailItem = oApp.CreateItem(0)
oMailItem.Display

End Sub



_________________<font face="Impact">Hope this helps,
Dan</font>
This message was edited by dk on 2002-10-31 04:42
 
Upvote 0
Yes Dan it does - I was trying to say that with the CreateObject it fails with an internal application error when trying to create the mail item (which implied to me that Outlook hadn't launched properly)

The error is:

'-2147287037 (80030003)':

Internal Application Error


I had a quick look trying to find info on this runtime but the only thing I could find implied that perhaps one of my .dll's was not registered properly - re-installed it but to no avail.

Any help appreciated - don't like not knowing why something won't work :(

Thanks
Luke
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,442
Members
448,898
Latest member
drewmorgan128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top