I can't send email with attachments, if the path of the attachment isn't hard-coded into the vba, itself.
I get "error 438: Object doesn't support this property or method" at this line:
.Attachments.Add Me.LinkedFile1
On my form is a simple textbox called LinkedFile1.
Right now, Me.LinkedFile1 = "C:\Users\pat.lewis\Desktop\description.docx"
It works just fine when I send an email from Outlook using this code:
With olMail
.To = Me.EmailTo
.CC = Me.EmailCC
.Subject = Me.EmailSubject
.BodyFormat = olFormatPlain
.Body = Me.EmailMessage
.Attachments.Add "C:\Users\pat.lewis\Desktop\description.docx"
.Send
End With
But it doesn't work if I assign the value of Me.LinkedFile1 to the .Attachments.Add property:
With olMail
.To = Me.EmailTo
.CC = Me.EmailCC
.Subject = Me.EmailSubject
.BodyFormat = olFormatPlain
.Body = Me.EmailMessage
.Attachments.Add Me.LinkedFile1 'DOESN'T WORK!!
.Send
End With
It Also does not work if I assign the value of Me.LinkedFile1 to a string variable then reference that:
Dim olAttachment1 As String
If IsNull(Me.AttachedFile1) = False Then
olAttachment1 = Me.LinkedFile1
End If
With olMail
.To = Me.EmailTo
.CC = Me.EmailCC
.Subject = Me.EmailSubject
.BodyFormat = olFormatPlain
.Body = Me.EmailMessage
.Attachments.Add olAttachment1 'DOESN'T WORK EITHER!!
.Send
End With
I have confirmed multiple ways that the value of Me.LinkedFile1 does actually equal
"C:\Users\pat.lewis\Desktop\description.docx".
Any ideas on what I could be missing here?
Using UNBOUND textbox set with result of FilePicker works just fine for me.
Only issue with textbox I can think of that would cause this failure is corruption. If running Decompile/Compile and/or Compact & Repair and/or deleting & replacing textbox does not resolve, eliminate it from the process. Set olAttachment1 variable directly with result of FilePicker. You can still set the textbox if you want to display to user.
Related
In windows 7 and Office 2007 I have been using a code which opens a new email in Outlook, attach a file and send it. The code it's not mine, I found it somewhere in the internet. The problem is that now I use Windows 10 and Office 2016, and using the same code produce different results as:
The original name of the file, let's say for example "Products.xlsx", is changed to "ProjectStatus.xlsx" (any file name is always changed to "ProjectStatus.xlsx")
If I open the file then Excel opens it and shows the original name of the file ("Products.xlsx")
If I send it, sometimes the recipients see the attached file as "ProjectStatus.xlsx" and sometimes see it as "Products.xlsx". But what always happens is that if they open the file, in excel is seen as "Products.xlsx"
I need the file name always be shown with the original name. How can I do this?
This is the code I use it and is executed from both access 2016 and excel 2016.
Sub MandaMailA(destinatarios As String, copia As String, subject As String, strbody As String, attachment1 As String, Optional attachment2 As String = "", Optional CO As String = "")
Dim OutApp As Object
Dim OutMail As Object
Dim SigString As String
Dim Signature As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Change only Mysig.htm to the name of your signature
SigString = Environ("appdata") & _
"\Microsoft\Firmas\VBA.htm"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
On Error Resume Next
With OutMail
.To = destinatarios
.CC = copia
.BCC = CO
.subject = subject
.HTMLBody = strbody & "<br>" & Signature
.Display 'or use .Display
.Attachments.Add attachment1, olByValue, 1, "ProjectStatus"
.Attachments.Add attachment2, olByValue, 1, "ProjectStatus"
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I notice that this code includes the word "ProjectStatus" but honestly I have not a deep knowledge of VBA.
Thanks in advance!!
A simple read of the Attachments.Add documentation is all you need, specifically the section on the optional DisplayName parameter:
This parameter applies only if the mail item is in Rich Text format
and Type is set to olByValue : the name is displayed in an Inspector
object for the attachment or when viewing the properties of the
attachment. If the mail item is in Plain Text or HTML format, then the
attachment is displayed using the file name in the Source parameter.
So if you always want to always use the original file name, simply delete the instances of , "ProjectStatus".
I've got an Excel spreadsheet built by someone else that sends an email to a group via a scheduled task, or at least used to. It recently stopped working. I don't have the time to rebuild his whole Rube Goldberg / Wile E. Coyote system at the moment, so I'm trying to fix it.
In one of the excel documents, this code exists
Set rng = Nothing
On Error Resume Next
Set rng = Sheets("Weight").Range("A2")
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "eric.lizotte#.com"
.CC = ""
.BCC = ""
.Subject = Sheets("Weight").Range("A1")
.HTMLBody = Convert.ToString(rng)
.Send
End With
whenever I see so many "on error resume next" in something I grit my teeth.
What's happening is that it "works" but the email it sends is blank. In debug I can see that the subject content and body content both exist and have values. I rewrote it to be
OutMail.To = "eric.lizotte#.com"
OutMail.CC = ""
OutMail.BCC = ""
OutMail.Subject = Sheets("Weight").Range("A1")
OutMail.HTMLBody = Convert.ToString(rng)
OutMail.Send
and threw a debug point on the send, and checked the properties of outmail, and the subject and htmlbody have values. I can't figure out why after the send they are blank.
Given you are automating another application you probably should not rely on the default property of the range - just specify the property you want e.g. Value:
OutMail.Subject = Sheets("Weight").Range("A1").Value
OutMail.HTMLBody = rng.Value
You might also try the Text property for the HTMLBody:
OutMail.HTMLBody = rng.Text
You can then be confident that you are assigning a String to the mail properties which is what it expects.
I am using the answer (i.e. the code) to this question Working with current open email to build an email in steps. Every time I run the macro, it adds a piece of body text to the existing body text of an open email. The problem is that if I manually do some changes to the email between the macro "runs", then upon next run, the macro will insert the new body text building block where I left the cursor. But I want the macro to always add the new body text after the end of the existing (and expanding) body text. More specifically, this means that I (probably) need some code just before the line with the code "oSelection.InsertAfter myText" (see the code in the above link) that moves the cursor (insertion point) to the end of the open email that I am working with.
I have tried to play around with the SendKeys-command but using that I only manage to send the cursor to the end of the excel workbook sheet where I have the macro button. I want the cursor to end of the open email instead!
Ok, so I finally figured it out:
If oInspector.IsWordMail Then
With NewMail
.Display
SendKeys "^+{END}", True
SendKeys "{END}", True
SendKeys "{NUMLOCK}"
End With
The macro leaves the cursor in the subject field. This will jump cursor down into the body, and move it to the end of the line:
SendKeys "{Tab}{End}", True
Full Example:
Public Sub CreateNewMessage()
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.To = "fake#example.com"
.CC = "other#example.com"
.BCC = ""
.Subject = "Test email for you"
.Categories = ""
.VotingOptions = ""
.BodyFormat = olFormatPlain ' send plain text message
.Body = "Thank you for your submission. "
.Display
SendKeys "{Tab}{End}", True
End With
Set objMsg = Nothing
End Sub
References:
http://www.slipstick.com/developer/create-a-new-message-using-vba/
This line:
SendKeys "{Tab}{End}", True
left an inactive cursor at the beginning of the body of my e-mail message. By manually tabbing, I noticed the cursor moving through the From, To, CC, BCC, etc. fields. It took 11 tabs, and 1 End to get the cursor to the end of a line of text in my e-mail. Silly though it seemed, I coded the extra tabs in, and it worked perfectly.
With Mess
.Display
.Sensitivity = 3
.To = Recip
.bcc = "redacted recipient"
.subject = subject
.Attachments.Add Path & fileName & ".pdf"
.HTMLBody = strbody & "<br>" & .HTMLBody
SendKeys "{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{End}", True
I've created a subroutine that grabs all the relevant details and attachments to send out automated emails for me. Here is the code I have:
Sub Mail_workbook_Outlook_1()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim To1 As String, CC1 As String, BCC1 As String, Title1 As String, Body1 As String, Att1 As String
' Create "Other Distribution - In Email" Emails
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
To1 = Cells(8, 2).Value
CC1 = Cells(8, 3).Value
BCC1 = Cells(8, 4).Value
Title1 = Cells(8, 5).Value
Body1 = Cells(8, 6).Value
Att1 = Cells(8, 7).Value
On Error Resume Next
With OutMail
' BodyFormat command makes the email a rich format message allowing us to place attachments within the body of the email instead of in the attachment header
.BodyFormat = olFormatRichText
.To = To1
.CC = CC1
.BCC = BCC1
.Subject = Title1
.Body = Body1
.Attachments.Add Att1, , 10
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
This works fine and inserts my attachment at the end of my email body. The issue is this specific line:
.Attachments.Add Att1, , 10
In this code the "10" is supposed to represent the position of the attachment. I.E. this is supposed to mean that after the first 9 characters in my "Body1" variable, instead of the 10th character the attachment will be placed here. See here: https://msdn.microsoft.com/en-us/library/office/ff869553.aspx
However, for some reason no matter what number I put in the position option it always just puts my attachment at the very end of the email. I need the attachment to be in the middle of several paragraphs so this is causing an issue. Any idea what is causing this?
I should mention I have selected the Microsoft Outlook Object Library from Tools>References.
Any help is greatly appreciated!
So I found out that this is a bug in Outlook 2008/2010 for which there does not seem to be a fix :(
http://argurosblog.blogspot.com/2011/11/how-to-create-task-or-appointment-using.html
Change the content of the body with:
Body1 = "This is the body of the mail, line 1" & vbcrlf
Body1 = Body1 & "This is the second line of text, line 2" & vbcrlf
Body1 = Body1 & "This is the last line of text, line 3."
and run your code.
As you can see the attachment is not placed after the 10.th character, but after the first vbcrlf found after the 10.th character.
If you try with .Attachments.Add Att1, , 50 (in the middle of the second line), it will be placed between line 2 and line 3.
If you delete all the vbcrlfs characters in your body, it will placed at the end of the body, and that is probably what happens to you.
Parse the content of your body and insert vbcrlf ('hard returns') characters where needed.
Hope this helps.
I am creating an email from a macro (Excel file Macro) and am trying to make the attachment be sandwiched by text. Using the following code the attachment is added after the signature, I would like for it to have body, file, signature
Set olMailItem = olApp.CreateItemFromTemplate(EmailFilePath)
With olMailItem
'search and replace subject for "KW ##" to the kw from "Key Metrics" tab
.Subject = Replace(.Subject, "KW ##", KW)
.Attachments.Add ("H:\QA\QA Mgmt\Presentations\" & reports(r).filename)
.Body = .Body & GetSignature(emailTemplatePath & "EmailSignature.txt")
.Display
End With
EmailFilePath is a path to an msg file with an existing body and reciepients. Is there a way I can force the signature to be added after the attached file?
Thanks in advance
The attachments can be placed inside of the body only when the "bodyformat" of the email is set to "Rich Text"
You can do this by implementing the following line of code into your function:
.BodyFormat = olFormatRichText ' 3 also works
Implementation
Set olMailItem = olApp.CreateItemFromTemplate(EmailFilePath)
With olMailItem
' **** Add this line vvvv ****
.BodyFormat = olFormatRichText ' 3 also works
'search and replace subject for "KW ##" to the kw from "Key Metrics" tab
.Subject = Replace(.Subject, "KW ##", KW)
.Attachments.Add ("H:\QA\QA Mgmt\Presentations\" & reports(r).filename)
.Body = .Body & GetSignature(emailTemplatePath & "EmailSignature.txt")
.Display
End With
Also, the 3rd parameter of the ".Attachments.Add" is "Position" and it is 0 by default but this number represents which character within the body that the attachment will be located. If your number is greater than the total amount of characters it will automatically be placed at the end. If your number is 1 it will be placed at the beginning.
This should work fine, just change the number of the position parameter
.Attachments.Add Source:="H:\QA\QA Mgmt\Presentations\" & reports(r).filename, Position:=100
More about Outlook Attachments: http://msdn.microsoft.com/en-us/library/office/ff869553(v=office.15).aspx