PDA

View Full Version : Solved: Using hyperlinks in excel -> outlook email



gemsera
08-13-2008, 08:19 AM
Hi All,

I am automating the creation of a daily report email, which all seems to work ok (brings up new email with all the right information) however the one problem I have is I have a link to an intranet site, which doesnt activate as a link in the email (just plain text).

The further difficulty is that it links to the file the macro uploaded earlier, which is a variable filename (Report (date).xls) which makes it quite hard. any pointers would be great, i've read all of Ron De Bruins work(which I am using) looking for tips to no avail.

I have also tried changing the outlook properties to use plain text or html neither works.


Sub Email()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2007
Dim Rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim SigString As String
Dim Signature As String
Dim StrBody As String


Set Rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set Rng = Sheets("Working").Range("A1:G30").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

SigString = "C:\Documents and Settings\username\Application Data\Microsoft\Signatures\testsig.htm"

If dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "abc@ssss.com"
.CC = ""
.BCC = ""
.Subject = "Daily Report"
.HTMLBody = StrBody & RangetoHTML(Rng)
'Add Attachments
'.Attachments.Add ("c:test.txt")
.display 'or use .Send
End With
On Error GoTo 0

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

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Function RangetoHTML(Rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2007
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

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

'Copy the range and create a new workbook to past the data in
Rng.Copy
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

'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

Function GetBoiler(ByVal sFile As String) As String
'Dick Kusleika
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

gemsera
08-19-2008, 06:51 AM
As it turns out, this happens because when inserting text into a cell from a vba string, it doesnt automatically convert it into a html hyperlink.

Still looking for a resolution to do this through code. (somehow double click on the cell, and hit enter to convert it to hyperlink.

gemsera
08-19-2008, 08:10 AM
I found a workaround: I insert the variable into say A5
I save my template with the following in B5:
=HYPERLINK(A5)
Seems to work.