Solved

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

Posted on 2004-08-13
13
163 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:coolcat139
  • 7
  • 5
13 Comments
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
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
 

Author Comment

by:coolcat139
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 

Author Comment

by:coolcat139
Comment Utility
"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
 

Author Comment

by:coolcat139
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:Dabas
Comment Utility
I did!

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

Dabas
0
 

Author Comment

by:coolcat139
Comment Utility
sure hold on..
0
 

Author Comment

by:coolcat139
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 

Author Comment

by:coolcat139
Comment Utility
Trying it......
0
 

Author Comment

by:coolcat139
Comment Utility
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
 
LVL 27

Accepted Solution

by:
Dabas earned 250 total points
Comment Utility
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now