I have a VBA set of code that generates an email and automatically sends it.
One of the things that I cannot get to work properly is putting a hyperlink to a specified folder location within the email.
Dim fpath As String
fpath = Worksheets("MS_JRNL_OPEN_TU_FR-4333635").Range("AD5").Value
"file://" & fpath & _
Essentially the user has to input a folder location when running the Macro which is in Cell AD5, but I want this is appear as the full folder location as a hyperlink once the email is generated.
Any help would be greatly appreciated
If you are currently using HTMLBody in your email code, it's quite easy to do. I'll assume you are using code similar to below. Take note of strbody and .HTMLBody. Assuming your fpath is formatted like C:\Users\tjb1\Desktop\file.docx then you don't need to add anything else to it. The section creating the hyperlink is "test link". You can change test link to say whatever you want or change the line to "" & fpath & "" to use the path as the link text.
Sub MailURL()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "test link"
On Error Resume Next
With OutMail
.To = "APerson#Somewhere.com"
.Subject = "Testing URL"
.HTMLBody = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I found the code above at MrExcel and just formatted it a bit to work with your variable.
Related
I'm not a programmer, but I like to create little macros for saving time on iterative tasks. Right now I was trying to create an userform able to replace on an HTML template few placeholders.
The code I've made so far:
Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = Application.CreateItemFromTemplate("C:\path\ciccio.oft")
With OutMail
.To = TextBox4.Text
.CC = ""
.BCC = ""
.HTMLBody = Replace(.HTMLBody, "###PLACEHOLDER1###", TextBox1.Text)
.HTMLBody = Replace(.HTMLBody, "###PLACEHOLDER2###", ComboBox1.Text)
Unload UserForm2
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
This works good on a plain text body, but on the HTML body just refuses to replace all ..
and seems even a little random.. so something is replaced and the rest not.
Any help?
Solved.
The issue is in how is formatted the html body. I've edited the placeholder words in other file and then copy directly on the template, saving it.
I am attempting to add a chart into the body of my email using GetInspector and WordEditor.
I am having difficulty simultaneously adding text into the body as well. Based on the paragraph position I choose, I can either have the text appear or the chart, but not both at the same time.
Code sample:
Sub generateEmail()
Dim OutApp as Object
Dim OutMail as Object
Dim filePath as String
Dim cht as ChartObject
Dim vInspector as Object
Dim wEditor as Object
Set cht = wsData.ChartObjects("Chart 2")
cht.copy
With wsHome
filePath = ""
'also including an attachment which is working fine
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set vInspector = OutMail.GetInspector
Set wEditor = vInspector.WordEditor
On Error Resume Next
With OutMail
.to = "All"
.CC = ""
.BCC = ""
.Subject = "Test"
.display
wEditor.Paragraphs(1).Range.Text = "Please see attached"
wEditor.Paragraphs(2).Range.Paste
'if I comment out paragraph 1 and change the second line to paragraph 1
'the chart prints perfectly, but the text does not show
'the way its set up now, only the "Please see attached" shows up
.Attachments.Add (filePath)
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I'm definitely missing something with the way paragraphs are read with the wordEditor, but have not used it enough to troubleshoot effectively.
On Error Resume Next hides the error that is occurring. Remove it and you'll get
Run-time error '5941': The requested member of the collection doesn't exist.
The email body only has 1 paragraph; you can't paste into paragraph 2 because it doesn't exist. Maybe Add a 2nd paragraph and then paste:
wEditor.Paragraphs(1).Range.Text = "Please see attached"
wEditor.Paragraphs.Add
wEditor.Paragraphs(2).Range.Paste
I'm using outlook 2016 and Excel 2016. I have written a code to paste a email in the outlook body. My email body contains html codes which has image included,the code works fine however I'm not able to paste the image in the email body. Please help.
Please find the vba code below.
Sub Send_Mails()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim subj As String
Dim recp As String
Dim bccrep As String
Dim ccrecp As String
Dim i As Integer
For i = 2 To 10
Sheets("Email Draft").Select
strbody = Sheets("Email Draft").Range("C1")
subj = "Welcome - " & Sheets("Macro").Range("O" & i)
recp = Sheets("Macro").Range("I" & i)
ccrecp = Sheets("Macro").Range("J" & i)
bccrep = Sheets("Macro").Range("K" & i)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = recp
.CC = ccrecp
.BCC = bccrep
.Subject = subj
.HTMLBody = .HTMLBody & strbody
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
Next i
End Sub
If you appending HTML to the existing HTML message body you are likely adding content outside of the element and it won't be visible. You have to modify what is in the element. As an alternative to manipulating the HTML string, you can use the Word Object Model to change the body content via Inspector.WordEditor.
I am doing an Automation on outlook where End user will receive a mail where their will be a link by clicking on which an auto reply mail will be trigger and go to respective person. The code i Tried So far is mentioned below.
Sub MailURL()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<HTML><BODY>"
strbody = strbody & "xxx#xxx.com"
strbody = strbody & "</BODY></HTML>"
On Error Resume Next
With OutMail
.to = "abc.domain.com"
.Subject = "Testing URL"
.HTMLBody = strbody
' .Send
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I understand that it is considered as a Spamming, but that was a requirement for our project, where we will send a mail with some data to end user and if user need some change in that data then he can click on that link and we receive a mail with requested changes so we make changes and update it in database. Thanks.
If I understand what you want, you need to add a link on the body that if clicked, will create a new mail with a pre-defined recipient. If that is so, this part:
"xxx#xxx.com"
should be:
"Relpy here"
You can also add a subject like this:
"Relpy here"
HTML syntax to add hyperlink is using your_linked_text. In your case, you need to add mailto: in front of the linked email address to create a new mail.
Note: This will not automatically send the reply (as you've said that can be considered spamming so let's not do that.)
here is a revision of your code
it worked for me (outlook2016), once i changed abc.domain.com to abc#domain.com
the "OutMail.Send" statement put the message into the outbox
the "OutMail.Display (True)" statement put the "new email" window on top of all other windows so that the user could verify the info and click "send"
Sub MailURL()
Dim strbody As String
strbody = "<HTML><BODY>"
strbody = strbody & "xxx#xxx.com"
strbody = strbody & "</BODY></HTML>"
Dim OutMail As Outlook.MailItem ' define the actual object ... then "help info" pops up on screen as you type
Set OutMail = Application.CreateItem(olMailItem) ' "Application" object already exists
With OutMail
.To = "abc#domain.com"
.Subject = "Testing URL"
.HTMLBody = strbody
' you can use only one of the following two lines
' .Send ' this puts the email message into the outbox
.Display (True) ' this show the email on top of everything else. just have the user click "send"
End With
Set OutMail = Nothing
End Sub
The following is a simple code that will send an email using whatever email program is open. My problem is that I would like to add three emails to the list but outlook 2013 only recognizes it if I have one address.
What is the correct format for outlook?
Application.Dialogs(xlDialogSendMail).Show _
arg1:="attributes#hotmail.ca", _
arg2:="East attributes" & Now()
I have tried
Application.Dialogs(xlDialogSendMail).Show _
arg1:="attributes#hotmail.ca" & ";" & "runaway#gmail.com", _
arg2:="East attributes" & Now()
Here is a very basic example of how to automate Outlook from Excel. Please note I am using LateBinding.
Option Explicit
Sub Sample()
Dim OutApp As Object
Dim OutMail As Object
Dim MyFileList(1) As String
Dim i As Long
'~~> Change/Add the file names here
MyFileList(0) = "C:\Sample1.xlsx"
MyFileList(1) = "C:\Sample2.xlsx"
'~~> Create a new instance of outlook
Set OutApp = CreateObject("Outlook.Application")
'~~> Create a new Email
Set OutMail = OutApp.CreateItem(0)
'~~> Set the To/CC/BCC etc here
With OutMail
.To = "MyEmail1#123.com" & ";" & "MyEmail2#123.com" & ";" & "MyEmail3#123.com"
.CC = "MyEmail4#123.com"
.Bcc = "MyEmail5#123.com"
.Subject = "Example for attaching 2 files"
.Body = "Hi Russel :)"
'~~> Attaching file
For i = LBound(MyFileList) To UBound(MyFileList)
.Attachments.Add MyFileList(i)
Next i
'~~> Display the email. To send the email, Change the below to .Send
.Display
End With
End Sub
ScreenShot:
You can automate Outlook from Excel to get the job done. See How to automate Outlook from another program for more information.
Use the Recipients collection to specify the To, CC or Bcc recipients.