Js使用word書簽填充內容
1.在模板文件中需要填充的地方插入書簽
填充內容為:(|光標所在處)
填寫書簽名,點擊添加完成:
2.使用js打開模板,獲取書簽位置,填充數據:
function printWord(flag){ var word; try{ word = new ActiveXObject("Word.Application"); word.visible = false; }catch(e){ alert("確認是否安裝了OFFICE軟件,是否添加了受信站點,是否將ActiveX控件啟用!"); return ; } try {//打開模板文件 word.Visible = false; var oDocument = word.Documents.open("http://"+document.location.host+"/web/word/DC001.doc"); }catch(Exception) { word.quit(); word = null; return ; } //獲取書簽位置,填充內容 try{ var range = word.Range; range = word.ActiveDocument.Bookmarks(“bookmarkname”).Range; range.InsertBefore(“需要插入的內容”); //書簽后插入內容 //word.Application.Printout();//打印 word.quit(); word=null; }catch(e){ alert("書簽賦值出現錯誤"+e.toString()); word.quit(); word=null; } } printWord() ;
3.完成導出、打印
//word.Application.Printout();//打印