VBA更改特定字符串中一个字符的字体大小(MSWORD MACRO)

时间:2017-08-18 12:55:14

标签: vba ms-word word-vba

我正在尝试替换特定字符串中一个字符的字体大小。

enter image description here

这个创建了我想要的单词,但我无法将其与替换功能合并。

Sub logo()
Selection.Collapse Direction:=wdCollapseEnd
With Selection.Range
 .Font.Reset
 .Text = "EXAMPLE"
 .Characters(2).Font.Size = 8
End With
End Sub

这个替换了整个单词。我无法将其与角色功能合并。

Sub RedoFonts()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Text = "EXAMPLE"
        .Replacement.Text = "EXAMPLE"
        .Font.Size = 12
        .Replacement.Font.Size = 9
        .Execute Replace:=wdReplaceAll
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Name = "Times New Roman"
        .Replacement.Font.Name = "Times New Roman"
        .Execute Replace:=wdReplaceAll
    End With
End Sub

任何方式合并这两个?

1 个答案:

答案 0 :(得分:2)

以下代码在Active Document中搜索字符串" this"并改变了" h"在它的字体大小为20。

Dim myRange As Range

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="this", Forward:=True
If myRange.Find.Found = True Then
    myRange.Find.Execute FindText:="h", Forward:=True
    If myRange.Find.Found = True Then myRange.Font.Size = 20
End If