?
Solved

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

Posted on 2004-08-13
13
Medium Priority
?
184 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

650 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