How does one programmatically insert a linked item into an Excel spreadsheet?

C

C Duden

How does one programmatically insert a linked item into an Excel
spreadsheet?



From my .NET Addin project, I am able to extract a linked item from a
Worksheet via the Shape and OLEObject objects. But I have been unsuccessful
in my attempt to imbed a linked item into the Worksheet.



I get a reference to the active Worksheet, then I attempt to use its
Shapes.AddOLEObject method, but I can't find any examples of how to use this
method (presuming this is the correct methodology to use to add a linked
item in the first place).



Any help would be appreciated.
 
C

C Duden

Never mind. Here is the solution:

// Get active sheet.
Excel.Application ExcelApp = (Excel.Application)mApplication;
Excel.WorkbookClass Book = (Excel.WorkbookClass)ExcelApp.ActiveWorkbook;
Excel.Worksheet Sheet = (Excel.Worksheet)Book.ActiveSheet;

// Will work for images and Word documents.
string SourceName;
SourceName = @"C:\Documents and Settings\mschippel\My Documents\My
Pictures\graphic_agent.jpg";
//SourceName = @"C:\Documents and Settings\mschippel\My Documents\doc1.doc";

// Link the item.
try
{
Excel.Shape Shp = Sheet.Shapes.AddOLEObject(Type.Missing, SourceName, true,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top