Wednesday, December 02, 2009

Automating MS Word

I was trying to fix a bug related to handling a windows document from ASP.net/C#/Windows Server 2003 program.
Some things I had issues and resolved.
1) Access Denied: Microsoft.Office.Interop.Word
Give access to Network Service to refered dll which should be Microsoft.Office.Interop.Word.dll
2) Removing existing watermark from file programmatically.
doc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;

foreach(Word.Shape s in doc.Application.Selection.HeaderFooter.Shapes ){
Logger.Write(s.Name);
if (s.Name.Length > 24 && s.Name.Substring(0, 24) == "PowerPlusWaterMarkObject") {
s.Delete();
}
}
doc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;


3) Memory issues
You will notice Winword.exe in TaskManager. which opens after each operation.
better refer following discussion at Stackoverflow.
ASP.net C# - WINWORD.exe

No comments: