Solved

Outlook automation

Posted on 2014-01-16
6
370 Views
Last Modified: 2014-01-26
Hi All,
I usually use redemption for this purpose but in this case it isn't an option.
I'm using late binding for compatibility.
When the code below runs on a non-Exchange platform it works great. It fails on the last line of the code below when using Exchange. (trying to add a recipient prior to resolving (for the purpose of add CCs))
The recipient is unknown to the system at this stage (not in contacts etc)
The error is Error 287, Application-defined or object-defined error
Does one use different libraries/code when using Outlook/Exchange?
thanks

Function SendEmail(RecipEmail As String, Subject As String, BBody As String, cc1 As String) As Boolean
  On Error GoTo err_SendEmail
  Dim olApp As Object
  Dim olNS As Object
  Dim olRecip As Object
  Dim oItem As Object
'  Set olApp = CreateObject("Outlook.Application")
'  Set olNS = CreateObject("Outlook.Namespace")
  Dim tInterval As Integer, signature As String
  Dim i, MyFile, MyFile2, SentItemsCounter, CoverLetter, tmpEmail, ErrorLine As Long
  If RecipEmail <> "" Then
    SentItemsCounter = 0
    SendEmail = False
    Set olApp = CreateObject("Outlook.Application")
    Set olNS = olApp.GetNamespace("MAPI")
    Set olRecip = olApp.CreateItem(olRecipient)
    olNS.Logon
    Set oItem = olApp.CreateItem(0) 'Create a new message
    Set olRecip = oItem.Recipients.Add(RecipEmail)
0
Comment
Question by:COACHMAN99
[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
  • 3
  • 3
6 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39787263
Not really an answer but perhaps it will help:

When you do this:
Dim i, MyFile, MyFile2, SentItemsCounter, CoverLetter, tmpEmail, ErrorLine As Long
...only "ErrorLine " will actually be declared as a Long datatype, ...all of the preceding variable will be "Variant" datatypes

As variants the code takes longer to "Resolve" these variables, ...possibly causing other issues as well.

See here: http://www.cpearson.com/excel/DeclaringVariables.aspx

JeffCoachman
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 39787380
Thanks Jeff. (any feedback/comment on this is welcome). In this case most of those vars aren't used (copied code form elsewhere) but I fixed it anyway (and no improvement :-().
The fact that this code works 100% of the time in a situation where Exchange isn't involved, and fails 100% of the time when it is is a pointer to the problem.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39787761
oK, so lets wait for an Outlook expert to chime in.

you can also add the Exchange zone to this Q.
0
Create Professional Looking Email Signatures

Create "Professional HTML Email Signatures" with ease.
7 Day Money Back Guarantee if not 100% Satisfied.
Affordable - Try it out for 7 Days Totally Risk Free.
Installers provided for over 45 Email clients.
Both Windows & MAC Supported.
Highly Recommended!

 
LVL 7

Accepted Solution

by:
COACHMAN99 earned 0 total points
ID: 39797948
Apparently (when the Outlook security options (Trust Center Programmatic Access) are set, and a valid anti-virus application exists) the standard security messages are NOT displayed but parts of the object model are disabled. e.g. the following code fails on 'signature = .Body', not '.to'. If the offending line is removed all is well.

Set olApp = CreateObject("Outlook.Application")
   Set oItem = olApp.CreateItem(olMailItem)
   With oItem
       .To = RecipEmail
       .Display   'to get signature for later use
       signature = .Body
       .Subject = Subject
       .Body = BBody & vbCr & vbCr & signature
   End With
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39798501
OK,

Great,

you should now accept your own post as the solution so others can benefit.

;-)

JeffCoachman
0
 
LVL 7

Author Closing Comment

by:COACHMAN99
ID: 39809852
Apparently (when the Outlook security options (Trust Center Programmatic Access) are set, and a valid anti-virus application exists) the standard security messages are NOT displayed but parts of the object model are disabled. e.g. the following code fails on 'signature = .Body', not '.to'. If the offending line is removed all is well.
0

Featured Post

Increase your protection from Zero Day threats!

Running two Antivirus' is never a good idea.
Taking advantage of Multiple Security layers on the other hand can often save your hide.
See which top notch security software brands have been proven to happily coexist together.
Reduce your chances of becoming a statistic.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

739 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