【问题标题】:How to change format of current paragraph without using Selection如何在不使用选择的情况下更改当前段落的格式
【发布时间】:2019-06-16 15:49:15
【问题描述】:

我有下面的代码,但没有使用Selection

Sub Format paragraph()

Dim wdDoc As Document

    With wdDoc.Range.Find 
       .Font.Size = 12
       .Text = "?" 
       .Execute 
    End With
End Sub

找到font size = 12的字符时,如何改变当前段落的格式?例如:

wdDoc.Paragraph(current).Font.Size = 14

wdDoc.Paragraph(current).Font.Color = wdBlue

感谢您的帮助。

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    诀窍是使用特定的Range 对象,该对象可用于访问其“父”段落。当Find.Execute 成功时,正在搜索的Range 包含找到的项目(与选择跳转到找到的项目相同)。例如:

    Sub Format paragraph()
      Dim rng as Range, para as Paragraph
      Dim wdDoc As Document
    
      Set wdDoc = ActiveDocument. 'Missing in code in question...
      Set rng = wdDoc.Content 'Content returns the Range
        With rng.Find 
           .Font.Size = 12
           .Text = "?" 
           If .Execute = True Then
             Set para = rng.Paragraphs(1)
             para.Font.Size = 14
             para.Font.Color = wdBlue
           End If
        End With
    End Sub
    

    【讨论】:

    • @Cindy_Meister 你好辛迪。感谢您的回答。这似乎是我需要的。有几个段落,一些字体大小为 12,另一些则为其他字体。那么,如何将您的 With rng.find 块包含在 while 循环中?还是我应该先计算与我想要的样式匹配的段落,然后执行for loop 以将相同的更改应用于其他段落?
    • @GerCas 循环是通常的方式。在这里和其他地方有很多与Find 一起使用的示例。例如stackoverflow.com/a/52596708/3077495 您可以考虑应用定义所需格式的段落样式,而不是应用手动格式。这只是一步,如果稍后需要再次更改格式,只需在 UI 中更改样式定义即可 - 无需宏。
    • @Cindy_Meister 你好辛迪。感谢您的建议和分享的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2021-09-19
    • 1970-01-01
    相关资源
    最近更新 更多