Word VBA 基础
Word VBA 基础
1. Application的常用对象
| 常用对象 | 说明 |
|---|---|
| Application.ActiveDocument | 当前文档,可以简写为ActiveDocument |
| Application.ActivePrinter | 获取当前打印机 |
| Application.ActiveWindows | 当前窗口 |
| Application.Height | 当前应用文档的高度 |
| Application.Width | 当前应用文档的宽度 |
| Application.Build | 获取Word版本号和编译序号 |
| Application.Caption | 当前应用程序名 |
| Application.DefaultSaveFormat | 返回空字符串,表示Word文档 |
| Application.DisplayRecentFiles | 返回是否显示最近使用的文档的状态 |
| Application.Documents.Count | 返回打开的文档数 |
| Application.FontNames.Count | 返回当前可用的字体数 |
| Application.Left | 返回当前文档的水平位置 |
| Application.MacroContainer.FullName | 返回当前文档名,包括所在路径 |
| Application.NormalTemplate.FullName | 返回文档标准模版名称及所在位置 |
| Application.Path | 显示活动文档的路径和文件名 |
| Application.RecentFiles.Count | 返回最近打开的文档数目 |
| Application.System.FreeDiskSpace | 返回应用程序所在磁盘可用空间 |
| Application.Templates.Count | 返回应用程序所使用的模板数 |
| Application.UserName | 返回应用程序用户名 |
| Application.Version | 返回应用程序的版本号 |
| Application.Activate | 激活指定对象 |
| Application.Move | 设置任务窗口或活动文档窗口的位置 |
| Application.GoForward | 将插入在活动文档中进行编辑的最后三个位置之间向前移动 |
| Application.PrintOut | 该方法可打印指定文档的全部或部分 |
| Application.Resize | 调整Word窗口大小。如果该窗口被最大化或最小化将导致出错 |
| Application.Quit | 退出Word,并可选择保存或传送打开的文档 |
1. Document的常用对象
| 参数 | 中文 |
|---|---|
| ActiveDocument.AttachedTemplate.FullName | 返回当前文档采用模板名及模板所在位置 |
| ActiveDocument.Bookmarks.Count | 返回当前文档中的书签数 |
| ActiveDocument.Characters.Count | 返回当前文档的字符数 |
| ActiveDocument.Comments.Count | 返回当前文档的批注数 |
| ActiveDocument.Endnotes.Count | 返回当前文档的尾注数 |
| ActiveDocument.Fields.Count | 返回当前文档的域数目 |
| ActiveDocument.Footnotes.Count | 返回当前文档中的脚注数 |
| ActiveDocument.FullName | 返回当前文档的全名及所在位置 |
| ActiveDocument.HasPassword | 判断当前文档是否有密码保护 |
| ActiveDocument.Hyperlinks.Count | 返回当前文档中的链接数 |
| ActiveDocument.Indexes.Count | 返回当前文档中的索引数 |
| ActiveDocument.ListParagraphs.Count | 返回当前文档中项目编号或项目符号数 |
| ActiveDocument.PageSetup | 文档内的页面设置 |
| ActiveDocument.Paragraphs.Count | 返回当前文档中的段落数 |
| ActiveDocument.Password = xxx | 设置打开文件使用的密码 |
| ActiveDocument.Path | 文档所在路径 |
| ActiveDocument.ReadOnly | 获取当前文档是否为只读属性 |
| ActiveDocument.Saved | 当前文档是否被保存 |
| ActiveDocument.Sections.Count | 当前文档中的节数 |
| ActiveDocument.Sentences.Count | 当前文档中的语句数 |
| ActiveDocument.Shapes.Count | 当前文档中的形状数 |
| ActiveDocument.Styles.Count | 当前文档中的样式数 |
| ActiveDocument.Tables.Count | 当前文档中的表格数 |
| ActiveDocument.TablesOfAuthorities.Count | 返回当前文档中的引文目录数 |
| ActiveDocument.TableOfContents.Count | 返回当前文档中的目录数 |
| ActiveDocument.TablesOfFigures.Count | 返回当前文档中的图表目录数 |
| ActiveDocument.Words.Count | 返回当前文档中字词数 |
3. Documents的常用对象
| 参数 | 中文 |
|---|---|
| Documents.Add | 表示添加至打开的文档集合中新建空文档 |
| Documents.Close | 关闭指定的一个或多个文档 |
| Documents.Item (indexs) | 表示第indexs文档 |
| Documents.Open | 打开指定的文档并将其添加至Documents集合 |
| Documents.Save | 保存指定文档及其说明 |
3.1 Summary of “Word VBA Basics”
3.1.1 Application Common Objects
The Application object in Word VBA provides properties and methods to interact with the Word environment. Key properties include:
- ActiveDocument: Represents the current document (can be shortened to
ActiveDocument). - ActivePrinter, ActiveWindow: Retrieve the current printer and window.
- Height, Width, Left: Control the application window’s dimensions and position.
- Version, Build: Return Word’s version and build number.
- Documents.Count, RecentFiles.Count: Track open and recently accessed documents.
- UserName, Caption: Display the user name and application title.
- System.FreeDiskSpace: Checks available disk space.
Key methods include:
- Activate, Move, Resize: Manage window states.
- PrintOut, Quit: Print documents or exit Word.
3.1.2 Document Common Objects
The ActiveDocument object represents the current document, with properties such as:
- FullName, Path: Document location and name.
- Paragraphs.Count, Words.Count: Count text elements.
- Tables.Count, Shapes.Count: Track document objects.
- Password, ReadOnly: Handle security and access.
- Saved: Checks if changes were saved.
3.1.3 Documents Collection
The Documents collection manages all open documents, with methods like:
- Add: Creates a new document.
- Open, Close, Save: Handle file operations.
- Item(index) : Accesses a specific document by index.
This overview covers essential VBA objects for automating Word tasks, from application settings to document manipulation.
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 个人杂货铺!
