Background:
I am supposed to create a dashboard(in excel) using vbscript and send it over an email(outlook) to someone. I already have a template for the Dashboard(excel). My script opens up that template excel, updates the Excel with today's results and some hyperlinks to some new files, creates an excel mail envelope and sends it to someone.
ISSUE:
My script adds 2 hyperlinks in the dashboard excel. 1st link points to a mht File and 2nd link points to another excel File. I sent the mail to myself and opened it. After opening the email, when I click on the 1st link(to mht file), it works fine and opens up IE for displaying mht file contents. Issue is with the 2nd hyperlink which points to an excel file. It is not Opening the excel file(I have made sure that the link is correct).
Is there something I need to change in my code or Is there any outlook setting which will allow me to open up the link to an excel file directly?
CODE:
Option Explicit
Dim objXL, objXb, objXs, strXlPath, strMHTLink, strExcelLink
'Dashboard Excel template
strXlPath = "C:\Users\Gurman\Work\Misc\Excel Hyperlink in Outlook\test.xlsx"
'Link to MHT file
strMHTLink = "C:\Users\Gurman\Work\Misc\Excel Hyperlink in Outlook\MHTFile.mht"
'Link to another Excel File
strExcelLink = "C:\Users\Gurman\Work\Misc\Excel Hyperlink in Outlook\EXLFile.xlsx"
'Opening an excel(strXlPath) and updating it's Sheet1. In Sheet1, I will be adding 2 hyperlinks.
'1st link for the MHT file(strMHTLink)
'2nd link for another Excel File(strExcelLink)
set objXl = CreateObject("excel.application")
objXl.visible = true
objXl.displayAlerts = false
set objXb = objXl.Workbooks.Open(strXlPath)
set objXs = objXb.Sheets("Sheet1")
objXs.Cells(2,1) = "=HYPERLINK("""&strMHTLink&""",""MHTLINK"")" 'When clicked, it should open IE to display mht file contents
objXs.Cells(2,2) = "=HYPERLINK(""["&strExcelLink&"]Sheet1!A1"",""EXLLINK"")" 'When clicked, it should open Sheet1 of EXLFile.xlsx
'Saving the Workbook
objXb.save
'Mailing this excels contents using Mail Envelope
objXb.EnvelopeVisible = true
With objXs.MailEnvelope.Item
.to = "xxxxxxxx@xxxxxx.xxx" 'contains an email address
.subject = "Today's Hyperlinks"
.attachments.add strXlPath 'attaches the saved dashboard in email
.send
End With
objXb.Close
objXl.Quit
set objXs = Nothing
set objXb = Nothing
set objXl = Nothing
Screenshots:
This is the email which I get. You can see, it has 2 links and an excel file as attachment(This attached excel is my dashboard(test.xlsx). It's contents are same the contents of this email body)
In email body, when I click on mht link, it opens up the IE as expected:
But when I click on the Excel's link, nothing opens up. Even the link color doesn't change on clicking:
How I made sure that excel hyperlink is correct?
This email has an excel attached which has the same contents as email body. I opened up that excel and then clicked on the hyperlink. Both hyper links works as shown below. But I want to open both the links from the outlook's email body directly(not this way).
I have tried to provide as much information as I could. Please let me know if any other information is required.
UPDATE 1:
Here is the Email body source code as requested by @garbb
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=us-ascii"> <meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 14"> <link rel=File-List href="cid:filelist.xml@01D36E74.B4504EE0"> <link rel=OLE-Object-Data href="cid:oledata.mso"> <style> <!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";} .xl15 {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:black; font-size:11.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:Calibri, sans-serif; mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom; mso-background-source:auto; mso-pattern:auto; white-space:nowrap;} .xl65 {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:blue; font-size:11.0pt; font-weight:400; font-style:normal; text-decoration:underline; text-underline-style:single; font-family:Calibri, sans-serif; mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom; mso-background-source:auto; mso-pattern:auto; white-space:nowrap;} --> </style> <!--[if gte mso 9]><xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name>Sheet1</x:Name> <x:WorksheetOptions> <x:DefaultRowHeight>300</x:DefaultRowHeight> <x:Selected/> <x:Panes> <x:Pane> <x:Number>3</x:Number> <x:ActiveRow>8</x:ActiveRow> <x:ActiveCol>4</x:ActiveCol> </x:Pane> </x:Panes> <x:ProtectContents>False</x:ProtectContents> <x:ProtectObjects>False</x:ProtectObjects> <x:ProtectScenarios>False</x:ProtectScenarios> </x:WorksheetOptions> </x:ExcelWorksheet> </x:ExcelWorksheets> <x:WindowHeight>8010</x:WindowHeight> <x:WindowWidth>14805</x:WindowWidth> <x:WindowTopX>240</x:WindowTopX> <x:WindowTopY>105</x:WindowTopY> <x:HasEnvelope/> <x:ProtectStructure>False</x:ProtectStructure> <x:ProtectWindows>False</x:ProtectWindows> </x:ExcelWorkbook> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="3073" /> </xml><![endif]--> </head> <body link=blue vlink=purple> <table border=0 cellpadding=0 cellspacing=0 width=183 style='border-collapse: collapse;table-layout:fixed;width:138pt'> <col width=89 style='mso-width-source:userset;mso-width-alt:3254;width:67pt'> <col width=94 style='mso-width-source:userset;mso-width-alt:3437;width:71pt'> <tr height=20 style='height:15.0pt'> <td height=20 class=xl15 width=89 style='height:15.0pt;width:67pt'>Link_To_MHT</td> <td class=xl15 width=94 style='width:71pt'>Link_To_Excel</td> </tr> <tr height=20 style='height:15.0pt'> <td height=20 class=xl65 style='height:15.0pt'><a href="file:///C:\Users\Gurman\Work\Misc\Excel%20Hyperlink%20in%20Outlook\MHTFile.mht">MHTLINK</a></td> <td class=xl65><a href="%5bC:/Users/Gurman/Work/Misc/Excel%20Hyperlink%20in%20Outlook/EXLFile.xlsx%5dSheet1!A1">EXLLINK</a></td> </tr> <![if supportMisalignedColumns]> <tr height=0 style='display:none'> <td width=89 style='width:67pt'></td> <td width=94 style='width:71pt'></td> </tr> <![endif]> </table> </body> </html>
UPDATE 2
This is how both the HyperLink formulas are displayed in Dashboard Excel cells:
MHTLINK(working from within dashboard excel):
=HYPERLINK("C:\Users\Gurman\Work\Misc\Excel Hyperlink in Outlook\MHTFile.mht","MHTLINK")
EXLLINK(working from within the dashboard excel):
=HYPERLINK("[C:\Users\Gurman\Work\Misc\Excel Hyperlink in Outlook\EXLFile.xlsx]Sheet1!A1","EXLLINK")
In Outlook, as shown in the email source in Update 1, the links are:
MHTLINK(working from email body):
<a href="file:///C:\Users\Gurman\Work\Misc\Excel%20Hyperlink%20in%20Outlook\MHTFile.mht">MHTLINK</a>
EXLLINK(NOT Working from email body):
<a href="%5bC:/Users/Gurman/Work/Misc/Excel%20Hyperlink%20in%20Outlook/EXLFile.xlsx%5dSheet1!A1">EXLLINK</a>
UPDATE 3(ISSUE ROOT CAUSE)
In my vbscript code, I have the line:
objXs.Cells(2,2) = "=HYPERLINK(""["&strExcelLink&"]Sheet1!A1"",""EXLLINK"")"
This actually creates a hyperlink to Sheet1 of an Excel Book. The formula which gets written within this cell is also correct which is =HYPERLINK("[C:\Users\Gurman\Work\Misc\Excel Hyperlink in Outlook\EXLFile.xlsx]Sheet1!A1","EXLLINK")
. It is important for me to add Sheet1!A1
in this formula as I want the hyperlink to point to a particular sheet(Sheet1 in this case) of the workbook
THE ISSUE COMES WHEN THIS EXCEL FORMULA IS TRANSFORMED TO A LINK IN THE OUTLOOK MAIL, its value becomes:
<a href="%5bC:/Users/Gurman/Work/Misc/Excel%20Hyperlink%20in%20Outlook/EXLFile.xlsx%5dSheet1!A1">EXLLINK</a>
The above link seems to be Incorrect as it has some HEX values like %5b
, %5d
etc. Hence, it is not working directly.
If I change my VBScript code to create a hyperlink to the Excel Workbook only rather than creating a hyperlink to its "Sheet1", it works fine and I can also open the excel workbook link directly from the email.
Updated line in VBscript:
objXs.Cells(2,2) = "=HYPERLINK("""&strExcelLink&""",""EXLLINK"")"
In Email source, it got transformed to:
<a href="file:///C:\Users\Gurman\Work\Misc\Excel%20Hyperlink%20in%20Outlook\EXLFile.xlsx">EXLLINK</a>
I am able to open the excel workbook using this link directly from the email. But the flaw with this approach is that it may or may not open up Sheet1 in this case. As soon as someone clicks on the hyperlink, may be, Sheet2 opens up instead of Sheet1. Hence, I wanted to handle this thing in my code itself.
So, my question/issue still remains unsolved:
Is it possible to have a hyperlink in Outlook's email body which opens up a particular sheet of an excel workbook?