Re-write code writen for Outlook 2003 to work for Outlook 2002

Can someone help me with this code? I need Outlook Object Library 11.0 in order for this code to work in Access 2002 and outlook 2002. Can i re-write the code so that it works in 2002? It works fine in 2003 because Access has the 11.0 object library in it....thanks!

Function send_email_Click()
Dim ol As New Outlook.Application
Dim olMail As Outlook.MailItem
Dim strCC As String
Dim rst As New ADODB.Recordset
Dim strNames As String


rst.Open "Email_accepted", CurrentProject.Connection

Do Until rst.EOF
  strNames = strNames & rst("email") & ";"
  rst.MoveNext
Loop
 
    Set olMail = ol.CreateItem(olMailItem)

    With olMail
        .To = "me@me.com"
        '.CC = strCC
         .BCC = strNames
        '.Attachments.Add "c:\somefile.txt"
        '.Attachments.Add "c:\somefile2.txt"
        .Subject = "Sanibel Island"
        .HTMLBody = ReadTextFileContents("C:\Sanibel\condos.html")
       
        .Display
        '.Send
    End With

    Set olMail = Nothing
    Set ol = Nothing

End Function
coolcat139Asked:
Who is Participating?
 
DabasConnect With a Mentor Commented:
Dim
Function send_email_Click()
'Dim ol As New Outlook.Application
Dim olMail As Object 'Note this one becomes an object too
Dim strCC As String
Dim rst As New ADODB.Recordset
Dim strNames As String
Dim strMsg As String
Dim ol As Object
Set ol = CreateObject("Outlook.Application")
Set olMail = ol.CreateItem(olMailItem) 'Do not use CreateObject, as in the original it was not New, but created from ol
0
 
DabasCommented:
Hi coolcat139:
Unfortunately the best way to solve this problem is to cause your binding to happen "Late" instead of "Early"
Once you have tested your application in 2003, do the following:

1) Remove the 11.00 object library from your references
2) Change all of your Dim statements that use the library to be variants.
Thus, instead of

Dim ol as New Outlook.Application

use

Dim ol as Object

In your code, add the following instead:

Set ol = CreateObject("Outlook.Application")

What actually happens is that your executable compiles without really knowing what kind of object your ol variable is.
When you run it in your environment, and it gets to the CreateObject line, it will find your version 11 Outlook object and will LATE Bind it at run time.
When a 2002 user runs your program, it will find the version 10 Outlook object (or earlier!), and as long as your code is not using any features that are new to version 11, the code should work OK

Dabas
0
 
David LeeCommented:
I don't think the Outlook syntax changed at all from Outlook 2002 to 2003.  So this code should work just fine with the Outlook Object Library for 2002.  Are you saying that it doesn't work if you use the Outlook 10 Object Library?  If so, where does it fail?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
coolcat139Author Commented:
Thats right - when I run this code in a 2002 environment it chokes here:

Dim ol As New Outlook.Application

error says:  
Compile Error:
User defined type not defined.

I too thought there were not any changes, but apparently, I need to get 11.0 or do what Dabas recommends..

what do you guys think?
0
 
DabasCommented:
coolcat139:
If you have VB in the 2002 environment, all you need to do is to go to references and change the reference from version 11 to version 10, then you should be able to compile OK.
If you want to run the same executable in both environments, (maybe on a computer that does not ahve vb installed) then you will have to go with my previous suggestion.
Alternatively, you could have a version 11 executable, compiled on  a 2003 machine, and a version 10 executable compiled on a 2002 machine. If you need to do changes in the future, this might become a nightmare!


Dabas
0
 
coolcat139Author Commented:
"all you need to do is to go to references and change the reference from version 11 to version 10, then you should be able to compile OK.
"

I did that, and it did not seem to work...

I will try your other suggestion
0
 
coolcat139Author Commented:
This is not working......

Dim ol as New Outlook.Application

use

Dim ol as Object

In your code, add the following instead:

Set ol = CreateObject("Outlook.Application")

Could you possibly provide an example??
0
 
DabasCommented:
I did!

Can you paste the code as changed by you, outlining where it produced an error?

Dabas
0
 
coolcat139Author Commented:
sure hold on..
0
 
coolcat139Author Commented:
Function send_email_Click()
'Dim ol As New Outlook.Application
'Dim olMail As Outlook.MailItem
Dim strCC As String
Dim rst As New ADODB.Recordset
Dim strNames As String
Dim strMsg As String

Dim ol As Object
Set ol = CreateObject("Outlook.Application")
Dim olMail As Object
Set olMail = CreateObject("Outlook.MailItem") <<<<< error is here - "Active-x component can't create object"
0
 
DabasCommented:
coolcat:

Use CreateObject only when in your original program you used New (As with ol)

For olMail, you should be able to keep your old code:

    Set olMail = ol.CreateItem(olMailItem)
0
 
coolcat139Author Commented:
Trying it......
0
 
coolcat139Author Commented:
Nope, I get the "user type not defined" error on


Dim olMail As Outlook.MailItem

Code looks like this now:

Function send_email_Click()
'Dim ol As New Outlook.Application
Dim olMail As Outlook.MailItem
Dim strCC As String
Dim rst As New ADODB.Recordset
Dim strNames As String
Dim strMsg As String
Dim ol As Object
Set ol = CreateObject("Outlook.Application")
0
All Courses

From novice to tech pro — start learning today.