Programmatically Embedding another file to the word document

Hi

Many a times we want to embed some document in the word file, with the help of Insert-> File-> Select File menu item. This will copy the content of the file to the word document. This can also be done programmatically. 

String LocationOfFileToMerge = "C:\\Myfile.doc";

range.InsertFile(LocationOfFileToMerge,ref oMissing, ref oFalse, ref oFalse, ref oFalse);

We can also embed the document in the word file instead of copying the text of the document. In this case you can embed any kind of document.


Object NameForLabel = "File Name";

 

//Icon To Be Shown For Label
Object IconFileName = "C:\\IconFile.ico";

 

String LocationOfFileToMerge = "C:\\Myfile.doc";

Object ClassType = "Word.Document.8";

Object True = true;

Object False = false;

Object Missing = System.Reflection.Missing.Value;

 

range.InlineShapes.AddOLEObject(

    ref ClassType,ref LocationOfFileToMerge,ref False, ref True, ref IconFileName,

    ref Missing,ref NameForLabel, ref Missing);

Vikram

Share this post   Email it

Feedback

Posted on 10/17/2008 9:11:36 AM

I am trying to open a 2007 template (.dotx) file, replace some bookmarks, and same the file as a 2007 Word document (.docx). I am able to do this fine with 2003 components (.dot & .doc) but I have been unable to save in 2007 formats. I have use these two lines to specify format before saving:

object Fileformat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;

myDoc.SaveAs(ref newFileName, ref Fileformat, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null);

I get a document that looks like a docx - Word 2007 document, but I get error messages when tying to open it.

Does anyone have any insight into this?

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2010 Vikram Lakhotia