VBA操作Word

检查word中的字体情况

vbnet 复制代码
Sub ListAllFontsInDocument()
    Dim doc As Document
    Dim rng As Range
    Dim char As Range
    Dim fontName As String
    Dim uniqueFonts As Collection
    
    ' 初始化集合用于存储唯一字体名称
    Set uniqueFonts = New Collection
    
    ' 获取当前活动文档
    Set doc = ActiveDocument
    
    ' 遍历文档中的每一个字符
    For Each rng In doc.Content.Characters
        ' 获取字符的字体名称
        fontName = rng.Font.Name
        
        ' 检查字体名称是否已经在集合中,如果没有则添加进去
        On Error Resume Next
        uniqueFonts.Add fontName, CStr(fontName)
        On Error GoTo 0
    Next rng
    
    ' 输出所有唯一的字体名称
    Dim item As Variant
    For Each item In uniqueFonts
        Debug.Print item
    Next item
End Sub

删除未使用样式

vbnet 复制代码
Sub 删除文档中未使用的样式()
    Dim doc As Document
    Dim pa As Paragraph
    Dim i As Long
    Dim sty As Style
    Dim dSty As Object
    Dim key
    
    Set dSty = CreateObject("Scripting.Dictionary")
    
    Set doc = ActiveDocument
    
    For Each pa In doc.Paragraphs
        key = pa.Style.NameLocal
        
        Debug.Print key
        If Not dSty.Exists(key) Then
            dSty(key) = True
        End If
    Next pa
    
    For i = doc.Styles.Count To 1 Step -1
        Set sty = doc.Styles(i)
        key = sty.NameLocal
        
        If Not dSty.Exists(key) Then
            On Error Resume Next
            sty.Delete
            On Error GoTo 0
        End If
    Next i
    
    Set doc = Nothing
    Set pa = Nothing
    Set sty = Nothing
    
    MsgBox "完成"
End Sub

选中所有表格

vbnet 复制代码
Sub 选择word中的表格()
Dim t As Table
an = MsgBox("即将选择选区内所有表格,若无选区,则选择全文表格。", vbYesNo, "提示")
If an - 6 Then Exit Sub
Set rg = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
For Each t In rg.Tables
    t.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges wdEditorEveryone
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
End Sub
vbnet 复制代码
 ' 移除正则匹配的字符串中的指定字符
Public Sub RemoveChars()
    Dim rng As Range
    Set rng = ActiveDocument.Content ' 或者指定特定的Range对象

    With rng.Find
        .ClearFormatting
        .Text = "[A-Z][-][0-9]?{1,10}[。]"
        .Forward = True
        .MatchWildcards = True

        Do While .Execute
            Dim matchText As String
            matchText = rng.Text

            ' 去除matchText中的"x"字符
            matchText = Replace(matchText, "。", "")

            ' 将修改后的文本替换回原文档
            rng.Text = matchText

            ' 移动查找范围到下一个匹配项
            rng.Collapse wdCollapseEnd
        Loop
    End With
End Sub
相关推荐
CNRio8 小时前
将word和excel快速转换为markdown格式
python·word·excel
玩泥巴的2 天前
.NET驾驭Word之力:基于规则自动生成及排版Word文档
c#·word·.net·com互操作
道之极万物灭2 天前
Python操作word实战
开发语言·python·word
跳动的喵尾巴4 天前
Endnote | word中加载项消失不见,如何处理?
word
夜白宋4 天前
【word多文档docx合并】
java·word
Uluoyu6 天前
word、pdf文档内容提取工具类
java·pdf·word
sanshizhang6 天前
word文档转pdf开源免费,可自定义水印
pdf·开源·word
lijingguang6 天前
excel和word文件默认用office打开而不是用wps
word·excel·wps
Uluoyu7 天前
支持Word (doc/docx) 和 PDF 转成一张垂直拼接的长PNG图片工具类
java·pdf·word
VBAMatrix7 天前
报告工具更新!Word附注一键期末转期初
word·办公自动化·审计报告·审计·会计师事务所·报告工具·word附注