Code - Send rich text e-mail.

staticbob

Well-known Member
Joined
Oct 7, 2003
Messages
1,079
Guys,

I have this code that uses redemption to send a mail from Excel.

Problem is, I need the savepath (in body of message, bottom of code) to show as a hyperlink in the sent message. This works fine if Outlook Mail format is set to rich text. If its set to plain text or HTML the hyperlink doesn't work.

How can I control the mail format of this message alone in code ? Some of our users have dynamic HTML signatures, I don't want to change the setting in outlook as this will disbale them.

Thanks,
Bob

Code:
Public Sub sendemail(savepath, docname)
On Error GoTo errorlog
'Declarations
    Dim appOutlook As Object
    Dim mi As Object
    Dim Created As Boolean
    Dim safeitem As Object
    
'Generate mail item
    Set myOlApp = CreateObject("Outlook.Application")
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    myNameSpace.Logon
    Set safeitem = CreateObject("Redemption.SafeMailItem") 'Create an instance of Redemption.SafeMailItem
    'Set myFolder = myNameSpace.GetDefaultFolder(olFolderOutbox)
    Set mi = myOlApp.CreateItem(olMailItem)
    safeitem.Item = mi
    'mi.Display
    If Created Then appOutlook.Quit


'Get the names for document type from given range
Select Case docname
    Case "RFI"
        Set rng = Worksheets("email").Range("P4:P25")
    Case "CVI"
        Set rng = Worksheets("email").Range("Q4:Q25")
    Case "SI"
        Set rng = Worksheets("email").Range("R4:R25")
    Case "Call Off"
        Set rng = Worksheets("email").Range("S4:S25")
    Case "Requisition"
        Set rng = Worksheets("email").Range("T4:T25")
    Case "Notice"
        Set rng = Worksheets("email").Range("U4:U25")
    Case Else
        MsgBox "No e-mail addresses"
        Exit Sub
    End Select
    
'Scan through the range and verify each name
    For Each cell In rng.Cells
    If cell.Value <> "" Then
        Set Rcp = safeitem.Recipients.Add(cell.Value)
        'If Not Rcp.Resolve Then
        '    Rcp.Delete
        Else
        End If
    Next cell

'Get info and send mail
    With safeitem.Item
        .subject = Worksheets("directory").Range("B3").Value & " - Workbook Notification"
        .Body = "This message has been generated automatically." & vbNewLine & vbNewLine & _
        "A new " & docname & " has been created in this location. Please click the link to view . . ." & vbNewLine & vbNewLine & _
        "<" & savepath & ">"
    End With
    
    safeitem.send

    Set mi = Nothing
    If Created Then appOutlook.Quit
    Set appOutlook = Nothing

Exit Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I am trying to understand this request to see if it fits my needs can someone interpret this for my in simpe english?

Newbie
:rolleyes:




staticbob said:
Guys,

I have this code that uses redemption to send a mail from Excel.

Problem is, I need the savepath (in body of message, bottom of code) to show as a hyperlink in the sent message. This works fine if Outlook Mail format is set to rich text. If its set to plain text or HTML the hyperlink doesn't work.

How can I control the mail format of this message alone in code ? Some of our users have dynamic HTML signatures, I don't want to change the setting in outlook as this will disbale them.

Thanks,
Bob

Code:
Public Sub sendemail(savepath, docname)
On Error GoTo errorlog
'Declarations
    Dim appOutlook As Object
    Dim mi As Object
    Dim Created As Boolean
    Dim safeitem As Object
    
'Generate mail item
    Set myOlApp = CreateObject("Outlook.Application")
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    myNameSpace.Logon
    Set safeitem = CreateObject("Redemption.SafeMailItem") 'Create an instance of Redemption.SafeMailItem
    'Set myFolder = myNameSpace.GetDefaultFolder(olFolderOutbox)
    Set mi = myOlApp.CreateItem(olMailItem)
    safeitem.Item = mi
    'mi.Display
    If Created Then appOutlook.Quit


'Get the names for document type from given range
Select Case docname
    Case "RFI"
        Set rng = Worksheets("email").Range("P4:P25")
    Case "CVI"
        Set rng = Worksheets("email").Range("Q4:Q25")
    Case "SI"
        Set rng = Worksheets("email").Range("R4:R25")
    Case "Call Off"
        Set rng = Worksheets("email").Range("S4:S25")
    Case "Requisition"
        Set rng = Worksheets("email").Range("T4:T25")
    Case "Notice"
        Set rng = Worksheets("email").Range("U4:U25")
    Case Else
        MsgBox "No e-mail addresses"
        Exit Sub
    End Select
    
'Scan through the range and verify each name
    For Each cell In rng.Cells
    If cell.Value <> "" Then
        Set Rcp = safeitem.Recipients.Add(cell.Value)
        'If Not Rcp.Resolve Then
        '    Rcp.Delete
        Else
        End If
    Next cell

'Get info and send mail
    With safeitem.Item
        .subject = Worksheets("directory").Range("B3").Value & " - Workbook Notification"
        .Body = "This message has been generated automatically." & vbNewLine & vbNewLine & _
        "A new " & docname & " has been created in this location. Please click the link to view . . ." & vbNewLine & vbNewLine & _
        "<" & savepath & ">"
    End With
    
    safeitem.send

    Set mi = Nothing
    If Created Then appOutlook.Quit
    Set appOutlook = Nothing

Exit Sub
 
Upvote 0
Hello staticboy,

The code you utilize controls excel and just sends the data to outlook as far as I can understand. What you want is to add another statement. which will make sure the message is sent as RichText. Right?

Well, by the time outlook determines the format - the case is out of excel VBA's hands.

I would suggest writing a simple VBA code for your outlook and placing it into ThisOutlookSession (Alt+F11 works in Outlook all right).

Just run a logical parameter, assigning RichText format to the message when a certain identifier is available. Under Identifier I am reffering to the message being passed from Excel.

This will solve it. Sorry fot not coming up with a ready code for Outlook.
 
Upvote 0

Forum statistics

Threads
1,215,365
Messages
6,124,512
Members
449,167
Latest member
jrob72684

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top