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 !
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:
Option Explicit
'
' References:
' - Microsoft Outlook 9.0 Object Library (msoutl9.olb)
'
Sub SendEmail()
Dim objOutlook As Outlook.Application
Dim objMailItem As Outlook.MailItem
'
Set objOutlook = New Outlook.Application
Set objMailItem = objOutlook.CreateItem(0)
'
With objMailItem
.To = "To"
.CC = "CC To"
.Subject = "Subject"
.HTMLBody = "Body in the form of HTML"
.Send
End With
'
End Sub
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
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:
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection
Set oExp = Outlook.ActiveExplorer
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
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 method?
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:
Option Explicit
'
' References:
' - Microsoft Outlook 9.0 Object Library (msoutl9.olb)
'
Sub SendEmail()
Dim objOutlook As Outlook.Application
Dim objMailItem As Outlook.MailItem
'
Set objOutlook = New Outlook.Application
Set objMailItem = objOutlook.CreateItem(0)
'
With objMailItem
.To = "To"
.CC = "CC To"
.Subject = "Subject"
.BodyFormat = olFormatHTML
.HTMLBody = "Body in the form of HTML"
.Send
End With
'
End Sub
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:
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection
Set oExp = Outlook.ActiveExplorer
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) ?
Re: Automated Transfer/Reply e-mail (VB, MAPI, Outlook)
This code works in either VB or VBA.
VB Code:
'If using in VB add a reference to MS Outlook xx.0 Object Library
Private Sub Command1_Click()
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection
Dim oFolder As Outlook.MAPIFolder
Dim sType As String
Set oExp = Outlook.ActiveExplorer
Set oFolder = oExp.CurrentFolder
Set oSel = oExp.Selection
Select Case oSel.Item(1).Class
Case olTask
sType = "Task"
Case olMail
sType = "Email"
Case olContact
sType = "Contact"
Case olJournal
sType = "Journal"
Case olNote
sType = "Note"
Case olAppointment
sType = "Calendar"
Case Else
sType = "Unknown Type!"
End Select
MsgBox sType & " Selected!"
End Sub
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 ?
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?