Document.Protect is not working

LoneWolf

New Member
Hi I have created one asp.net application, in which i am converting asp:panel to the word document by using the below code snippet.\[code\] StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); tblMain.RenderControl(htw); if (strType.Equals("Save")) { string filename = DateTime.Now.ToString().Replace("/", "").Replace("-", "").Replace(":", "").Replace(" ", "") + ".doc"; string strPath = Server.MapPath("Attachments") + "\\" + filename; StreamWriter sWriter = new StreamWriter(strPath); sWriter.Write(sw.ToString()); sWriter.Close();\[/code\]Now the file is saving to the specified folder. after that i am fetching the same file for appending password for the protection. And i used the below code for the same.\[code\]Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document doc = null; object missing = System.Reflection.Missing.Value; object readOnly = false; object visible = true; object password = "123"; object fileToOpen = strPath; try { doc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref password, ref missing, ref missing, ref missing, ref visible, ref visible, ref missing, ref missing, ref missing); doc.Activate(); doc.Protect(WdProtectionType.wdAllowOnlyComments,ref missing, ref password, ref missing, ref missing); doc.SaveAs(ref fileToOpen, ref missing, ref missing, ref password, ref missing, ref password, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } catch (Exception ex) { // exception catching } finally { doc.Close(ref missing, ref missing, ref missing); wordApp.Quit(ref missing, ref missing, ref missing); }\[/code\]But it will save the file along with one folder named as filename_files.If i Remove the line \[code\]doc.Protect(WdProtectionType.wdAllowOnlyComments,ref missing, ref password, ref missing, ref missing);\[/code\]it will save the doc file only, but no password appended.Any idea? Please help me to resolve this issue. All the suggestions are invited.Thanks in advance..
 
Back
Top