Editing and forwarding email (from different sender) Outlook VBA

75 Views Asked by At

i'm new in VBA so i need help to understand how can i:

  1. taking an email from my inbox, edit the body (deleting the last two rows and the first five rows).
  2. Forwarding that modified mail from another mailbox to another one.
1

There are 1 best solutions below

0
Eugene Astafiev On

taking an email from my inbox, edit the body (deleting the last two rows and the first five rows).

First of all, you need to get the Inbox folder in the code. The GetDefaultFolder method can help with that. As soon as you retrieved an instance of the Folder class you may get an item and edit the message body. The Outlook object model supports three main ways of customizing the message body:

  1. The Body property returns or sets a string representing the clear-text body of the Outlook item.
  2. The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately.
  3. The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.

Forwarding that modified mail from another mailbox to another one.

The MailItem.Forward method executes the Forward action for an item and returns the resulting copy as a MailItem object. For example:

Sub RemoveAttachmentBeforeForwarding() 
 Dim myinspector As Outlook.Inspector 
 Dim myItem As Outlook.MailItem 
 Dim myattachments As Outlook.Attachments 
 
 Set myinspector = Application.ActiveInspector 
 If Not TypeName(myinspector) = "Nothing" Then 
   Set myItem = myinspector.CurrentItem.Forward 
   Set myattachments = myItem.Attachments 
   While myattachments.Count > 0 
     myattachments.Remove 1 
   Wend 
   myItem.Display 
   myItem.Recipients.Add "Eugene" 
   myItem.Send 
 Else 
   MsgBox "There is no active inspector." 
 End If 
End Sub