Friday, 15 February 2013

c# - object of Interop Word Document Class is null on Windows Server 2008 - Word Open method -



c# - object of Interop Word Document Class is null on Windows Server 2008 - Word Open method -

while opening word document , saving on machine working fine, when uploading on server , opening there, going in if (doc == null) block, should not go.

please update question title if not relevant or inquire clarification.

here class:

using system; using system.collections.generic; using system.web; using microsoft.office.interop.word; /// <summary> /// summary description clswordexmanager /// </summary> public class clswordexmanager { public enum extension { webpage = 0 } private static string htmextension { { homecoming ".htm"; } } private static application objwordapp = null; private static object objmissing = system.reflection.missing.value; private static document doc = null; static clswordexmanager() { seek { objwordapp = new application(); } grab (exception ex) { throw ex; } } public static void initializeclass() { objwordapp.visible = false; } private static string open(object strfilepath) { string str = string.empty; seek { objwordapp.visible = false; str += "<br /> word app visiblitly false"; } grab (exception ex) { objwordapp = new application(); str += ex.message; } seek { doc = objwordapp.documents.open(ref strfilepath, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing); str += "<br /> word document opened"; if (doc == null) { // null when upload on windows server 2008 office 2007 installed. str += "<br /> after openging null"; } } grab (exception ex) { close(); objwordapp.visible = false; str += "<br /> word document closed : " + ex.message; } homecoming str; } private static void close() { seek { doc.close(ref objmissing, ref objmissing, ref objmissing); } grab { } } private static string saveas(string filepath, string strfileextension, wdsaveformat objsaveformat) { seek { if (clscommon.isvaliduser()) // impersonating user { filepath = system.io.path.changeextension(filepath, strfileextension); seek { if (doc != null) { object objfilepath = filepath; object objformat = objsaveformat; doc.saveas(ref objfilepath, ref objformat, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing, ref objmissing); } else { filepath += "document value null"; } } grab { filepath += "<br /> saving document throwing expe"; homecoming filepath; } } else { filepath += "<br /> not valid saving file "; } } grab (exception ex) { filepath += ex.message; } { close(); } homecoming filepath; } public static string readwordfile(string strfilepath, extension objextension) { string strfilecontent = "<br /> reading word file not completed"; seek { strfilecontent += open(strfilepath); if (objextension == extension.webpage) { seek { string strnewfilename = saveas(strfilepath, htmextension, wdsaveformat.wdformatfilteredhtml); if (strnewfilename != "") { strfilecontent += strnewfilename + clscommon.readfile(strnewfilename, true); // ignore line read html file. } else { strfilecontent += "file not saved"; } } grab (exception ex) { strfilecontent += ex.message; } } else { close(); } } grab (exception exx) { strfilecontent += exx.message; } homecoming strfilecontent; } public static void quit() { seek { objwordapp.quit(ref objmissing, ref objmissing, ref objmissing); } grab { } } }

creating desktop folder asp.net user might have solved one problem, you'll run many more. word might pop dialog box, , you'll stuck.

office automation explicitly unsupported on server-side, , trust me - you'll have lots of problem:

microsoft not recommend, , not support, automation of microsoft office applications unattended, non-interactive client application or component (including asp, asp.net, dcom, , nt services), because office may exhibit unstable behavior and/or deadlock when office run in environment.

i suggest go on article in link above quote came, , utilize alternatives.

c# ms-word interop windows-server-2008 office-interop

No comments:

Post a Comment