ZetCode

VBScript Word.Document 对象

最后修改于 2025 年 4 月 9 日

VBScript 中的 Word.Document 对象代表一个 Microsoft Word 文档。它提供了对文档内容、格式和属性的访问。该对象是 Word 自动化模型的一部分,允许进行编程控制。通过它,您可以从脚本创建、修改和保存 Word 文档。

Word.Document 支持文本插入、格式设置和文档操作。它与段落、表格、样式和其他 Word 功能协同工作。本教程通过实际示例涵盖 Word.Document,以展示其在自动化场景中的功能。

Word.Document 对象概述

Word.Document 对象是通过 Word Application 对象访问的、用于以编程方式处理 Word 文档的核心接口。该对象提供了用于文档操作的方法和属性。

主要功能包括内容编辑、格式控制和文档管理。它支持文本插入、段落操作和样式应用。理解此对象能够通过 VBScript 实现强大的 Word 自动化。

创建新的 Word 文档

此示例演示了如何使用 VBScript 创建新的 Word 文档。它展示了基本的 Word 自动化设置和文档创建。脚本创建了 Word 应用程序和文档对象。

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

Set doc = wordApp.Documents.Add() ' Create new document
doc.Content.Text = "This is a new Word document created with VBScript"

' Save and close (optional)
' doc.SaveAs "C:\temp\newdoc.docx"
' doc.Close
' wordApp.Quit

Set doc = Nothing
Set wordApp = Nothing

脚本创建了一个 Word 应用程序实例并使其可见。将新文档添加到 Documents 集合中。文本被插入到文档内容中。注释掉的行显示了可选的保存和关闭操作。

向文档添加格式化文本

此示例展示了如何向 Word 文档添加格式化文本。它演示了字体属性操作和段落格式设置。脚本创建了具有不同格式选项的样式化内容。

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

' Add heading
Set heading = doc.Paragraphs.Add.Range
heading.Text = "Document Title" & vbCrLf
heading.Font.Name = "Arial"
heading.Font.Size = 16
heading.Font.Bold = True
heading.ParagraphFormat.Alignment = 1 ' wdAlignParagraphCenter

' Add body text
Set body = doc.Paragraphs.Add.Range
body.Text = "This is the document body with normal formatting." & vbCrLf
body.Font.Name = "Calibri"
body.Font.Size = 11

Set body = Nothing
Set heading = Nothing
Set doc = Nothing
Set wordApp = Nothing

脚本创建了一个带有居中、粗体标题和普通正文文本的文档。为每个部分应用了不同的字体和大小。段落格式控制文本对齐。该示例展示了基本的文本样式功能。

处理文档段落

此示例演示了如何以编程方式处理文档段落。它展示了如何访问、修改和格式化段落。脚本创建了具有不同属性的多个段落。

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

' Add three paragraphs with different formatting
For i = 1 To 3
    Set para = doc.Paragraphs.Add.Range
    para.Text = "This is paragraph " & i & vbCrLf
    
    Select Case i
        Case 1
            para.Font.Bold = True
        Case 2
            para.Font.Italic = True
        Case 3
            para.ParagraphFormat.Alignment = 1 ' Center
    End Select
Next

' Access existing paragraphs
Set firstPara = doc.Paragraphs(1).Range
firstPara.Text = "Modified first paragraph text" & vbCrLf

Set firstPara = Nothing
Set para = Nothing
Set doc = Nothing
Set wordApp = Nothing

脚本创建了三个具有不同格式样式的段落。然后修改了第一个段落的文本。这演示了段落的创建和访问。该示例展示了基本的段落操作技术。

在文档中插入表格

此示例展示了如何在 Word 文档中插入和格式化表格。它演示了表格创建、单元格访问和格式设置。脚本创建了一个简单的数据表格。

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

' Add a 3x3 table
Set tableRange = doc.Content
tableRange.Collapse 0 ' wdCollapseEnd
Set myTable = doc.Tables.Add(tableRange, 3, 3)

' Fill table cells
For row = 1 To 3
    For col = 1 To 3
        myTable.Cell(row, col).Range.Text = "Row " & row & ", Col " & col
    Next
Next

' Format table
myTable.Borders.Enable = True
myTable.Rows(1).Range.Font.Bold = True ' Header row

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

脚本创建了一个 3x3 的表格并填充了数据。启用了表格边框,并将第一行设置为粗体。这演示了基本的表格创建和格式设置。该示例展示了如何以编程方式处理表格单元格。

保存和关闭文档

此示例演示了文档保存和关闭操作。它展示了如何将文档保存为不同格式。脚本包括文件操作的错误处理。

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

doc.Content.Text = "This document will be saved and closed."

On Error Resume Next ' Error handling for file operations

' Save in different formats
doc.SaveAs "C:\temp\mydoc.docx" ' Word format
' doc.SaveAs "C:\temp\mydoc.pdf", 17 ' PDF format
' doc.SaveAs "C:\temp\mydoc.rtf", 6 ' RTF format

If Err.Number <> 0 Then
    WScript.Echo "Error saving document: " & Err.Description
End If

On Error GoTo 0 ' Reset error handling

doc.Close
wordApp.Quit

Set doc = Nothing
Set wordApp = Nothing

脚本创建了一个文档并演示了将其保存为不同格式。错误处理可防止文件操作期间脚本失败。文档被正确关闭并释放了资源。这展示了正确的文档管理实践。

来源

Word.Document 对象文档

在本文中,我们探讨了 VBScript 中的 Word.Document 对象,涵盖了它的用法和实际应用。从文档创建到格式设置和保存,这些示例展示了 Word 的自动化功能。通过这些知识,您可以在脚本中自动化 Word 文档处理。

作者

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

列出所有 VBScript 教程