VBA Outlook: E-mail Forwarding

If you are, for example, using Form Wizard Pro as a part of mojoPortal and want to ensure that individuals filling out your form receive a copy of their submission, this code snippet may prove useful:

Dim emailtosend As String

Sub SAHE_EVALS(msg As Outlook.MailItem)

Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
Dim objNS As Outlook.NameSpace

On Error Resume Next
On Error GoTo 0

msg.BodyFormat = olFormatPlain
‘CALL THE SUB TO PARSE BODY TEXT AND GET THE EMAIL

If SearchBody(msg.Body) Then
‘Continue code

Dim ForwardEmail As Outlook.MailItem
Set ForwardEmail = msg.Forward

With ForwardEmail
.Recipients.Add emailtosend
.Send
End With

msg.UnRead = False
Set objNS = Application.GetNamespace(“MAPI”)
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objFolder = objInbox.Folders(“zzz_FormSubmissions”)

‘This is to move item to trash
‘objNS.GetDefaultFolder (olFolderDeletedItems)

If objFolder Is Nothing Then
MsgBox “This folder doesn’t exist!”, vbOKOnly + vbExclamation, “INVALID FOLDER”
End If

msg.Move objFolder

Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing

ExitProc:

Else
MsgBox “error parsing email”, vbCritical
GoTo ExitProc
End If
End Sub

Function SearchBody(ByVal msgbody) As Boolean
Dim foundEmail As Boolean
foundEmail = False

Lines = Split(msgbody, vbCrLf)
‘GET THE EMAIL TO SEND TO
‘DOing this in ugly fashion since the e-mail address is actually on the next line
For Each Line In Lines
If InStr(Line, ” Email:”) Then
foundEmail = True
Else
If foundEmail = True Then
If InStr(Line, “@”) Then
emailtosend = Line
SearchBody = True
Exit Function
Else
‘Error parsing
MsgBox “error parsing email”, vbCritical
End If
End If
End If
Next
End Function

Once this code has been created in Visual Basic Editor (just press Alt + F11), you can create a rule that triggers the above code:

Rules/Alerts