Solved

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

Posted on 2004-08-13
13
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 11797815
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
ID: 11797819
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
ID: 11798057
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Dabas
ID: 11798076
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
ID: 11798107
"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
ID: 11798135
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
 
LVL 27

Expert Comment

by:Dabas
ID: 11798146
I did!

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

Dabas
0
 

Author Comment

by:coolcat139
ID: 11798157
sure hold on..
0
 

Author Comment

by:coolcat139
ID: 11798165
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
ID: 11798173
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
ID: 11798315
Trying it......
0
 

Author Comment

by:coolcat139
ID: 11798320
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
ID: 11798657
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

695 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