Consulting

Results 1 to 4 of 4

Thread: how to add hyperlink into the body of an email via VBA?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Apr 2021
    Posts
    45
    Location

    how to add hyperlink into the body of an email via VBA?

    Hello,

    I have a code that sends out emails to stakeholders attaching an excel file. I want to include a URL in the emails to direct stakeholders to one of our policies on our intranet. Ideally it would show the link as a text such as 'here' and not the file path e.g. 'the policy can be found on the intranet here'. I have tried to find a way of doing this but have been unsuccessful, can anyone help please?

    Here is the code:

    Sub ClientUnder()
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim Destwb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim OutApp As Object
        Dim OutMail As Object
    'indicates whether action is shown on scree (like excel pop up)
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
    
        Set Sourcewb = ActiveWorkbook
    
    
        'Copys the ActiveSheet to a new workbook
        Sheet7.Copy
        Set Destwb = ActiveWorkbook
    
    
        'Determines the Excel version and file extension/format
        With Destwb
            If Val(Application.Version) < 12 Then
                'You use Excel 97-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2016
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End With
    
    
        '    'Changes all cells in the worksheet to values
            With Destwb.Sheets(1).UsedRange
                .Cells.Copy
                .Cells.PasteSpecial xlPasteValues
                .Cells(1).Select
            End With
            Application.CutCopyMode = False
    
    
        'Saves the new workbook, Mails it then Deletes it
        TempFilePath = Environ$("temp") & "\"
        TempFileName = Sheet7.Name & " " & Sourcewb.Name
    
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
    
        With Destwb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
            'use <br></br> for new line, put text inbetween and & _ @ the end as it indicates that the command isnt finished
                .to = Sheet7.Range("M4")
                .CC = Sheet93.Range("R1")
                .BCC = Sheet93.Range("R2")
                .Subject = Sheet7.Range("A5") & " " & Sheet7.Range("A7") & " " & Sheet7.Range("A6")
                .htmlBody = Sheet93.Range("A6").Value & " " & Sheet7.Range("A8").Value & ", " & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A8").Value & " " & Sheet7.Range("G13") & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A10").Value & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A12").Value & "<br></br>" & "<br></br>" & _
                "<br><u><b>Included in Budget</b></u></br>" & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A16").Value & "<br></br>" & _
                Sheet93.Range("A17").Value & "<br></br>" & _
                Sheet93.Range("A18").Value & "<br></br>" & _
                Sheet93.Range("A19").Value & "<br></br>" & _
                Sheet93.Range("A20").Value & "<br></br>" & _
                Sheet93.Range("A21").Value & "<br></br>" & _
                Sheet93.Range("A22").Value & "<br></br>" & "<br></br>" & _
                "<br><u><b>Not Included in Budget</b></u></br>" & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A26").Value & "<br></br>" & _
                Sheet93.Range("A27").Value & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A29").Value & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A31").Value & "<br></br>" & _
                Sheet93.Range("A32").Value & "<br></br>" & _
                Sheet93.Range("A33").Value & "<br></br>" & _
                Sheet93.Range("A34").Value & "<br></br>" & _
                Sheet93.Range("A35").Value & "<br></br>" & _
                Sheet93.Range("A36").Value & "<br></br>" & _
                Sheet93.Range("A37").Value & "<br></br>" & "<br></br>" & Sheet93.Range("A39").Value & "<br></br>" & "<br></br>" & _
                Sheet93.Range("A41").Value & "<br></br>" & Sheet93.Range("A42").Value & "<br></br>" & "<br></br>" & "<br></br>" & Sheet93.Range("A45").Value & "<br></br>" & Sheet93.Range("A46").Value & _
                eMsg
                .Attachments.Add Destwb.FullName
                'You can add other files also like this
                '.Attachments.Add ("C:\test.txt")
                .Send   'or use .Display
            End With
            On Error GoTo 0
            .Close savechanges:=False
        End With
        'Delete the file you have send
        Kill TempFilePath & TempFileName & FileExtStr
    
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Last edited by Ray707; 04-07-2021 at 09:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •