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
相关推荐
gc_22994 小时前
学习C#调用OpenXml操作word文档的基本用法(10:读取其它文档属性)
word·openxml·自定义属性·扩展属性
缺点内向4 小时前
如何在 C# 中将 Word 文档转换为 EMF(增强型图元文件)
开发语言·c#·word·.net
secondyoung11 小时前
Pandoc转换Word文档:使用Lua过滤器统一调整Pandoc文档中的图片和表格格式
经验分享·junit·word·lua·markdown·pandoc·mermaid
宋情写1 天前
poi-tl根据模板生成word文件
word
2501_930707781 天前
使用C#代码向 Word 文档添加文档属性
开发语言·c#·word
IT一氪2 天前
一款 AI 驱动的 Word 文档翻译工具
人工智能·word
东方佑2 天前
使用Python标准库将Word文档转换为HTML:深入解析.docx文件处理脚本
python·html·word
wtsolutions2 天前
Sheet-to-Doc 支持 JSON 和 JSONL 格式:批量生成 Word 文档的新方式
json·word·wtsolutions·sheet-to-doc
伟贤AI之路2 天前
原创分享:Word 转 Markdown - 精准还原 Word 格式、支持 LaTeX 公式与表格
word
开开心心_Every3 天前
优化C盘存储:自定义软件文档保存路径工具
java·网络·数据库·typescript·word·asp.net·excel