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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing outlook using VB? 2

Status
Not open for further replies.

Agent009

Programmer
Apr 4, 2003
96
IE
Hi all,

I use this code to attach a document to a new mail in outlook.

This works fine with Outlook 2000, but on other versions it wont work for some reason.

How do i change the code below so that i doesnt matter what Outlook version i run it on?

Set objOutlookmsg = objOutlook.CreateItem(olMailItem)
objOutlookmsg.Attachments.Add sPath
objOutlookmsg.Subject = "Image Attached"
objOutlookmsg.Display
Set objOutlook = Nothing

Thanks in advance,

Agent009
 
How did you create the objOutlook object? By setting a reference to it (compile time) or by using GetObject/CreateObject (run time). If using the former make sure to set the correct reference i.e. Office 10. In the latter case you should get the library corresponding to the office system installed.

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
The program wont open up a mail in Outlook 97. I have 2000.

The only thing i have done with regards to setting references to outlook is in

Project -> References -> and then i have selected "Microsoft Outlook 9.0 Object Library"

I did not create the "objOutlook" object. This is available after you select the above reference.

I dont know exactly what or where the error occurs. I have just been told that there is an object that i must change in order for Outlook 97 to be able to open a mail.

I have Outlook 2000 on my machine, so it worked fine, it also works on other machines with Outlook 2000 on them, but if i try to open a mail using this application with Outlook 97, an error occurs!

thanks,

Agent009.
 
You have two choices:

1. Use two development machines so that both versions of Outlook are available and compile separate versions of your app. For OL97 systems you would set a reference to Microsoft Outlook 8.0 Object Library.

2. Remove the reference to Outlook and use late binding. There is a performance hit but it isn't likely to be significant in your case. Use the CreateObject function to instantiate the Outlook object:

Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

This code will work with all versions of Outlook so you only need to compile one version of the app.

Paul Bent
Northwind IT Systems
 
Hey rvBasic,

so instead of the above code i should create the object rather reference to it in the Project -> references directory, because it will then know the difference between the different versions of Outlook!

Could you give me an example or something as i am relatively new to VB,

Thanks in advance,

Agent009,
 
Sorry paulbent,

Just reading you post now. I'll try it immediately!

Agent009,
 
This is the code i have entered and i have removed the referecnes to Outlook 9.0 in the Project -> References menu

-----------------------------------------
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

Dim objOutlookmsg As Object

Dim strSQL As String
Dim rs_img As New ADODB.Recordset
Dim shell_str As String
Dim ret_val
Dim sPath As String
Dim sNewPath As String

SendMail = False

If lvwMain.SelectedItem Is Nothing Then
SendMail = True
GoTo SendMail_Handle_Exit
End If

If lvwMain.SelectedItem.Text <> &quot;&quot; Then

MsgBox (&quot;Opening a new e:mail and attaching the selected image&quot;)

strSQL = &quot;SELECT DocumentPath From [Image] WHERE (((Image.DocumentID)=&quot; + lvwMain.SelectedItem.Text + &quot;));&quot;

Set rs_img = m_Conn.Execute(strSQL)
sPath = rs_img.Fields(&quot;DocumentPath&quot;)

'Create a message.
Set objOutlookmsg = CreateObject(&quot;Outlook.MailItem&quot;) - At this line i get this error.
&quot;Active X component cant create object&quot;
objOutlookmsg.Attachments.Add sPath
objOutlookmsg.Subject = &quot;POD Image Attached&quot;
objOutlookmsg.Display
Set objOutlook = Nothing

End If
-----------------------------------------

Please send me the solution if you can!

Thanks, in advance,

Agent009
 

I don't think you can do this with earlier versions of OutLook...
 
I have Outlook 2000 but i have been told that it can be done this way for earlier versions such as Outlook 97!

Could you have a look for me anyway?

Cheers!
 

I believe this Object Library first came with Outlook 2000.
You cannot distribute it, I believe, and Outlook 2000 must be on the machine.
I may be wrong though...
 
Hi,

I am not using any references to the outlook library

I want to create an Outlook object,(in the code above)

so that it can run on Outlook 97.

I am nearly there, i just keep on getting an error(see code above)

thanks anyway,
 

>I am not using any references to the outlook library

>I want to create an Outlook object,(in the code above)

Basically, the same.

The error you get looks like the Object Library is not on the client...
 

Are you sure the client has Outlook 97, or is it Outlook Express?
 
Once you have a reference to the OL app object, use its CreateItem method to create an Outlook item. The method is supported by 97, 98, 2000 & 2002:

Set objOutlookmsg = objOutlook.CreateItem(0) 'olMailItem

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top