如题,上面让做个功能,让关键词在指定的WORD文档中高亮显示我知道用 Word.Application 可以插入内容,替换内容,但高亮怎么弄。。
改变背景颜色?怎么改?下面附上插入和替换的代码
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>HighLight</title>
</head>
<script language="javascript">
function highlight(){
var wdapp = null;
try {
wdapp = new ActiveXObject ("Word.Application");
wdapp.visible = true;
} catch(e) {
alert('不支持 ActiveXObject');
return false;
} var path = "c:/test.doc";
var wddoc = wdapp.documents.open(path);
// 插入
var range = wddoc.range(3, 10);
range.insertbefore('hello world!');
// 插入网页内容
var sel = div1.document.body.createTextRange();
sel.select();
div1.document.execCommand('Copy');
sel.moveEnd('character');
wdapp.selection.Paste(); // 替换
var keyword = "[title]";
var newstr = "这是标题";
wdapp.selection.find.execute(keyword, false, false, false, false, false, true, 1, true, newstr, 2);
}
</script>
<body>
<div id="div1">
<label style="background-color:yellow;">新内容</label>
</div>
<input type="button" name="submit" value="按钮" onclick="highlight()">
</body>
</html>
改变背景颜色?怎么改?下面附上插入和替换的代码
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>HighLight</title>
</head>
<script language="javascript">
function highlight(){
var wdapp = null;
try {
wdapp = new ActiveXObject ("Word.Application");
wdapp.visible = true;
} catch(e) {
alert('不支持 ActiveXObject');
return false;
} var path = "c:/test.doc";
var wddoc = wdapp.documents.open(path);
// 插入
var range = wddoc.range(3, 10);
range.insertbefore('hello world!');
// 插入网页内容
var sel = div1.document.body.createTextRange();
sel.select();
div1.document.execCommand('Copy');
sel.moveEnd('character');
wdapp.selection.Paste(); // 替换
var keyword = "[title]";
var newstr = "这是标题";
wdapp.selection.find.execute(keyword, false, false, false, false, false, true, 1, true, newstr, 2);
}
</script>
<body>
<div id="div1">
<label style="background-color:yellow;">新内容</label>
</div>
<input type="button" name="submit" value="按钮" onclick="highlight()">
</body>
</html>
sel.Font.Color= "#ff0000"
对 Word.Application 实在不熟悉最好改背景颜色,字体颜色也能凑合
http://topic.csdn.net/u/20100608/10/209d9c73-ccc7-44e4-9e1a-6198592eeec7.html
但唉是一个一个替换的,求解决办法
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>HighLight</title>
</head>
<script language="javascript">
function highlight(keyword){
var wdapp = null;
try {
wdapp = new ActiveXObject ("Word.Application");
wdapp.visible = true;
} catch(e) {
alert('不支持 ActiveXObject 或未安装 WORD');
return false;
} var path = "c:/test.doc";
var wddoc = wdapp.documents.open(path);
wdapp.Selection.Find.Text = keyword;
while(wdapp.Selection.Find.Execute()){
wdapp.Selection.Range.Shading.BackgroundPatternColorIndex = 6;
}
}
</script>
<body>
<input type="button" name="submit" value="按钮" onclick="highlight('[title]')">
</body>
</html>