ZetCode

VBScript Word.Application 对象

最后修改于 2025 年 4 月 9 日

VBScript 中的 Word.Application 对象提供了 Microsoft Word 的自动化功能。它允许脚本以编程方式创建、修改和控制 Word 文档。此对象是 Word COM 自动化接口的一部分。它提供了对 Word 功能的完全控制。

使用 Word.Application,您可以生成报告、格式化文本和处理文档。本教程将通过实际示例介绍该对象。您将学习如何通过 VBScript 自动化常见的 Word 任务。

Word.Application 对象概述

Word.Application 对象本身代表 Word 应用程序。它提供了对文档、模板和 Word 功能的访问。对象模型包括 Documents、Selection 和 Range 对象。这些允许精确的文档操作。

关键属性包括 Visible 用于控制 UI 显示,Documents 用于访问打开的文件。Quit 等方法可以以编程方式关闭 Word。理解此对象可以实现强大的文档自动化。

创建新的 Word 文档

此示例演示了如何使用 VBScript 创建新的 Word 文档。它展示了基本的 Word.Application 对象初始化。脚本会创建一个可见的 Word 实例并添加一个空白文档。这是 Word 自动化的基础。

create_document.vbs
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True ' Make Word visible
Set newDoc = wordApp.Documents.Add()

' Add some text to the document
wordApp.Selection.TypeText "Hello, Word Automation!"
wordApp.Selection.TypeParagraph

' Save and close
newDoc.SaveAs "C:\Temp\NewDocument.docx"
newDoc.Close
wordApp.Quit

Set newDoc = Nothing
Set wordApp = Nothing

脚本会创建一个 Word 应用程序对象并使其可见。它会添加一个新文档并插入文本。文档会被正确保存和关闭。请始终释放对象以释放资源。

打开和修改现有文档

此示例演示了如何打开现有 Word 文档并对其进行修改。脚本展示了文件打开和文本操作。它会查找并替换文档中的文本。这对于模板处理很有用。

modify_document.vbs
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True

' Open existing document
Set doc = wordApp.Documents.Open("C:\Temp\ReportTemplate.docx")

' Replace placeholder text
With doc.Content.Find
    .Text = "[COMPANY_NAME]"
    .Replacement.Text = "ACME Corporation"
    .Execute Replace:=2 ' wdReplaceAll
End With

' Save changes
doc.SaveAs "C:\Temp\FinalReport.docx"
doc.Close
wordApp.Quit

Set doc = Nothing
Set wordApp = Nothing

脚本会打开一个模板文档并替换占位符文本。Find 对象提供了强大的搜索功能。更改会被保存到新文件中。原始模板保持不变。

格式化文档文本

此示例演示了 Word 文档中的文本格式化。它展示了字体属性操作和段落格式化。脚本会创建一个带有样式文本的文档。格式化包括字体、大小和颜色。

format_text.vbs
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set doc = wordApp.Documents.Add()

' Format heading
With wordApp.Selection
    .Font.Name = "Arial"
    .Font.Size = 16
    .Font.Bold = True
    .Font.Color = RGB(0, 0, 255) ' Blue
    .TypeText "Document Title"
    .TypeParagraph
End With

' Format body text
With wordApp.Selection
    .Font.Name = "Calibri"
    .Font.Size = 11
    .TypeText "This is formatted body text."
    .TypeParagraph
End With

doc.SaveAs "C:\Temp\FormattedDocument.docx"
doc.Close
wordApp.Quit

Set doc = Nothing
Set wordApp = Nothing

脚本会创建一个带有样式标题和正文文本的文档。字体属性使用 Font 对象进行设置。RGB 值指定文本颜色。每个格式化块应用于后续文本。

在 Word 中创建表格

此示例演示了在 Word 文档中创建表格。它展示了如何添加表格并用数据填充。脚本会创建一个 3x3 的表格并格式化单元格。表格对于数据呈现很有用。

create_table.vbs
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set doc = wordApp.Documents.Add()

' Add a table (3 rows, 3 columns)
Set myTable = doc.Tables.Add(wordApp.Selection.Range, 3, 3)

' Populate and format table
With myTable
    ' Header row
    .Cell(1, 1).Range.Text = "Product"
    .Cell(1, 2).Range.Text = "Price"
    .Cell(1, 3).Range.Text = "Stock"
    
    ' Data rows
    .Cell(2, 1).Range.Text = "Laptop"
    .Cell(2, 2).Range.Text = "$999"
    .Cell(2, 3).Range.Text = "15"
    
    .Cell(3, 1).Range.Text = "Mouse"
    .Cell(3, 2).Range.Text = "$25"
    .Cell(3, 3).Range.Text = "42"
    
    ' Apply formatting
    .Range.Font.Name = "Calibri"
    .Range.Font.Size = 11
    .Rows(1).Range.Font.Bold = True
