I have a custom form that users will use when they receive a certain email. I have the VBA to select the email and forward it, but I cannot figure out how to include the forwarded email within the custom form's email body.
UPDATE: For any lost soul trying to accomplish this, I've found a solution to my problem.
Sub CustomForm_IncludesSelectedEmailBody()
Dim myOlApp As Application
Dim myNameSpace As NameSpace
Dim myFolder As MAPIFolder
Dim myItems As Items
Dim myItem As Object
Dim origEmail As MailItem
Set origEmail = ActiveExplorer.Selection(1)
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = _
myNameSpace.GetDefaultFolder(olFolderTasks)
Set myItems = myFolder.Items
Set myItem = myItems.Add("IPM.Note.DL_MALC_R_V1")
'the "IPM.Note.DL_MALC_R_V1" is the custom form's file name
myItem.HTMLBody = origEmail.Forward.HTMLBody
'this attaches the body of the email you're viewing to your new, forwarded email
myItem.Subject = origEmail.Forward.Subject
'This includes the selected email's subject line in your new email.
myItem.To = "[email protected]"
myItem.Display
Set myOlApp = Nothing
Set myNameSpace = Nothing
Set myFolder = Nothing
Set myItems = Nothing
Set myItem = Nothing
Set CustomForm = Nothing
End Sub
To create a button for running the VBA, that's as simple as customizing the ribbon and selecting the macro to run from there.
/End Updated Answer
Essentially what I want to happen is:
- The user clicks a button which loads my custom form
- Upon the button click in step 1, the selected or viewed email is included in the custom form's email body.
My thinking was that I would combine the 2 code sections below with the following
Sub Combined()
RunMyForm
Forward
'gets the forwarded email in the Forward sub, but doesnt include the body of the forwarded email in the custom form.
End Sub
and that it would work, but that isn't happening. I'm very new to VBA so I'm sure I'm missing something here.
Creating the button isn't the problem, as that's easy to do within Outlook itself. I listed the 2 code sections from the Sub Combined() below.
- VBA to pull the custom form.
Sub RunMyForm()
Dim myOlApp As Application
Dim myNameSpace As NameSpace
Dim myFolder As MAPIFolder
Dim myItems As Items
Dim myItem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = _
myNameSpace.GetDefaultFolder(olFolderTasks)
Set myItems = myFolder.Items
Set myItem = myItems.Add("IPM.Note.Test1")
myItem.Display
Set myOlApp = Nothing
Set myNameSpace = Nothing
Set myFolder = Nothing
Set myItems = Nothing
Set myItem = Nothing
End Sub
- Selecting the currently viewed item, forwarding it, and forwarding it to a specific email address.
Sub Forward()
'Combined with function GetcurrentItem, this forwards the selected email
Dim fwd As Outlook.MailItem
Dim itm As Object
Set itm = GetCurrentItem()
If Not itm Is Nothing Then
Set fwd = itm.Forward
fwd.Recipients.Add "[email protected]"
fwd.Display
End If
Set fwd = Nothing
Set itm = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = CreateObject("Outlook.Application")
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
Case Else
' anything else will result in an error, which is
' why we have the error handler above
End Select
Set objApp = Nothing
End Function
Set GetCurrentItem = Nothing
in the Case Else? If this macro is run within Outlook, no need to Create Outlook Application Object. – PatricK