再说两句






4.53/5 (11投票s)
2006年1月5日
7分钟阅读

85506

699
了解如何将MS Word功能集成到你的JScript/WSH脚本解决方案中。
引言
自动化技术在你需要以编程方式创建、编辑或以某种方式控制各种文档时非常有用。主要问题是,大多数与Office脚本相关的文档都是用Visual Basic或WordBasic编写的。对于不熟悉该主题的人来说,本文可以被视为MS Word自动化的快速入门;对于其他人来说,本文是一个简短的过渡参考。
本文假定您熟悉JScript。
通用操作指南
- 开始一个空白文档。
首先,你创建一个应用程序
var WordApp = new ActiveXObject("Word.Application");
然后,你创建一个文档
WordApp.Documents.Add();
如果你想向用户显示Word窗口,你可以添加
WordApp.Visible = true;
- 从模板开始文档。
当你有一个文档模板时,你可能希望使用
WordApp.Documents.Add("TemplateName.dot");
而不是简单地开始一个空白文档。
- 处理选择和在文档中导航。
图 1. 折叠的选择(左,I形光标)和范围选择(右)。
移动
Selection
是主要的导航技术。将Selection
想象成你在打开你最喜欢的文本处理器时每次都会看到的I形光标很有用。可用的方法有:Collapse(Direction)
将选择折叠到开始或结束位置。选择折叠后,开始和结束点相等。
Direction
可以是wdCollapseStart
(值:1)或wdCollapseEnd
(值:0)。Move(Unit, Count)
将选择折叠到其开始或结束位置,然后将折叠的对象按
Unit
的Count
数量进行移动。如果Count
为正数,则选择折叠到其结束位置,并向前移动文档中的Count
个Unit
。如果Count
为负数,则选择折叠到其开始位置,并向后移动Count
个Unit
。Unit
可以是以下之一单位 值 字符
1 单词
2 Sentence
3 段落
4 Line
5 故事
6 通过
Collapse
和Move
方法,你可以像处理I形光标一样处理Selection
;以下方法允许你选择的不是位置,而是字符、单词等的范围。MoveStart(Unit, Count)
将选择的开始位置按
Unit
的Count
数量进行移动。MoveEnd(Unit, Count)
将选择的结束位置按
Unit
的Count
数量进行移动。MoveStartWhile(CharSet, Count)
/MoveEndWhile(CharSet, Count)
移动选择的开始/结束位置,直到在文档中找到
CharSet
中列出的任何字符。Count
是一个可选参数,指定要移动选择的最大字符数。可以是数字,也可以是wdForward
(值:1073741823)或wdBackward
(值:-1073741823)常量。如果Count
为正数,则选择向前移动;如果为负数,则选择向后移动。MoveStartUntil(CharSet, Count)/MoveEndUntil(CharSet, Count)
移动选择的开始/结束位置,直到在文档中找到
CharSet
中列出的任何字符。Count
是一个可选参数,指定要移动选择的最大字符数。可以是数字,也可以是wdForward
(值:1073741823)或wdBackward
(值:-1073741823)常量。如果Count
为正数,则选择向前移动;如果为负数,则选择向后移动。
WordApp.Selection.TypeText("Selection") WordApp.Selection.MoveStart(1, -4); WordApp.Selection.MoveEnd(1, -1); // Chars "tio" from "Selection" are now selected.
- 添加文本。
最常用的命令是
WordApp.Selection.TypeText("Text here");
- 向字段添加文本。
对于基于字段的文档(报告等)很有用
// For text field WordApp.ActiveDocument.FormFields("TextFieldName").Result = String; // For checkbox field WordApp.ActiveDocument.FormFields("CheckFieldName").CheckBox.Value = {true | false}; // For drop-down-list field WordApp.ActiveDocument.FormFields("DropDownFieldName").DropDown.Value = number-index;
- 输入新的一行。
WordApp.Selection.TypeParagraph();
- 格式化段落。
如果你想为文本应用样式,你有两个对象可供使用:
WordApp.Selection.ParagraphFormat
和WordApp.Selection.Font
。ParagraphFormat
对象最常用的属性是:Alignment
= {对齐方式值}根据值设置对齐规则
规则 值 左对齐 0 居中对齐 1 右对齐 2 两端对齐 3 分散对齐 4 FirstLineIndent
= {数字}设置第一行或悬挂缩进的值(以磅为单位)。使用正值设置第一行缩进,使用负值设置悬挂缩进。
KeepTogether
= {true
(非零)/false
(零)}当Word重新分页文档时,如果段落中的所有行都保留在同一页上,则设置非零值;否则,设置为零。
KeepWithNext
= {true
(非零)/false
(零)}当Word重新分页文档时,如果当前段落与下一段落保留在同一页上,则设置非零值;否则,设置为零。
LeftIndent
= {数字},RightIndent
= {数字}设置段落的左/右缩进值(以磅为单位)。
LineSpacing
= {数字}设置段落的行距(以磅为单位)。
SpaceAfter
= {数字},SpaceBefore
= {数字}设置段落之后/之前的间距(以磅为单位)。
Font
对象最常用的属性是:AllCaps
= {true
(非零)/false
(零)}如果字体应格式化为全部大写字母,则设置非零值;否则,设置为零。
Bold
= {true
(非零)/false
(零)}如果字体应格式化为粗体文本,则设置非零值;否则,设置为零。
DoubleStrikeThrough
= {true
(非零)/false
(零)}如果字体应格式化为双删除线文本,则设置非零值;否则,设置为零。
Emboss
= {true
(非零)/false
(零)},Engrave
= {true
(非零)/false
(零)}如果字体应格式化为浮雕/雕刻效果,则设置非零值;否则,设置为零。
Italic
= {true
(非零)/false
(零)}如果字体应格式化为斜体,则设置非零值;否则,设置为零。
Position
= {数字}设置文本相对于基线的(以磅为单位的)位置。正数向上抬高文本,负数向下压低文本。
Name
= {字符串}设置字体的名称。
Shadow
= {true
(非零)/false
(零)}如果字体应格式化为阴影效果,则设置非零值;否则,设置为零。
Size
= {数字}设置字体大小(以磅为单位)。
SmallCaps
= {true
(非零)/false
(零)}如果字体应格式化为小型大写字母,则设置非零值;否则,设置为零。
Spacing
= {数字}设置字符之间的间距(以磅为单位)。
StrikeThrough
= {true
(非零)/false
(零)}如果字体应格式化为删除线文本,则设置非零值;否则,设置为零。
Subscript
= {true
(非零)/false
(零)}如果字体应格式化为下标,则设置非零值;否则,设置为零。
Superscript
= {true
(非零)/false
(零)}如果字体应格式化为上标,则设置非零值;否则,设置为零。
Underline
= {下划线类型值}设置应用于字体的下划线类型。可用类型有:
类型 值 Underline-Dash
7 Underline-Dash-Heavy
23 Underline-Dash-Long
39 Underline-Dash-Long-Heavy
55 Underline-Dot-Dash
9 Underline-Dot-Dash-Heavy
25 Underline-Dot-Dot-Dash
10 Underline-Dot-Dot-Dash-Heavy
26 Underline-Dotted
4 Underline-Dotted-Heavy
20 Underline-Double
3 Underline-None
0 Underline-Single
1 Underline-Thick
6 Underline-Wavy
11 Underline-Wavy-Double
43 Underline-Wavy-Heavy
27 Underline-Words
2
在更改段落/字体样式时,你必须知道只有一条规则:属性一旦更改,就会保持其状态,直到下次更改;或者,如果没有引入更改,则在文档的剩余部分保持不变。对于字体,此规则仅在
Selection
折叠时有效;如果未折叠,则字体更改仅适用于所选范围。换句话说,你应该跟踪字体/段落样式的所有更改。例如
WordApp.Selection.TypeText("Ordinary text here"); ... WordApp.Selection.Font.Superscript = 1; WordApp.Selection.TypeText("Superscripted text here and further on"); ... WordApp.Selection.Font.Superscript = 0; WordApp.Selection.TypeText("Normal text again");
- 格式化单个字符。
结合导航的繁琐操作和
Font
的功能,我们有了一种方法来控制单个字符的样式。- 将选择的开始/结束移动到适当的位置,以便选择包含所需的段落/单词/字符;
- 更改
Selecton.Font
的属性; - 折叠选择;
- 恢复字体属性(以便你继续使用正常字体输入)。
示例
// Adding some text WordApp.Selection.TypeText("Some chars are different from others"); // Moving the selection to 'different' word: WordApp.Selection.MoveStart(1, -21); WordApp.Selection.MoveEnd(1, -12); WordApp.Selection.Font.Superscript = 1; // Move selecton to the end of a phrase: WordApp.Selection.MoveEnd(1, 12); WordApp.Selection.Collapse(0); WordApp.Selection.Font.Superscript = 0; // Continue typing...
- 处理剪贴板。
剪贴板是自动化应用程序之间主要的通信渠道。它也使得大块文本的传输更容易。
Selection
对象。基本方法是:Copy
- 将Selection
的内容复制到剪贴板WordApp.Selection.Copy()
Cut
- 将Selection
的内容复制到剪贴板,并将其从文档中删除WordApp.Selection.Cut()
Paste
- 将剪贴板的内容插入到Selection
对象指定的位置。如果Selection
未折叠,则其内容将被剪贴板的内容替换。WordApp.Selection.Paste()
- 保存文档。
你完成了更改吗?
var Path = WScript.ScriptFullName; Path = Path.substring(0, Path.lastIndexOf("\\")); WordApp.ActiveDocument.SaveAs(Path + "/letter.doc");
如果你希望将文档保存到脚本所在的文件夹,就需要使用
WScript.ScriptFullName
进行神奇操作;否则,Word会将你的文档保存在默认文件夹中(通常是我的文档文件夹)。 - 退出。
WordApp.Quit();
格式化一封信。
首先,示例程序将创建一个商务信函并将其保存到当前目录。
// Start a new instance of Microsoft Word
var WordApp = new ActiveXObject("Word.Application");
// Create a new document
WordApp.Documents.Add();
// Uncomment the following to see the Word window:
// WordApp.Visible = true;
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
// Add text header:
WordApp.Selection.ParagraphFormat.Alignment = 1;
//wdAlignParagraphRight = 2, wdAlignParagraphCenter = 1
WordApp.Selection.TypeText("Dear Customer,");
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
// Add main text:
WordApp.Selection.ParagraphFormat.Alignment = 3;
//wdAlignParagraphJustify = 3
WordApp.Selection.ParagraphFormat.FirstLineIndent = 10;
WordApp.Selection.TypeText("We are very sorry" +
" to hear that our Push-O-MaticTM");
// Show trademark sign as a superscript:
WordApp.Selection.MoveStart(1, -2);
WordApp.Selection.MoveEnd(1, 0);
WordApp.Selection.Font.Superscript = 1;
// Continue typing a text:
WordApp.Selection.Collapse(0);
WordApp.Selection.Font.Superscript = 0;
WordApp.Selection.TypeText(" toy doesn’t push properly." +
" It was working perfectly by the time we sent it to you. ");
WordApp.Selection.TypeText("Please be assured that" +
" we shall scrupulously investigate the cause.");
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText("Your purchase will be replaced" +
" in a week; we thank you for patience.");
// Add a signature:
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeParagraph();
WordApp.Selection.ParagraphFormat.Alignment = 2;
WordApp.Selection.TypeText("Sincerely yours,");
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText("William Gates IX Jr.,");
WordApp.Selection.TypeParagraph();
WordApp.Selection.TypeText("PleasureToys Inc, CEO.");
// Save & exit:
var Path = WScript.ScriptFullName;
Path = Path.substring(0, Path.lastIndexOf("\\"));
WordApp.ActiveDocument.SaveAs(Path + "/letter.doc");
WordApp.Quit();
格式化报告
此示例利用了文档模板(ReturnAndUplift.dot,参见附带的zip存档)以及对字段的广泛使用。
// Start a new instance of Microsoft Word
var WordApp = new ActiveXObject("Word.Application");
var Path = WScript.ScriptFullName;
Path = Path.substring(0, Path.lastIndexOf("\\"));
// Create a new document
WordApp.Documents.Add(Path + "/ReturnAndUplift.dot");
// Comment the following to hide the Word window:
WordApp.Visible = true;
var dt = new Date();
WordApp.ActiveDocument.FormFields("DateOfReport").Result =
(dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
WordApp.ActiveDocument.FormFields("OrderID").Result = "8765-4321";
WordApp.ActiveDocument.FormFields("ProducedBy").Result = "J. C. Denton";
WordApp.ActiveDocument.FormFields("StoreName").Result = "Store-456";
WordApp.ActiveDocument.FormFields("CustomersName").Result = "John C.";
WordApp.ActiveDocument.FormFields("Address").Result = "14 Street, 21";
WordApp.ActiveDocument.FormFields("CityTown").Result = "NY";
WordApp.ActiveDocument.FormFields("PostCode").Result = "1234-5678";
// Uncomment the followng lines to automatically save & exit:
// WordApp.ActiveDocument.SaveAs(Path + "/letter.doc");
// WordApp.Quit();
将文本从MS IE传输到Word
此示例显示了如何使用剪贴板将文本从网页传输到Word文档。与前两个示例不同,这个示例不是一个独立的脚本;它是一个网页,其中的脚本同时利用了MS IE用户javascript和Word脚本功能。应该用Internet Explorer打开。
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
function SelectionToWord()
{
// Determine a selection:
var select = document.selection;
var range = select.createRange();
// If selection is not empty:
if(range.text.length)
{
// Copy selection to clipboard:
range.execCommand("Copy");
// Start MS Word instance:
var word = new ActiveXObject("Word.Application");
// Create new document:
word.Documents.Add();
// Paste clipboard contents into the document:
word.Selection.Paste();
// Show MS Word:
word.Visible = true;
}
}
</SCRIPT>
</HEAD>
<BODY>
<P>Select a text and push a button.</P>
<P>blah-blah-blah <B>blah-blah-blah blah-blah-blah</B> blah-blah-blah<BR>
blah-blah-blah <B>blah-blah-blah blah-blah-blah</B> blah-blah-blah<BR>
blah-blah-blah <B>blah-blah-blah blah-blah-blah</B> blah-blah-blah<BR>
blah-blah-blah <B>blah-blah-blah blah-blah-blah</B> blah-blah-blah</P>
<INPUT type="button" onclick="SelectionToWord()"
value="Selection -> MS Word"></INPUT>
</BODY>
</HTML>
历史
- 发布日期:2005年12月26日。
- 更新日期:2007年1月20日。