End With

doc.SaveAs "C:\Temp\TableDocument.docx"
doc.Close
wordApp.Quit

Set myTable = Nothing
Set doc = Nothing
Set wordApp = Nothing

脚本会创建一个表格并用产品数据填充。单元格引用使用行和列索引。格式化会应用于表格和特定行。表格在文档中提供了结构化的数据呈现。

生成多页报告

此示例创建了一个包含各种元素的多页完整报告。它结合了文本、格式化、表格和分页符。脚本展示了全面的文档自动化。通过这种方法可以自动生成报告。

generate_report.vbs
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set doc = wordApp.Documents.Add()

' Add title
With wordApp.Selection
    .Font.Name = "Arial"
    .Font.Size = 18
    .Font.Bold = True
    .ParagraphFormat.Alignment = 1 ' wdAlignParagraphCenter
    .TypeText "Quarterly Sales Report"
    .TypeParagraph
    .Font.Size = 12
    .Font.Bold = False
    .TypeText "Q1 2025"
    .TypeParagraph
    .TypeParagraph
End With

' Add summary section
With wordApp.Selection
    .Font.Name = "Calibri"
    .Font.Size = 12
    .Font.Bold = True
    .TypeText "Executive Summary:"
    .TypeParagraph
    .Font.Bold = False
    .TypeText "Sales increased by 15% compared to last quarter."
    .TypeParagraph
    .TypeParagraph
End With

' Add sales table
Set salesTable = doc.Tables.Add(wordApp.Selection.Range, 5, 3)
With salesTable
    .Cell(1, 1).Range.Text = "Product"
    .Cell(1, 2).Range.Text = "Q4 2024"
    .Cell(1, 3).Range.Text = "Q1 2025"
    
    ' Fill table data
    .Cell(2, 1).Range.Text = "Product A"
    .Cell(2, 2).Range.Text = "$45,000"
    .Cell(2, 3).Range.Text = "$52,000"
    
    .Cell(3, 1).Range.Text = "Product B"
    .Cell(3, 2).Range.Text = "$32,000"
    .Cell(3, 3).Range.Text = "$38,500"
    
    .Cell(4, 1).Range.Text = "Product C"
    .Cell(4, 2).Range.Text = "$28,000"
    .Cell(4, 3).Range.Text = "$29,750"
    
    .Cell(5, 1).Range.Text = "Total"
    .Cell(5, 2).Range.Text = "$105,000"
    .Cell(5, 3).Range.Text = "$120,250"
    
    ' Format table
    .Range.Font.Name = "Calibri"
    .Range.Font.Size = 11
    .Rows(1).Range.Font.Bold = True
    .Rows(5).Range.Font.Bold = True
End With

' Add page break and appendix
wordApp.Selection.InsertBreak 7 ' wdPageBreak

With wordApp.Selection
    .Font.Name = "Calibri"
    .Font.Size = 14
    .Font.Bold = True
    .TypeText "Appendix: Detailed Metrics"
    .TypeParagraph
    .Font.Size = 11
    .Font.Bold = False
    .TypeText "Additional data available upon request."
    .TypeParagraph
End With

' Save final report
doc.SaveAs "C:\Temp\QuarterlyReport.docx"
doc.Close
wordApp.Quit

Set salesTable = Nothing
Set doc = Nothing
Set wordApp = Nothing

脚本会生成一个完整的销售报告,包含多个部分。它包括格式化文本、数据表格和分页符。该报告展示了专业的文档自动化。所有元素都以编程方式创建,无需用户交互。

来源

Word.Application 对象文档

在本文中,我们探讨了 VBScript 中的 Word.Application 对象。从基本的文档创建到复杂的报告生成,这些示例展示了强大的 Word 自动化功能。通过这些知识,您可以高效地自动化文档处理任务。VBScript 和 Word 自动化为业务需求提供了强大的解决方案。

作者

我的名字是 Jan Bodnar,我是一位充满激情的程序员,拥有多年的编程经验。我自 2007 年以来一直在撰写编程文章。到目前为止,我已撰写了 1400 多篇文章和 8 本电子书。我在教学编程方面拥有八年以上的经验。

列出所有 VBScript 教程