I have an c# console application which create an excel worksheet containing a smartart object with the hiearchy layout(OrgChart). I would like to add hyperlinks to the nodes within the org chart, but somehow i can't.
In the picture below, i would like to add a hyperlink to "Node 1"(1) which will take me to the "LinkedSheet" sheet(2):
with the following code snippet, i tried to add a hyperlink to the sheet "orgChart" where the textFrame of "Node 1"(var name: ndTop) is the anchor and the sheet "LinkedSheet" is the target:
Sheet.Hyperlinks.Add(ndTop.TextFrame2, "", "'" + LinkSheet.Name + "'!A1", "", "");
But i get the following error:
The error translated to english:
'The remote procedure call failed. (Exception from HRESULT: 0x800706BE) '.
There are no inner exception.
I added the following references to my project:
The usings:
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
The Code:
private static Excel.Workbook Wb = null;
private static Excel.Application Xl = null;
private static Excel.Worksheet Sheet = null;
private static Excel.Worksheet LinkSheet = null;
static void Main(string[] args)
{
Xl = new Excel.Application();
Xl.Visible = true;
Wb = Xl.Workbooks.Add();
LinkSheet = Wb.Worksheets[1];
LinkSheet.Name = "LinkedSheet";
Sheet = Wb.Worksheets.Add();
Sheet.Name = "OrgChart";
var myLayout = Xl.SmartArtLayouts[88];
var smartArtShape = Sheet.Shapes.AddSmartArt(myLayout, 50, 50, 200, 200);
if (smartArtShape.HasSmartArt == Office.MsoTriState.msoTrue)
{
Office.SmartArt smartArt = smartArtShape.SmartArt;
Office.SmartArtNodes nds = smartArt.AllNodes;
Office.SmartArtNode ndTop = null;
foreach (Office.SmartArtNode nd in nds)
{
if (nd.Level != 1)
{
nd.Delete();
}
else
{
ndTop = nd;
ndTop.TextFrame2.TextRange.Text = "Node 1";
}
}
//Adding the hyperlink
Sheet.Hyperlinks.Add(ndTop.TextFrame2, "", "'" + LinkSheet.Name + "'!A1", "", "");
Office.SmartArtNode ndLev2_1 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
ndLev2_1.TextFrame2.TextRange.Text = "Node 1.1";
Office.SmartArtNode ndLev2_2 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
ndLev2_2.TextFrame2.TextRange.Text = "Node 1.2";
Office.SmartArtNode ndLev2_3 = ndTop.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
ndLev2_3.TextFrame2.TextRange.Text = "Node 1.3";
Office.SmartArtNode ndLev2_1_1 = ndLev2_1.AddNode(Office.MsoSmartArtNodePosition.msoSmartArtNodeBelow);
ndLev2_1_1.TextFrame2.TextRange.Text = "Node 1.1.1";
}
}
User contributions licensed under CC BY-SA 3.0