Saturday, April 3, 2010

Creating hyperlinks in Excel file with VBScript

Creating hyperlinks in Excel file with VBScript

Below code opens a file called 1.xlsx (which is on the C:\ drive) and makes the text written in cell A1 as hyperlink. When you click on that hyperlink it goes to Google.com

Set O_EXCEL = WScript.CreateObject("Excel.Application")
O_EXCEL.Visible = TRUE
O_EXCEL.Workbooks.Open "C:\1.xlsx"
O_EXCEL.Workbooks(1).Activate
O_EXCEL.Workbooks(1).Worksheets(1).Range("A1").Select
LINK_TO = "http://www.google.com"
O_EXCEL.Workbooks(1).Worksheets(1).Hyperlinks.Add O_EXCEL.Selection, LINK_TO
O_EXCEL.Workbooks(1).Save
O_EXCEL.Quit
set O_EXCEL = nothing