I have a workbook that houses several macros, one takes a message body that has been entered into a specific cell (this changes every quarter based on what the business wants) and uses it as the email body that is generated by the macro. The body of the email is pre-formatted when entered into this cell and retains that formatting when I use the regular body in VBA but loses its formatting when I change it to HTMLBody. They recently changed the email message to include hyperlinks. Unfortunately, if I try to make the hyperlinks look correct, it appears I need HTMLBody but then I lose the formatting of my message.
OriginalText = HF.Sheets("Email_Setups").Range("B9").Value
Link = "Open Requests"
CorrectedText = Replace(OriginalText, "Open Requests", Link)
'Create a new email item
Set objMail = objOutlook.CreateItem(olMailItem)
'From
'To
'objMail.To = Sheet1.Range("A" & lCounter).Value
'Cc
'objMail.CC = Sheet1.Range("B" & lCounter).Value
'Subject
objMail.Subject = HF.Sheets("Email_Setups").Range("A9").Value & " " & Tar_Num
'Email Body
objMail.HTMLBody = CorrectedText
With this using the HTMLBody, the formatting from the original cell text is lost. Is there a way to retain it without having to enter the whole message body in HTML code (since it changes every time and needs to be something the business can do without me coding it) and still get the desired result of the hyperlink? Thanks in advance!
Related
There is and light order managment system where you can order a few items. When you have chosen your order, you click a button, and the rows that is order and number of items is copied to a new confirmation sheet. This sheet is then supposed to be sent to a chosen reciever by oMail. This works fine.
But in one of the columns there is a certificate on PDF(hyperlink), that is linked to a server(local file on my computer:))
I'm trying to send this PDF's as multiple hyperlinks in the mail, but that is no sucess :p. The thing I want to do is check if the cell is empty, if not, attach the hyperlink(s) in my stringbody. And send the mail.
Here is my code:
I use a function also to make the range to html format:
It's more useful to edit e-mail body via GetInstector
For example:
Dim myInspector As Object'inspector object
Dim wdDoc As Object''document
Dim myitem As Object'mailitem
Set myitem = CreateObject("Outlook.Application").CreateItem(0)'creating mailitem from Excel
Set myInspector = myitem.GetInspector'set inspector to edit mailitem
Set wdDoc = myInspector.WordEditor'set MS Word to edit mailbody
With myitem'here you are setting the e-mail properties
.To = Email'fill to e-mail
.Subject = Subja'Subject
.Display'display mailitem etc.
End With
'here edit the body with MS Word methods
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(1).Range.Text = "Something you want in 1st paragraph" & vbCr
wdDoc.Range.Paragraphs.Add
wdDoc.Range.Paragraphs.item(2).Range.Text = "Something else you want in 2nd paragraph " & vbCr
So, actually you edit e-mail programmatically the same as you do it in Word. It makes useless long and complicated string with HTML-tags. You add MS Word objects and forms.
I have an Excel Workbook with the 2 sheets.
Sheet 1 contains an Email template and sheet 2 contains the raw data.
Sheet 2 contains the headers as mentioned below.
Name ERP Employee Date Email Start Date Is Anniversary(Yes/No) Years Completed
In Sheet 1, I have created a drop-down list where the list will contain the names of the Employees who have the Is Anniversary field (yes)
I have written a VBA script to send an Email to the person who is selected in the drop-down list.
I would like to make it fully automated where the script should be able to select the next value automatically and then send the Email to the person and likewise for the whole list. Please suggest if there is a possibility.
Script for sending the Email
Sub Send_Anniversary_Email()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Mailer")
Dim lr As Integer
lr = sh.Range("J" & Application.Rows.Count).End(xlUp).Row
sh.Range("E5:L" & lr).Select
With Selection.Parent.MailEnvelope.Item
.to = sh.Range("B12").Value
.cc = sh.Range("B13").Value
.Subject = sh.Range("B14").Value
.send
ActiveWorkbook.Save
End With
MsgBox "Done"
End Sub
I have tried to create the same coding but once i try to send an email to a second person, i get the "Mailenvelope of object_worksheet failed" from this "With Selection.Parent.MailEnvelope.Item" code.
What am i supposed to do here?
In Excel VBA 2013 want to use a selection of filtered cells in excel as a source of email addresses to be passed to a procedure that generates a new email.
My data looks like this:
I have a line of code which should select all data (email addresses) in column B after the header row:
Dim recipients As String
....
recipients = Join(Application.Transpose(.Range("B6" & .Rows.Count).Value), ";")
This line produces an error Invalid or Unqualified Reference.
The recipients string is later passed to the email procedure. For completeness the email VBA is:
With OutMail
.Subject = "Some subject"
.Body = "Get a body for the email"
.To = recipients
.Importance = olImportanceHigh
.Display
End With
Here's how you can iterate the filtered cells in code:
Dim recipients As String
Dim recipient As Range
With ThisWorkbook.Sheets("Sheet1") ' Or ActiveSheet
For Each recipient In .Range("B6:B" & .UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Cells
recipients = recipients + recipient + ";"
Next recipient
End With
1: Your range needs to be: Range("B6:B" & .Rows.Count)
2: You should restrict rows to the used area Range("B6:B" & .UsedRange.Rows.Count)
3: To select only the filtered cells you need to add .SpecialCells(xlCellTypeVisible)
Something like this:
recipients = Join(Application.Transpose(.Range("B6:B" & .UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Value), ";")
4: It is probably better to iterate the cells in code instead of using Transpose/Join: Otherwise you may get an error when only 1 email address is filtered, or no emails are visible.
What I am trying to do is basically you click a button, it brings up the Excel MailEnvelope to send an email, and you can then send it off to the relevant button email address'.
However one of the email addresses needs to be modifiable by the end user.
So I want a drop down where you select said email, and it then inputs that into the VBA code.
I know basically nothing about VBA and I could not find a way of doing this by searching around the web.
I figured I need some way of setting a variable to read a cell (the drop down cell), and then input that into the MailEnvelope Item.CC but I was struggling.
Any help would be appreciated.
This is what I have so far;
Sub Send_Range_Email()
' Select the range of cells on the active worksheet.
ActiveSheet.Range("B6:D302").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = True
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To, CC and Subject lines.
With ActiveSheet.MailEnvelope
.Introduction = ""
.Item.To = "Email 0"
.Item.Subject = "Email Tracker Results"
.Item.CC = "Email 1" & text input here & "Email 2"
End With
End Sub
When using formulas, if you want to put a variable in there, just break it apart and add in the variable. As commented,
.Item.CC = "email 1" & "," & Range("A1").Value & ", " & "Email 2"
So to make super clear, say we want to add A1's value in this string: str = The man lives in STATE all the time by doing str = "The man lives in " & Range("A1").Value & " all the time"
I'm writing the VBA code in Outlook, to tracking all email information to 1 Excel file.
For each email, I will put information in 1 row of Excel. And I want to add a Hyperlink in Excel file to open the corresponding Email.
My code looks like this:
Set objFD = objNS.Folders("dtk142#aaaa.com")
Set objToFD = objFD.Folders("Inbox")
For Each Msg In objToFD.Items
'' mail information
mDate = Msg.ReceivedTime
mSubject = Msg.Subject
mSender = Msg.SenderName
mSAddress = Msg.SenderEmailAddress
' put to excel
xlTmp.Sheets("RequestTracker").Cells(cntrow, 1).Value = cntID
xlTmp.Sheets("RequestTracker").Cells(cntrow, 2).Value = mDate
xlTmp.Sheets("RequestTracker").Cells(cntrow, 3).Value = "Email"
xlTmp.Sheets("RequestTracker").Cells(cntrow, 5).Value = mSAddress
xlTmp.Sheets("RequestTracker").Cells(cntrow, 7).Value = user
xlTmp.Sheets("RequestTracker").Cells(cntrow, 11).Value = mSubject
'' code to add hyperlink to the email item here
cntrow = cntrow + 1
cntID = cntID + 1
Next
Next
Now I get stuck in creating hyperlink in excel to open corresponding Email. Please help me in this case. Thank you!!!
A simple hyperlink will not allow to open a message in Outlook. Instead, you need to handle the hyperlink click (or button) and find a corresponding entry in Outlook. Properties shown on the Excel workbook don't allow to identify mail items uniquely. You can use the EntryID value, but it can be changes when an item is moved to another store/folder in Outlook. Here is what MSDN states:
A MAPI store provider assigns a unique ID string when an item is created in its store. Therefore, the EntryID property is not set for a Microsoft Outlook item until it is saved or sent. The EntryID changes when an item is moved into another store, for example, from your Inbox to a Microsoft Exchange Server public folder, or from one Personal Folders (.pst) file to another .pst file. Solutions should not depend on the EntryID property to be unique unless items will not be moved.
As a workaround you may add a user property to Outlook items with your own generated ID (see UserProperties.Add) and store it in the Excel row along with other human-readable properties.
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.