You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Word VBA为插入新建Word文档的指定文本单独设置格式

修正后VBA代码

下面的代码已经实现你要求的三个自定义格式功能,每个可自定义格式的位置都加了注释,你可以按需修改对应参数:

Sub A__GrabTheBolds()
On Error GoTo cleanUp
Application.ScreenUpdating = False
    Dim ThisDoc As Document
    Dim ThatDoc As Document
    Dim r As Range
    Dim insertRng As Range ' 新增用来定位插入内容的范围变量
    
    Set ThatDoc = Documents.Add

    '遍历所有打开的Word文档
    For Each ThisDoc In Application.Documents
        Dim FileNames As String, fFound As Boolean
        Dim fWritten As Boolean

        Set r = ThisDoc.Range
        
        '跳过代码所在的文档
        If Not ThisDoc Is ThisDocument Then
            Set r = ThisDoc.Range
            
            With r
                With .Find
                    .Text = ""
                    .Format = True
                    .Font.Bold = True
                End With
                
                Do While .Find.Execute(Forward:=True) = True
                    fWritten = True
                    
                    If r.Bold Then
                        ThatDoc.Range.Characters.Last.FormattedText = .FormattedText
                        ThatDoc.Range.InsertParagraphAfter
                        .Collapse 0
                    End If
                Loop
            End With
            
            '仅当文档内存在粗体文本时添加文件名到列表
            If fWritten = True Then
                FileNames = FileNames & vbCrLf & ThisDoc.Name
                fWritten = False
            End If
        End If
    Next

    ' --------------------------
    ' 需求1:设置"List of Documents:"标题格式
    ' --------------------------
    ThatDoc.Content.InsertParagraphAfter
    Set insertRng = ThatDoc.Range(ThatDoc.Content.End - 1, ThatDoc.Content.End - 1) ' 定位到文档末尾空白位置
    insertRng.Text = "List of Documents:"
    ' 以下参数可按需修改
    With insertRng.Font
        .Bold = True ' 粗体开关
        .Italic = True ' 斜体开关
        .Color = wdColorRed ' 字体颜色,可替换为wdColorBlue、RGB(0,128,0)等自定义色值
        .Size = 14 ' 字号
    End With
    insertRng.InsertParagraphAfter

    ' --------------------------
    ' 需求2:设置FileNames文件名列表格式
    ' --------------------------
    Set insertRng = ThatDoc.Range(ThatDoc.Content.End - 1, ThatDoc.Content.End - 1)
    insertRng.Text = FileNames
    ' 以下参数可按需修改
    With insertRng.Font
        .Bold = False
        .Italic = False
        .Color = wdColorBlue
        .Size = 10.5
    End With
    insertRng.InsertParagraphAfter

    ' --------------------------
    ' 需求3:设置第一行自定义文本格式
    ' --------------------------
    Set insertRng = ThatDoc.Range(ThatDoc.Content.End - 1, ThatDoc.Content.End - 1)
    insertRng.Text = "Can this text be uniquely formatted, i.e. it's own colour, italic, font size etc"
    ' 以下参数可按需修改
    With insertRng.Font
        .Bold = False
        .Italic = True
        .Color = RGB(0, 128, 0) ' 绿色
        .Size = 12
    End With
    insertRng.InsertParagraphAfter

    ' --------------------------
    ' 需求3:设置第二行自定义文本格式
    ' --------------------------
    Set insertRng = ThatDoc.Range(ThatDoc.Content.End - 1, ThatDoc.Content.End - 1)
    insertRng.Text = "Can this text also be uniquely formatted, i.e. it's own colour, italic, font size etc"
    ' 以下参数可按需修改
    With insertRng.Font
        .Bold = True
        .Italic = False
        .Color = wdColorDarkOrange
        .Size = 12
    End With

cleanUp:
Application.ScreenUpdating = True
Set insertRng = Nothing
Set ThatDoc = Nothing
Set ThisDoc = Nothing
End Sub

如果需要套用Word内置样式,可直接在对应位置添加insertRng.Style = "标题 1"这类配置即可。

内容的提问来源于stack exchange,提问作者Mikee S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 19:45:08