Microsoft. Office. InterOP. Word. Application APP = new Microsoft. Office. InterOP. Word. Application (); // template file string templatefile = server. mappath ("~ /Wordtemplate/"+ templater); // templatefile = server. mappath ("~ /Upload/testword/testprint.doc "); // The generated new file string filename = server. mappath ("~ /Wordtemplate/document/"+ datetime. now. tostring ("yyyymmddhhmmssfffff") + ". doc "); // @" C: \ Documents ents and Settings \ Administrator \ Desktop \ "+ datetime. now. tostring ("yyyymmddhhmmssfffff") + ". doc "; string fname = datetime. now. tostring ("yyyymmddhhmmssfffff") + ". doc "; filename = server. mappath ("~ /Wordtemplate/document/"+ fname); // copy the template file to the new file. copy (templatefile, filename); Microsoft. office. interOP. word. document Doc = new Microsoft. office. interOP. word. document (); object obj_filename = filename; object visible = false; object missing = system. reflection. missing. value; // open the file Doc = app. documents. open (ref obj_filename, ref missing, ref readonly, ref missing, ref missing, ref missin G, ref missing, ref visible, ref missing, ref missing); Doc. activate (); # region generates wrod if (type = "1") {foreach (Microsoft. office. interOP. word. bookmark BM in Doc. bookmarks) {If (BM. name = "banner") {BM. select (); BM. range. TEXT = This. gbanner. innerhtml;} else if (BM. name = "content") {BM. select (); BM. range. TEXT = T His. gcontent. innerhtml;} else if (BM. name = "Foote") {BM. select (); BM. range. TEXT = This. lblgdate. innerhtml;} else if (BM. name = "right") {BM. select (); BM. range. TEXT = "case: MS-0" + this. glblnum. innerhtml + "letter: 0" + this. glblhon. innerhtml;} else if (BM. name = "tal") {BM. select (); BM. range. TEXT = "contact number of the Contractor:" + this. lbllawyertal. innerhtml. trim () ;}} else {foreach (Microsoft. office. interOP. Word. bookmark BM in Doc. bookmarks) {If (BM. name = "banner") {BM. select (); BM. range. TEXT = This. sbanner. innerhtml;} else if (BM. name = "content") {BM. select (); BM. range. TEXT = This. scontent. innerhtml;} else if (BM. name = "Foote") {BM. select (); BM. range. TEXT = This. lblsdate. innerhtml;} else if (BM. name = "right") {BM. select (); BM. range. TEXT = "case: MS-0" + this. lblsnum. innerhtml + "letter: 0" + This. lblhonnum. innerhtml ;}}// Doc. printout (ref missing, ref missing, // ref missing, // ref missing, ref missing, ref missing, ref missing, ref missing, // ref missing, ref missing); object issave = true; Doc. close (ref issave, ref missing, ref missing); // app. quit (ref issave, ref missing, Ref missing); try {fileinfo downloadfile = new fileinfo (hostingenvironment. applicationphysicalpath + "\ wordtemplate \ document \" + fname); response. clear (); response. clearheaders (); response. buffer = false; response. contenttype = "application/octet-stream"; response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (fname, system. text. encoding. utf8); response. AP Pendheader ("Content-Length", downloadfile. length. tostring (); response. writefile (downloadfile. fullname); response. flush (); response. end () ;} catch (exception ex) {Throw ex ;}// response. write ("<script language = 'javascript '> alert ('word generation successful! '); </SCRIPT> "); # endregion