tags:

views:

300

answers:

2

Hi,

Can anybody tell me, how can we add hyperlink in Excel[2007] to one of its worksheet?

i.e. from one tab to another tab [Within excel] .

Thanks, Amit

A: 

What you want to use here is the Hyperlinks.Add method.

You can call it with code that looks something like this:

Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
Excel.Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
string hyperlinkTargetAddress = "Sheet2!A1";

worksheet.Hyperlinks.Add(
    rangeToHoldHyperlink,
    string.Empty,
    hyperlinkTargetAddress,
    "Screen Tip Text",
    "Hyperlink Title");

Here is a full automation example that you can test:

void AutomateExcel()
{
    Excel.Application excelApp = new Excel.Application();
    excelApp.Visible = true;

    Excel.Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
    workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
    Excel.Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
    string hyperlinkTargetAddress = "Sheet2!A1";

    worksheet.Hyperlinks.Add(
        rangeToHoldHyperlink,
        string.Empty,
        hyperlinkTargetAddress,
        "Screen Tip Text",
        "Hyperlink Title");

    MessageBox.Show("Ready to clean up?");

   // Cleanup:
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

    Marshal.FinalReleaseComObject(range);

    Marshal.FinalReleaseComObject(worksheet);

    workbook.Close(false, Type.Missing, Type.Missing);
    Marshal.FinalReleaseComObject(workbook);

    excelApp.Quit();
    Marshal.FinalReleaseComObject(excelApp);
}

Hope this helps!

Mike

Mike Rosenblum
A: 

Very easy.

  1. Put some text on the cell (let's say that you put "Click here to see Financials" at cell A1).
  2. Right click on the cell and select "Hyperlink..."
  3. Select "Place in This Document", and then choose one of the tabs at the list on the right.
luiscolorado