WORD中的VBA操作

为什么会有这篇文章

最近用WORD比较多,发现VBA的用处真的是很方便,可以批量做很多手工很麻烦甚至是无法完成的功能。

系列文档

  1. WORD好用的技巧
  2. WORD中的VBA操作

前置条件

WORD

常用的VBA记录一下

至于怎么启用WORD中的宏操作,简单说一下,依次点击 文件 - 选项 - 信任中心 - 右侧信任中心设置 - 选择启用所有宏。 然后在视图选项卡中,最右侧可以见到。

批量删除文档中没用的样式

Sub DeleteUnusedStyles()
    Dim oStyle As Style

    For Each oStyle In ActiveDocument.Styles
        'Only check out non-built-in styles
        If oStyle.BuiltIn = False Then
            With ActiveDocument.Content.Find
                .ClearFormatting
                .Style = oStyle.NameLocal
                .Execute FindText:="", Format:=True
                If .Found = False Then oStyle.Delete
            End With
        End If
    Next oStyle
End Sub

批量删除空白表格

Sub Removetables ()
    Dim oTable As Table
    Each oTable In ActiveDocument.Tables
        oTable.Delete
    Next oTable
End Sub

参考链接

  1. How To Remove/Delete All Tables From A Document In Word
  2. Removing Unused Styles