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.
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?
I am trying to attach a PDF from a shared drive to many Outlook emails generated by a VBA code in Excel.
One sheet has all of the email addresses that will have an email created.
The other sheet in the same workbook has inputs such as the subject line, email body text, and PDF location that can be changed based on what the mass email is about.
While the code to make the subject and htmlbody of the email works, the attachment code doesn't work.
Copying the PDF address to a cell in the active worksheet with the lines of email data, I can attach the PDF.
It is when I treat it as an input on the Inputs worksheet that the issue occurs.
Sub Email_New_Patent_Case()
Dim i As Integer
Dim AttorneyCount As Long
'Set the end of the range equal to the last row of attorney data in the sheet
AttorneyCount = WorksheetFunction.CountA(Range("B2:B10"))
For i = 1 To AttorneyCount
If ActiveSheet.Cells(i + 1, 16) > 0 Then
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
.To = ActiveSheet.Cells(i + 1, 10).Value
.CC = ""
'Make sure this cell corresponds with the Subject Line
.Subject = ActiveWorkbook.Worksheets("Inputs").Range("D3")
'Make sure nickname has nickname or first name as value
.HTMLBody = ActiveSheet.Cells(i + 1, 4).Value & ",<br><br>"
'Make sure this cell corresponds with the Body
.HTMLBody = .HTMLBody & ActiveWorkbook.Worksheets("Inputs").Range("D4")
'Make sure this cell corresponds with
' the desired attachment location on the shared drive
.Attachments.Add ActiveWorkbook.Worksheets("Inputs").Range("D5")
.Save
End With
End If
Next i
End Sub
I get an error message:
"Object doesn't support this property or method."
This is a bit ambiguous:
.Attachments.Add ActiveWorkbook.Worksheets("Inputs").Range("D5")
...because Attachments.Add can take either a string representing the full file path or an actual object. In this case Add is unable to tell whether you're trying to add the cell itself, or its Value - seems like it defaults to trying to add the range object instead of reading the cell's default property (Value)
Being more explicit fixes the problem:
.Attachments.Add ActiveWorkbook.Worksheets("Inputs").Range("D5").Value
I am trying to attach a PDF from a shared drive to many Outlook emails
and
.Attachments.Add ActiveWorkbook.Worksheets("Inputs").Range("D5") 'Make sure this cell corresponds with the desired attachment location on the shared drive
The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.
So, you need to copy files on the hard drive first and then attach them passing a local file path.
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!
I created an Excel workbook that "generates" new forms (tabs) based on selections made in the main tab. Background: I have hidden predefined tabs that I created and based on the selections made in the main tab, my VBA code will copy contents from a specific hidden tab and paste contents to a new tab that's automatically created through VBA. The new tab that has the pasted contents also contains a form control (button).
Everything works perfectly fine in Excel 2010 and higher but in Excel 2007 the form button is missing in my pasted contents tab. I cannot find any issues with my code and cannot identify why the button is not getting copied over as well.
Code that checks to see if a sheet exits and if not then it creates a new tab and copies contents from hidden tab to the newly created tab.
What part of my code is not compatible for Excel 2007 and how do I go about fixing this issue?
Sub NewWorkSheetProcess(ByVal b As String)
Dim numLines As Integer
Dim requestType, clientNumber, clientName, processType, screenName, ProcessName
requestType = Sheets("FrontPage").Range("c9")
clientNumber = Sheets("FrontPage").Range("c7")
clientName = Sheets("FrontPage").Range("c5")
processType = Sheets("FrontPage").Range("c11")
screenName = Sheets("FrontPage").Range("c13")
ProcessName = Sheets("FrontPage").Range("c15")
If Not SheetExists(b) Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = b
Sheets("MainProcess").Cells.Copy Worksheets(b).Range("A1")
Worksheets(b).Cells(5, 3).Value = ProcessName
Worksheets(b).Cells(7, 3).Value = clientName
Worksheets(b).Cells(8, 3).Value = processType
Worksheets(b).Cells(6, 3).Value = screenName
Worksheets(b).Cells(9, 3).Value = Worksheets(b).Name
End If
If Worksheets(b).Name = "Modify Process" Then
MsgBox "Please ensure to highlight/notate all required configuration changes. -Thank you!", VbMsgBoxStyle.vbInformation, "Courtesy Message"
End If
End Sub
Update: I tried adding Application.CopyObjectsWithCells = True before my copy and still does not work.
Add
Application.CopyObjectsWithCells = True
before running the Copy