用javascript操作word文档
1、 向word中写入内容
首先在word中设置书签,如书签名为bookmark,javascript中可以这样写
var word;
word = new ActiveXObject("Word.Application");
var range = word.Range;
word.Visible = true;
var path = "filepath";
word.Documents.Open(path);
range = word.ActiveDocument.Bookmarks("bookmark").Range;
range.InsertBefore("哈哈哈哈哈哈"); //书签后插入内容
//-----以下代码段附加保护及取消保护文档功能----
//保护文档,书签处可编辑
range.select();//选定书签内容
var psw='123'
word.ActiveDocument.BookMarks("bookmark").Range.Editors.Add(-1); //常量:wdEditorEveryone=-1
word.ActiveDocument.Protect(3,false,psw,false,false);//常量:wdAllowOnlyReading=3
//取消保护文档
word.ActiveDocument.Unprotect(psw);
//-----------------------end--------------
word = new ActiveXObject("Word.Application");
var range = word.Range;
word.Visible = true;
var path = "filepath";
word.Documents.Open(path);
range = word.ActiveDocument.Bookmarks("bookmark").Range;
range.InsertBefore("哈哈哈哈哈哈"); //书签后插入内容
//-----以下代码段附加保护及取消保护文档功能----
//保护文档,书签处可编辑
range.select();//选定书签内容
var psw='123'
word.ActiveDocument.BookMarks("bookmark").Range.Editors.Add(-1); //常量:wdEditorEveryone=-1
word.ActiveDocument.Protect(3,false,psw,false,false);//常量:wdAllowOnlyReading=3
//取消保护文档
word.ActiveDocument.Unprotect(psw);
//-----------------------end--------------
2、把word文件转成html文件
<script language=javascript>
function saveword(){
var oWordApp=new ActiveXObject("Word.Application");
var oDocument=oWordApp.Documents.Open("C:\\doc2html\\x.doc");
oDocument.SaveAs("C:\\test.htm", 8)
oWordApp.Quit();
}
</script>
</HEAD>
<BODY>
Click the "save" button to save the file "C:\test.doc" to "C:\test.htm":
<input type=button onclick="saveword()" value=save>
</BODY>
</HTML>
function saveword(){
var oWordApp=new ActiveXObject("Word.Application");
var oDocument=oWordApp.Documents.Open("C:\\doc2html\\x.doc");
oDocument.SaveAs("C:\\test.htm", 8)
oWordApp.Quit();
}
</script>
</HEAD>
<BODY>
Click the "save" button to save the file "C:\test.doc" to "C:\test.htm":
<input type=button onclick="saveword()" value=save>
</BODY>
</HTML>