Outlook Automation


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Outlook Automation

  1. #1
    Join Date
    Aug 2004
    Posts
    92

    Outlook Automation

    I am new to sending e-mails through VB6. I am interested in both CDO and Outlook automation. However, this question pertains to Outlook.

    I use the following code to automate Outlook...
    Code:
    'Dimension objects for Outlook
    Dim objOutlookApp as Outlook.Application
    Dim objNameSpace as Outlook.NameSpace
    Dim objMail as Outlook.MailItem
    
    'Set th application objects
    Set objOutlookApp = Outlook.Application
    Set objNameSpace = objOutlookApp.GetNamespace("MAPI")
    
    'Login to Outlook
    objNameSpace.Login
    
    'Set properties for the e-mail message and send e-mail
    With objMail
           .To = "someone@microsoft.com"
           .Subject = "Message Subject"
           .Body = "This is the body of the e-mail message."
           .Send
    End With
    
    Set objMail = Nothing
    Set objNameSpace = Nothing
    Set objOutlookApp = Nothing
    My question is how can I include one or more attachements in the e-mail. I know the property to use in the above code is .Attachements, but I cannot find any reference regardng the format for this property to send one or more attachements.

    Any help would be appreciated.
    Last edited by Hack; 05-05-2009 at 07:27 AM. Reason: Added Code Tags
    Brian

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    You will need to tweak this to fit your application and code. I extracted this from one of my applications (the code it comes from is actually quite lengthy, but this is the part that pertains to attachments.)

    MailMsg is declared as Outlook.MailItem
    Code:
    Dim AttachmentArray() As String
    Dim MyAttachment As Long
    
    If Len(txtMsgAttachments.Text) > 0 Then  'then we have attachment(s).  deal with it
        AttachmentArray = Split(txtMsgAttachments.Text, ",")
        For MyAttachment = 0 To UBound(AttachmentArray)
            AttachmentArray(MyAttachment) = Trim$(AttachmentArray(MyAttachment))
            If Len(Dir$(AttachmentArray(MyAttachment))) > 0 Then
                Call MailMsg.Attachments.Add(AttachmentArray(MyAttachment), 1)
            End If
        Next
    End If
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Aug 2004
    Posts
    92
    Hack,

    Thanks for the code example. I'll give it a try.
    Brian

  4. #4
    Join Date
    Aug 2004
    Posts
    92
    Your suggestion worked well. However on one system it work okay, but on the other system, before the e-mail was sent I received the following security warning as a pop-up box apparently issued by Outlook...

    A program is trying to automatically send e-mail on your behalf. Do you want to allow this?

    If this is unexpected, it may be a virus and you should choose "No".


    Then I am given three choices "Yes," "No," and "Help."

    Is there some way of eliminating this warning box as my application will be sending dozens of e-mails and I don't want it to show this warning box before each e-mail is sent.

    Of note, this warning appears in Outlook 2003 but not in Outlook 2007.

    Any advice will be appreciated.
    Brian

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    That is a built in security feature that so many people complained about that Microsoft removed it in 2007.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #6
    Join Date
    Aug 2004
    Posts
    92
    That's nice!!! Is there any way to work around this warning? If they removed it from 2007, it's surprising that they didn't offer a patch for 2003.
    Brian

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    There is no real clean way around it (hence everyone complaining)

    Here is one possible alternative...

    http://www.vbforums.com/showthread.php?t=429381
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  8. #8
    Join Date
    Aug 2004
    Posts
    92
    I appreciate the assistance given earlier. However since I am developing my application on two different systems each with a different version of Outlook, I would prefer to use late binding instead of early binding.

    My code for early binding which works well is...

    Dim objOutlookApp As Outlook.Application
    Dim objNameSpace As Outlook.NameSpace
    Dim objMail As Outlook.MailItem

    'Set the application objects
    Set objOutlookApp = Outlook.Application
    Set objNameSpace = objOutlookApp.GetNamespace("MAPI")

    'Login to Outlook
    objNameSpace.Logon

    'Set the mail item
    Set objMail = objOutlookApp.CreateItem(olMailItem)

    'Set properties for the mail item and send mail
    With objMail
    .To = "someone@xyz.com"
    .Subject = "Message Sent from Visual Basic"
    .Body = "This message was created by automating Outlook from VB!"
    For i = 1 To 7
    Call .Attachments.Add(FileArray(i), 1)
    Next i
    .Send
    End With

    Set objMail = Nothing
    Set objNameSpace = Nothing
    Set objOutlookApp = Nothing

    However, I am having difficulty with the the code for late binding which is...

    Dim objOutlookApp As Object
    Dim objNameSpace As Object
    Dim objMail As Object

    'Set the application objects
    Set objOutlookApp = CreateObject("Outlook.Application")
    Set objNameSpace = objOutlookApp.GetNamespace("MAPI")

    'Login to Outlook
    objNameSpace.Logon

    'Set the mail item
    Set objMail = objOutlookApp.CreateItem(olMailItem) 'PROBLEM WITH SYNTAX

    'Set properties for the mail item and send mail
    With objMail
    .To = "someone@xyz.com"
    .Subject = "Message Sent from Visual Basic"
    .Body = "This message was created by automating Outlook from VB!"
    For i = 1 To 7
    Call .Attachments.Add(FileArray(i), 1)
    Next i
    .Send
    End With

    Set objMail = Nothing
    Set objNameSpace = Nothing
    Set objOutlookApp = Nothing


    I am having difficulting with the syntax used to set the objMail object. Any help with the code necessary for late binding would be appreciated.

    Thanks
    Brian

  9. #9
    Join Date
    Aug 2004
    Posts
    92
    I found the answer and thought I would post it for other...

    Set objMail = objOutlookApp.CreateItem(0)
    Brian

  10. #10
    Join Date
    Aug 2004
    Posts
    92
    With regards to the security alert in Outlook 2003, I found the same Alert in Outlook 2007 if the Anti-Virus program is not up-to-date. When I updated my anti-virus, no warnings appeared.
    Brian

Similar Threads

  1. Replies: 4
    Last Post: 02-20-2007, 08:09 AM
  2. Replies: 0
    Last Post: 02-13-2007, 01:54 PM
  3. Access to Outlook automation
    By looper8 in forum VB Classic
    Replies: 0
    Last Post: 12-19-2005, 08:24 AM
  4. Re: Using VB forms in Outlook
    By Brian Weiss in forum Enterprise
    Replies: 1
    Last Post: 04-05-2000, 08:34 PM
  5. Programming Custom Views in Outlook
    By Brian Weiss in forum Enterprise
    Replies: 0
    Last Post: 03-31-2000, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center