How to forward a mail via vba
To forward a mail via VBA in Outlook, you can use the Forward
method of the MailItem
object. Here's an example code:
Sub ForwardMail()
Dim olApp As New Outlook.Application
Dim olMail As MailItem
Set olMail = olApp.CreateItem(0) ' 0 = MailItem
' Set the mail item properties
olMail.Subject = "Forwarded mail"
olMail.Body = "This is a forwarded mail"
olMail.To = "[email protected]"
olMail.CC = ""
olMail.BCC = ""
' Forward the mail
olMail.Forward
' Clean up
Set olMail = Nothing
Set olApp = Nothing
End Sub
This code creates a new mail item, sets its properties, and then forwards it to the specified recipient.
You can also use the Forward
method to forward a specific mail item that is already in the inbox. For example:
Sub ForwardMailItem()
Dim olApp As New Outlook.Application
Dim olMail As MailItem
Set olMail = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.Item(1) ' Get the first item in the inbox
' Forward the mail item
olMail.Forward "[email protected]"
' Clean up
Set olMail = Nothing
Set olApp = Nothing
End Sub
This code gets the first item in the inbox and forwards it to the specified recipient.
You can also use the Forward
method to forward a mail item that is selected in the inbox. For example:
Sub ForwardSelectedMailItem()
Dim olApp As New Outlook.Application
Dim olMail As MailItem
Set olMail = olApp.ActiveExplorer.Selection.Item(1) ' Get the selected item
' Forward the mail item
olMail.Forward "[email protected]"
' Clean up
Set olMail = Nothing
Set olApp = Nothing
End Sub
This code gets the selected item in the inbox and forwards it to the specified recipient.
Note that the Forward
method will create a new mail item with the same subject and body as the original mail item, and will add the recipient(s) specified in the To
, CC
, and BCC
properties. You can modify these properties before forwarding the mail item if needed.