Saturday, June 20, 2009

Add Image to Word document using C# code

Hi everyone,
i'd like to add an image/logo to word document automatically via c# code.
i used with Microsoft.Office.Interop.Word.Document object to open the word documents,
and then i used the Shapes.AddPicture method for inserting the image into the documents.

(I didn't use the InlineShapes.AddPicture method because, these method doesn't support layers etc.)

here is my code:
object FileName = filesCollection.ToString();
object saveFileName = FileName;
object vk_read_only = false;
object vk_visibile = true;
object vk_false = false;
object vk_true = true;
object vk_dynamic = 2;
object vk_missing = System.Reflection.Missing.Value;
object LinkToFile = true;
object myRange = System.Reflection.Missing.Value;
object vk_format = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatDocument;
object Left = 1;
object Top = 1;
object Width = 498;
object Height = 89;

oWord.Visible = true;
oWord.Activate();

Microsoft.Office.Interop.Word.Document doc = oWord.Documents.Open(ref FileName, ref vk_false, ref vk_read_only,
ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_visibile, ref vk_true, ref vk_missing, ref vk_missing, ref vk_missing);
doc.Shapes.AddPicture(LogoPath, ref LinkToFile, ref saveWithObject, ref Left, ref Top, ref Width, ref Height, ref vk_missing);
doc.SaveAs(ref FileName, ref vk_format, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing, ref vk_missing, ref vk_missing);
doc.Close(ref SaveChanges, ref OriginalFormat, ref saveWithObject);

No comments:

Post a Comment