I've seen this question posted several times but after looking extensively, I have not found a successful answer.
I'm trying to create a script for Outlook that auto-forwards an email with an attachment back to the sender, with an updated message, that includes the original attachment. It's all working EXCEPT no matter what I do I cannot forward the attachment. If I just use .Forward, the attachment is not included. If I try to use the .Attachment property, it breaks the script.
CODE:
Sub ForwardEmail(Item As Outlook.MailItem)
With Item.Forward
.Attachments.Add Item.Attachment
.Subject = ("APPROVED - ") & Item.Subject
.Recipients.Add Item.SenderEmailAddress
.Body = "Your timesheet has been approved." & vbCrLf & Item.Body
.Send
End With
End Sub
The Microsoft Outlook (and other email engines like Gmail, Yahoo) has been designed in this way:
When a mail is replied, the receiver do not need same attachments as the receiver already has them in their outbox (unless deleted). It saves storage space on server and network bandwidth too.
When a mail is forwarded, it is expected that forward is generally to at least a new user and hence all the content remain as is (i.e. attachments aren't removed)
Hence, please use this code. It follows the Rule#2 mentioned above i.e. whenever you forward an email, the attachment will be automatically attached.
Sub ForwardEmail(item As Outlook.MailItem)
Dim oMail As MailItem
On Error GoTo Release
If item.Class = olMail Then
Set oMail = item.Forward
oMail.Subject = ("APPROVED - ") & Item.Subject
oMail.HTMLBody = "Your timesheet has been approved." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add Item.SenderEmailAddress
oMail.Save
oMail.Send
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub
Related
I am working on a program to look for a specific email in a folder and perform some actions on that email based on a UserForm.
I create and show the UserForm but it is not connected to the email, so I am unable to pass the data from the Userform to the email before the UserForm is unloaded for the next email in the list.
UserForm with redacted information
None of the code is implemented, so I will not include the code here.
If I try to run a script if "Format and Send" is pressed, I am unable to find what email my loop is on as it is in another sub.
Is there a way to access the values of the UserForm in my sub function that creates the UserForm?
Sub nameofSubRedacted()
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = FindInFolders(myNameSpace.Folders, "specific folder")
Dim mail As MailItem
For Each mail In myFolder.Items
If InStr(mail.Subject, "specific text") Then
mail.Display
UserForm_Main.Show
'this is where the form is filled in, and a selection is made.
End If
Next mail
End Sub
You need to change the MessageClass property of the message to match that of the custom form.
You can pass the mailitem as a parameter.
Option Explicit
Sub nameofSubRedacted()
Dim myFolder As Folder
Dim mail As MailItem
Dim replyMail As MailItem
Set myFolder = Session.GetDefaultFolder(olFolderInbox)
For Each mail In myFolder.Items
If InStr(mail.subject, "specific text") Then
mail.Display
UserForm_Main.Show
'this is where the form is filled in, and a selection is made.
Set replyMail = mail.reply
With replyMail
.HTMLBody = "Hello " & UserForm_Main.TextBox1.Value & .HTMLBody
.Display
End With
MsgBox "Preferred name updated." & vbCr & vbCr & _
"Pass replyMail for more processing."
moreProcessing replyMail
End If
Next mail
End Sub
Sub moreProcessing(mailAsParameter As MailItem)
MsgBox "Reply mail with subject:" & vbCr & vbCr & _
mailAsParameter.subject & vbCr & vbCr & _
" has been passed to moreProcessing."
End Sub
I have a client that is using Access 2002 because it allows Replication. He is using this on Windows 10 with Outlook from Office 365.
The goal is to create a new email with all of the info filled in and attach a scanned proposal so that my client can review the email, make any changes that he wants and then send it.
In Access, the SendObject command creates and opens a plain text email and while this email is open my Outlook macro to scan a document and attach it to the email will not run.
So I would like to create a new Outlook email from Access that allows me to run my Outlook macro.
Or if I could get Access 2002 to create an email and attach the scanned document to it, I think I could get by with using msgboxes to verify specific items.
Below is the Access macro with the SendObject command followed by the Outlook macro.
Private Sub EmailProposal_Click()
'Access macro.
Dim stDocName As String
Dim stEmailAddress As String
Dim stSubject As String
Dim stMessage As String
stDocName = "rptProposal"
stEmailAddress = Forms!RequestForm!EmailAddress.Value
stSubject = "PROPOSAL"
stMessage = "Your proposal is attached." & vbCrLf & vbCrLf & "If you have any questions, please call us."
'Email the proposal.
DoCmd.SendObject acReport, stDocName, acFormatRTF, stEmailAddress, , , stSubject, stMessage
End Sub
Sub Scan()
'Outlook macro.
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
On Error Resume Next
Dim objCommonDialog As WIA.CommonDialog
Dim objImage As WIA.ImageFile
Dim strPath As String
Set objCommonDialog = New WIA.CommonDialog
'This shows the dialog box. I'd rather tell it what to do instead of having to manually choose each time.
Set objImage = objCommonDialog.ShowAcquireImage
strPath = Environ("TEMP") & "\TempScan.jpg" 'Save the scan.
If Not objImage Is Nothing Then
objImage.SaveFile strPath ' save into temp file
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
ActiveInspector.WordEditor.Application.Selection.Inlineshapes.AddPicture strPath 'Insert into email. I want to attach it instead.
End If
End If
Kill strPath
Else
MsgBox "The Scan macro in Outlook did not find a document." & vbCrLf & vbCrLf & _
"Please place the proposal in the printer so it can be scanned.", vbOKOnly
End If
lbl_Exit:
Set objImage = Nothing
Set objCommonDialog = Nothing
Exit Sub
ErrHandler:
Beep
Resume lbl_Exit
End Sub
It seems you just need to automate Outlook for sending out emails with the required content set up. Take a look at the following articles that give you the basics of Outlook automation:
Automating Outlook from a Visual Basic Application
Automating Outlook from Other Office Applications
Sub Send_Mail_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "eugene#astafiev.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I haven't noticed manually forwarding an email using outlook (2016) forward button is giving me different result from when I use a macro to forward it. Here is my macro:
Sub W()
Dim helpdeskaddress As String
Dim objMail As Outlook.MailItem
Dim strbody As String
Dim oldmsg As String
Dim senderaddress As String
Dim addresstype As Integer
' Set this variable as your helpdesk e-mail address
helpdeskaddress = "blah#blah.com"
Set objItem = GetCurrentItem()
Set objMail = objItem.Forward
' Sender E=mail Address
senderaddress = objItem.SenderEmailAddress
'Searches for # in the email address to determine if it is an exchange user
addresstype = InStr(senderaddress, "#")
' If the address is an Exchange DN use the Senders Name
If addresstype = 0 Then
senderaddress = objItem.SenderName
End If
'adds the senders e-mail address as the created by object for the ticket and appends the message body
strbody = "#created by " & senderaddress & vbNewLine & vbNewLine & objItem.Body
objMail.To = "receiver#blah.com"
objMail.Subject = objItem.Subject
objMail.Body = strbody
' remove the comment from below to display the message before sending
'objMail.Display
'Automatically Send the ticket
objMail.Send
MsgBox ("The email has been sent for verification. You may receive a report in a few moments.")
Set objItem = Nothing
Set objMail = Nothing
End Sub
and a function to obtain the current email object item:
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = 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
End Select
End Function
When I forward an email, I can see all images (linked to another website on the Internet) I am forwarding but when I use the following macro, all I see is the text inside the email. Is there anyway I can make the following macro to do the similar job as manually forwarding does?
To forward the original content, use HTMLBody instead of Body:
strbody = "HTML-encoded content"
objMail.HTMLBody = strbody & objMail.HTMLBody
Sample HTML Format specific text in Outlook
Thanks for any help with this. I have a userform I've made that gathers criteria from the user and then when they hit submit it opens Outlook and emails that data to me.
I'm having 2 issues. The first is that when I try to use SENDKEYS method I'm running into the spell check feature stopping the email from actually sending without the user needing to go through it. Is there a way to bypass spell check and send the email?
Secondly, I couldn't find a way to actual send an email automatically without using SENDKEYS but I'm sure there is a better way out there to send the email rather than manipulating the window with TAB key strokes.
Private Sub SubmitButton_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strBody, RequestName, ProductName, Month, TestName, Summary As String
If Me.RequesterNameTxt.Value <> "" And Me.ProductCombo.Value <> "" And Me.MonthCombo.Value <> "" And Me.TestNameCombo <> "" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
RequestName = Me.RequesterNameTxt.Value
ProductName = Me.ProductCombo.Value
Month = Me.MonthCombo.Value
TestName = Me.TestNameCombo.Value
Summary = Me.SummaryTxt.Value
strBody = "<HTML><BODY>"
strBody = "Requester Name: " & RequestName & "<BR>" & "Product Name: " & ProductName & "<BR>" & "Month: " & Month & "<BR>" & _
"Test Name: " & TestName & "<BR>" & "<BR>" & "Summary of Request: " & Summary
strBody = strBody & "</BODY></HTML>"
On Error Resume Next
With OutMail
.To = "example#gmail.com;"
.CC = ""
.bcc = ""
.Subject = "QA Service Request"
.htmlBody = strBody
.send 'This fixed my issue. I had this as .Display which opens email up and doesn't send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Application.SendKeys ("%s")
Else: MsgBox "Please fill out all form data before submitting request. Thank you!"
End If
End Sub
You need to use the Send method of Outlook items instead. The Send method sends an item using the default account specified for the session. In a session where multiple Microsoft Exchange accounts are defined in the profile, the first Exchange account added to the profile is the primary Exchange account, and is also the default account for the session. To specify a different account to send an item, set the SendUsingAccount property to the desired Account object and then call the Send method.
Also I'd recommend using the Recipients property for adding recipients instead. The property returns a Recipients collection that represents all the recipients for the Outlook item.
I have code that creates a PDF of a sheet and saves it to a relative path. I'm then trying to upload that PDF to an email that displays (not sends). I want this all to happen at once as I click a button (I've assigned a macro to run both macros to this button).
However, I'm having trouble getting the attachment to work. Is it possible to save the attachment as a relative path? I'm doing it this way because the code needs to be standardized so I can use it on quite a few spreadsheets just by copying and pasting.
The specific code I'm using for the email is below.
On Error Resume Next
With OutMail 'Todos dados do email
.to = ""
.CC = ""
.BCC = ""
.Subject = "" & CompanyName & " - Invoice - " & Data & ""
.Body = "Dear ," & Chr(10) & Chr(10) & "Please, find enclosed the invoices regarding the (COLOQUE ACORDO AQUI) between our companies."
.Attachments.Add 'Adicionar Attachments
'.Attachments.Add ("C:\test.txt")
.Display '.Display para mostrar o email
End With
This is how Microsoft recommends it be done.
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myItem = Application.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\Test.doc", _
olByValue, 1, "Test"
myItem.Display
See here: Attachments.Add Method