Do you have a C# code example showing how to embed an excel file into a word document?
Hi,
Thanks for your request. Yes, Aspose.Words now supports insertion of embedded OLE objects in Microsoft Word document. Please see the following examples:
Insert embedded Excel 97-2003 document as iconic OLE object from stream using predefined image:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Stream memoryStream = File.OpenRead(MyDir + "Book1.xls");
Shape oleObject = builder.InsertOleObject(memoryStream, "Excel.Sheet.8", true, null);
doc.Save(MyDir + @"out.docx");
Insert linked Word document from file as normal OLE object using custom presentation image
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Image image = Image.FromFile(MyDir + "Aspose.Words.png");
Shape oleObject = builder.InsertOleObject(MyDir + "Doc1.docx", true, false, image);
doc.Save(MyDir + @"out.docx");
I hope, this helps.
Best regards,
The above code works when embedding excel into a word doc however it looks like it puts it in excel 97-2003 format (xls). Anyway to have it format the embedded object as xlsx for users with Office 2013? I tried passing in Excel.Sheet.12 and I am getting an error message below which is strange b/c I definitely have office 2013 and excel installed (note I have a third party plugin that needs to be used with this excel workbook as well as the end users so we can’t remove this plugin):
Hi,
Thanks for your inquiry. Please attach your input Word document and Excel file you’re getting this problem with here for testing. We will investigate the issue on our end and provide you more information.
Best regards,
Ok here is my code and I have attached the excel and word document… As I said before I have office 2013 installed and get that error message in the above post every time I click the excel icon in the word doc:
Aspose.Words.Document doc = new Aspose.Words.Document(System.Web.HttpContext.Current.Server.MapPath("~/Docs/test.docx"));
DocumentBuilder docBuilderTest = new DocumentBuilder(doc);
docBuilderTest.MoveToBookmark("BOOKMARK_TEST");
// Instantiate a new workbook
Workbook workBook = new Workbook();
// Open the template
workBook.Open(System.Web.HttpContext.Current.Server.MapPath("~/Docs/Book1.xlsx"));
Worksheet workSheet = workBook.Worksheets[0];
Aspose.Cells.Cells cells = workSheet.Cells;
Aspose.Cells.Cell cell = cells["D3"];
cell.PutValue("TESTER");
MemoryStream memoryStream = workBook.SaveToStream();
Shape oleObject = docBuilderTest.InsertOleObject(memoryStream, "Excel.Sheet.12", true, null);
// Print it
Aspose.Words.Saving.SaveOptions saveOptions = Aspose.Words.Saving.SaveOptions.CreateSaveOptions(clientPK + " CAM Report.docx");
saveOptions.SaveFormat = Aspose.Words.SaveFormat.Docx;
doc.Save(System.Web.HttpContext.Current.Response, clientPK + " CAM Report.docx", Aspose.Words.ContentDisposition.Attachment, saveOptions);
System.Web.HttpContext.Current.Response.End();
Hi,
Thanks for the additional information. I tested your scenario over a standard installation of MS Excel 2013 and was unable to reproduce this issue on my end. I was able to open or edit the excel worksheet object from inside Word document. This doesn’t seem to be an issue with Aspose.Words. Could you please try opening/editing this excel worksheet object on another machine where there is no other third party plugin for Excel is installed and see how it goes?
Best regards,
I copied these codes
Insert linked Word document from file as normal OLE object using custom presentation image
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Image image = Image.FromFile(MyDir + "Aspose.Words.png");
Shape oleObject = builder.InsertOleObject(MyDir + "Doc1.docx", true, false, image);
doc.Save(MyDir + @"out.docx");
and converted into VB
Dim MyDir As String = "C:\Aspose"
Dim doc As New Aspose.Words.Document()
Dim builder As New DocumentBuilder(doc)
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(MyDir & "doc_icon.gif")
Dim oleObject As Shape = builder.InsertOleObject(MyDir & "MyEmbeddedWord.docx", True, False, image)
doc.Save(MyDir & "MyEmbeddedWord-out.docx")
but no output was created.
What’s oleObject for? Thanks
Hi,
Thanks for your inquiry. After an initial test with Aspose.Words for .NET 16.6.0, I was unable to reproduce this issue on my side. I would suggest you please upgrade to the latest version of Aspose.Words. You can download it from the following link. I hope, this helps.
https://releases.aspose.com/words/net/
In case the problem still remains, please attach your MyEmbeddedWord.docx, MyEmbeddedWord-out.docx documents and doc_icon.gif image file here for testing. We will investigate the issue on our end and provide you more information.
Best regards,