Access VBA and Outlook automation: Problem with CC Recipients.type

Answered Access VBA and Outlook automation: Problem with CC Recipients.type

  • Wednesday, April 06, 2011 3:51 PM
     
     

    Environment: Outlook, Access Version 14.0

    Hi,

    I followed article http://support.microsoft.com/kb/161088/en

    to create mailitem's using Access VBA and Outlook automation.

     

    Here's my code snipset:

     

    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    With objOutlookMsg

        ...

        Set objOutlookRecip = .Recipients.Add(FormatFaxNummer(rstKontakt!kontakttelefax))
        objOutlookRecip.Type = olTo

       Set lvMA_CC = Me!ListViewCC.Object
       For Each lvItemCC In lvMA_CC.ListItems
            Set objOutlookRecip = .Recipients.Add(FormatFaxNummer(lvItemCC.SubItems(2)))
            objOutlookRecip.Type = olCC
            Set objOutlookRecip = Nothing
       Next

     

       .Display                                           

    End with

     

    When the message gets displayed I noticed that the message CC line is empty and

    all the Recipients who supposed to appear in the CC line are in the messages To-Line.

     

    Any suggestions are greatfully appreciated.

    Thanks

    Bodo

     

     

All Replies

  • Thursday, April 07, 2011 2:47 AM
     
     

    You have ot put your set to nothing outside the FOR loop

     

       For Each lvItemCC In lvMA_CC.ListItems
            Set objOutlookRecip = .Recipients.Add(FormatFaxNummer(lvItemCC.SubItems(2)))
            objOutlookRecip.Type = olCC
       Next

       Set objOutlookRecip = Nothing


    jdweng
  • Thursday, April 07, 2011 11:58 PM
     
     
    I don't think the location of setting the object to nothing should matter. It's after the code is finished with the outlook recipient. The only thing I could suggest is you might need to assign the recipient type either while adding the recipient to the recipient collection or before, and not after. I believe the Recipients.Add function has an optional argument for the recipient type, so you could specify it there.
  • Friday, April 08, 2011 12:22 AM
     
     Answered

    Environment: Outlook, Access Version 14.0

    Hi,

    I followed article http://support.microsoft.com/kb/161088/en

    to create mailitem's using Access VBA and Outlook automation.

     Here's my code snipset:

     Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    With objOutlookMsg

        ...

        Set objOutlookRecip = .Recipients.Add(FormatFaxNummer(rstKontakt!kontakttelefax))
        objOutlookRecip.Type = olTo

       Set lvMA_CC = Me!ListViewCC.Object
       For Each lvItemCC In lvMA_CC.ListItems
            Set objOutlookRecip = .Recipients.Add(FormatFaxNummer(lvItemCC.SubItems(2)))
            objOutlookRecip.Type = olCC
            Set objOutlookRecip = Nothing
       Next

     

       .Display                                           

    End with

     

    When the message gets displayed I noticed that the message CC line is empty and

    all the Recipients who supposed to appear in the CC line are in the messages To-Line.

     

    Do you have a reference set to the Microsoft Outlook Object Library?  If you don't, I imagine that the constant olCC would not be defined, and your assignment of that constant to objOutlookRecip.Type will fail, and the recipient type will remain set to the default. The failure *ought* to raise error 94, "Invalid use of Null", but maybe you have normal error-handling turned off.

    Does it come out correctly if you replace the line:

        objOutlookRecip.Type = olCC

    with:

        objOutlookRecip.Type = 2

    ?

    If you are using late binding, you can declare the olCC constant as

        Const olCC As Integer = 2

     


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
  • Friday, April 06, 2012 1:07 PM
     
     Proposed Answer

    I have been struggling with sending reports as pdf's via access  2007 for a day or so now and finally think I have cracked the problem which was suggested by others saying that  their routines only worked if Outllook was up and running beforehand  - I was having troubling with inserting a recipient olto -

    By inserting the .display immediately after 'With objOutlookMsg' everything now works just fine!!!

    ie

    With objOutlookMsg

          .display

    the window opens in the background and closes immediately after

    .send

    you can REM out the send and check the email if you prefer then manually send - after which it self closes

    Had I known that I would have saved myself 2 days of muckin' abahht!

    I notice I have no return address in the email and am currently looking to alter that - one can of course simply put it in as part of .body

    db


    • Edited by davidbailie Friday, April 06, 2012 1:08 PM
    • Proposed As Answer by davidbailie Friday, April 06, 2012 1:21 PM
    •