Results 1 to 10 of 10

Thread: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

  1. #1

    Thread Starter
    New Member olivier.gies's Avatar
    Join Date
    Jan 2005
    Posts
    4

    Question Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    Hi everybody,

    I'm very new to VB programming within Office Apps (though I have heavy Java/C/C++ background).

    I want to create a macro that will process a stored sent mail (i.e. the currently selected mail in Outlook) by modifying some of its attached documents, its subject and its body, and put the new e-mail either in the Draft or Outgoing mail box.

    I saw a lot of resources here on the forums but none giving a short start to this kind of task I am asking for (still looking throughout the forums, though).

    Thanks for any help !

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    I have a module specifically for working with Outlook. It's only got 1 function but it may help you out to get started.

    VB Code:
    1. Option Explicit
    2. '
    3. ' References:
    4. '   - Microsoft Outlook 9.0 Object Library (msoutl9.olb)
    5. '
    6.  
    7. Sub SendEmail()
    8.     Dim objOutlook As Outlook.Application
    9.     Dim objMailItem As Outlook.MailItem
    10.     '
    11.     Set objOutlook = New Outlook.Application
    12.     Set objMailItem = objOutlook.CreateItem(0)
    13.     '
    14.     With objMailItem
    15.         .To = "To"
    16.         .CC = "CC To"
    17.         .Subject = "Subject"
    18.         .HTMLBody = "Body in the form of HTML"
    19.         .Send
    20.     End With
    21.     '
    22. End Sub
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  3. #3

    Thread Starter
    New Member olivier.gies's Avatar
    Join Date
    Jan 2005
    Posts
    4

    Thumbs up Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    Thanks for the kickstart.

    Now, I guess it's a matter of :
    1. recover the currently selected e-mail (ideally, I would have to make a macro of this, operable with a toolbar button from Outlook)
    2. move it to a desired folder and/or send it

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    Are you wanting to do this in Outlook VBA or automate Outlook using VB?

    To get the currently selected item in Outlook using either method use the
    Outlook.Selection object. Something like this...

    VB Code:
    1. Dim oExp As Outlook.Explorer
    2. Dim oSel As Outlook.Selection
    3.  
    4. Set oExp = Outlook.ActiveExplorer
    5. Set oSel = oExp.Selection
    Ps, Dave you may want to set the .BodyFormat = olFormatHTML first before
    specifing the body content.

    Pss, if you do this in Outlook VBA you can avoid the "Do you want to send
    this emai" Security prompt.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    Quote Originally Posted by RobDog888
    Ps, Dave you may want to set the .BodyFormat = olFormatHTML first before specifing the body content.
    [/color]
    Then do I still use the
    VB Code:
    1. .HTMLBody =
    method?
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    Yes, what the format does is determines the standard used to display the text of the message.
    VB Code:
    1. Option Explicit
    2. '
    3. ' References:
    4. '   - Microsoft Outlook 9.0 Object Library (msoutl9.olb)
    5. '
    6.  
    7. Sub SendEmail()
    8.     Dim objOutlook As Outlook.Application
    9.     Dim objMailItem As Outlook.MailItem
    10.     '
    11.     Set objOutlook = New Outlook.Application
    12.     Set objMailItem = objOutlook.CreateItem(0)
    13.     '
    14.     With objMailItem
    15.         .To = "To"
    16.         .CC = "CC To"
    17.         .Subject = "Subject"
    18.         .BodyFormat = olFormatHTML
    19.         .HTMLBody = "Body in the form of HTML"
    20.         .Send
    21.     End With
    22.     '
    23. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member olivier.gies's Avatar
    Join Date
    Jan 2005
    Posts
    4

    Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    Quote Originally Posted by RobDog888
    Are you wanting to do this in Outlook VBA or automate Outlook using VB?
    I'm doing it under Outlook VBA

    Quote Originally Posted by RobDog888

    To get the currently selected item in Outlook using either method use the
    Outlook.Selection object. Something like this...

    VB Code:
    1. Dim oExp As Outlook.Explorer
    2. Dim oSel As Outlook.Selection
    3.  
    4. Set oExp = Outlook.ActiveExplorer
    5. Set oSel = oExp.Selection
    Alright. But then, Outlook.Selection object is a collection of any type of Outlook elements.
    How do I know it is an e-mail (codewise) ? Do I have to cast it somehow ?
    And also : how do I get the element itself rather than the Selection (=the collection object) ?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    This code works in either VB or VBA.
    VB Code:
    1. 'If using in VB add a reference to MS Outlook xx.0 Object Library
    2. Private Sub Command1_Click()
    3.  
    4.     Dim oExp As Outlook.Explorer
    5.     Dim oSel As Outlook.Selection
    6.     Dim oFolder As Outlook.MAPIFolder
    7.    
    8.     Dim sType As String
    9.    
    10.     Set oExp = Outlook.ActiveExplorer
    11.     Set oFolder = oExp.CurrentFolder
    12.     Set oSel = oExp.Selection
    13.  
    14.     Select Case oSel.Item(1).Class
    15.         Case olTask
    16.             sType = "Task"
    17.         Case olMail
    18.             sType = "Email"
    19.         Case olContact
    20.             sType = "Contact"
    21.         Case olJournal
    22.             sType = "Journal"
    23.         Case olNote
    24.             sType = "Note"
    25.         Case olAppointment
    26.             sType = "Calendar"
    27.         Case Else
    28.             sType = "Unknown Type!"
    29.     End Select
    30.     MsgBox sType & " Selected!"
    31. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    New Member olivier.gies's Avatar
    Join Date
    Jan 2005
    Posts
    4

    Question Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    Thanks a lot. I made huge steps in a few hours.

    There are some details, though, that I wish to change :

    Using this code as an Outlook macro, it would execute a new Outlook process each time it is executed. Is there any way to keep the execution within the running Outlook process ?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)

    How do your code look because the code I posted shouldn't be creating a new Outlook session?
    Last edited by RobDog888; Feb 1st, 2005 at 11:49 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width