• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Convert Hyperlink Function to Embedded Link

cdbauer1

New Member
Hi all,

I'm using Ron De Bruin's Excel > Outlook method for converting worksheet data into an Outlook message. One of the struggles I'm encountering is that any hyperlinks created using the =HYPERLINK function are automatically removed (though they retain formatting). Only embedded Hyperlinks (Insert > Hyperlink) work.

Does anyone have any code to to take the =HYPERLINK function and convert it to an embedded Hyperlink?

Thanks!
 
Here is the function from Ron de Bruin to convert the Excel range to HTML:

Code:
Function RangetoHTML(rng As Range)
    ' Changed by Ron de Bruin 28-Oct-2006
    ' Working in Office 2000-2010
    Dim FSO As Object
    Dim ts As Object
    Dim RowDifference As String
    Dim TempFile As String
    Dim HomeWB As Workbook
    Dim TempWB As Workbook
    Dim lngLastRow As Long
    Dim rngConsider As Range
    Dim lngRows As Long
    Dim lngVisibleRows As Long
    Dim Offset As Integer
    Dim lngHiddenRows As Long
    Dim i As Long
    Dim RowTotal As Long

    Dim HyperL As Hyperlink
    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to paste the data in
    rng.Copy
    'Debug.Print rng.Address
    Set TempWB = Workbooks.Add(1)
  
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
    'Calculate how far from the top the row is (this is used to offset hyperlinks because we paste into the first cell of the temporary workbook which shifts everything from the original sheet up when pasted into the temp sheet
    RowDifference = (rng.Cells(1, 1).Row - Cells(1, 1).Row) * -1
    '~~~~~~~~~~~~~~~~~~~~~~~~~~CONVERT AND SHIFT HYPERLINKS~~~~~~~~~~~~~~~~~~~~~~~
    For Each HyperL In rng.Hyperlinks
        TempWB.Sheets(1).Hyperlinks.Add _
            Anchor:=TempWB.Sheets(1).Range(HyperL.Range.Address).Offset(RowDifference), _
            Address:=HyperL.Address, _
            TextToDisplay:=HyperL.TextToDisplay
    Next HyperL
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set ts = FSO.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set FSO = Nothing
    Set TempWB = Nothing
End Function

This is used to generate the signature:
Code:
Function GetBoiler(ByVal sFile As String) As String
http://www.rondebruin.nl/win/s1/outlook/signature.htm
    Dim FSO As Object
    Dim ts As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set ts = FSO.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.ReadAll
    ts.Close
End Function

This is the actual code to generate the e-mail:
Code:
Sub Emails_Welcome()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2013
    Dim rng As Range
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim K As String
    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection. Change the range between the quotations below to update where the body of the email is pulling from
    Set rng = Range("Email_Templates_Welcome_Body").SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

  
    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)
'Change only Mysig.htm to the name of your signature. For Windows 7/8 you can find the default path here (make sure you can view hidden files) C:\Users\<UserName>\AppData\Roaming\Microsoft\Signatures
    SigString = Environ("appdata") & _
                "\Microsoft\Signatures\My Signature.htm"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If
    On Error Resume Next
    With OutMail
    'Update the Range in Quotes in order to update the cell containing the addresses to place in the To line, Subject line, etc.
        .To = Range("Email_Templates_Welcome_To")
        .CC = Range("Email_Templates_Welcome_CC")
        .BCC = Range("Email_Templates_Welcome_BCC")
        .Subject = Range("Email_Templates_Welcome_Subject")
        .HTMLBody = RangetoHTML(rng) & "<br>" & Signature 'formats the body of the e-mail using the RangetoHTML function above
        .Display   'or use .Send to not view the e-mail before sending and just send automatically
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
  
    Range("Switch_Welcome_to_CUNA_E_mail").Value = "Yes"

  
End Sub
 
I'm using a range in my workbook - are you saying you want me to use the HTML codes for links within my worksheet in the hopes that when the sheet is fed through the RangeToHTML function it will convert properly?
 
Unfortunately, I'm not allowed to share the workbook outside of my company. My expectation was that this was a generic enough request to accommodate without the sample.
 
Sorry, I am quite busy on some projects.But i must look on the same too.
If you share just a sample file then it will help me to trouble out you easily & early too.
 
Back
Top