Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. Microsoft.Office.Interop.Word.Application worddoc = new
  4. Microsoft.Office.Interop.Word.Application();
  5. worddoc.Visible = false;
  6. object docpath = "c:\test.docx";
  7. object objYes = true;
  8. object objNo = false;
  9. object defaultValue = System.Reflection.Missing.Value;
  10. Document myDoc = worddoc.Documents.Open(ref docpath, ref objNo, ref objNo, ref objNo,
  11. ref defaultValue,
  12. ref defaultValue, ref defaultValue, ref defaultValue,
  13. ref defaultValue, ref defaultValue, ref defaultValue,
  14. ref defaultValue, ref defaultValue, ref defaultValue,
  15. ref defaultValue, ref defaultValue);
  16. if (myDoc.Sections.Count > 0)
  17. {
  18. myDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture("c:\yay.jpg",
  19. ref defaultValue, ref defaultValue, ref defaultValue, ref defaultValue,
  20. ref defaultValue, ref defaultValue, ref defaultValue);
  21. //^this adds an image to the header.
  22. foreach (Section section in myDoc.Sections)
  23. {
  24. Range headerRange= Section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
  25. headerRange.Fields.Add(headerRange, WdFieldType.wdFieldPage);
  26. headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
  27. //^sets alignment to the right, this works fine.
  28. }
  29. myDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text="myTex";
  30. //^this adds my text to the header, but it overwrites the picture, thus deleting it.
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement