在Word中更改字体

时间:2013-12-02 02:24:28

标签: vba ms-word

我在Word中编译了这段代码并弹出:

  

编译错误:预期:表达式

当我改变

时会发生这种情况
.Font.Name <> "Times New Roman"

以下是代码:

Public Sub ChangeFonts()
SelectAllInstancesOfStyle ("Normal")
  With Selection.Find
     ' Clear all previously set formatting for Find dialog box.
     .ClearFormatting
     ' Set font to Find for replacement.
     .Font.Name <> "Times New Roman"

     ' Clear all previously set formatting for Replace dialog box.
     .Replacement.ClearFormatting
     ' Set font to Replace found font.
     .Replacement.Font.Name = "Verdana"
     ' Don't find or replace any text.
     .Text = ""
     .Replacement.Text = ""
     ' The following parameters must be set as follows
     ' to find only text formatted for the specified font.
     .Forward = True
     .Wrap = wdFindContinue
     .Format = True
     .MatchCase = False
     .MatchWholeWord = False
     .MatchWildcards = False
     .MatchSoundsLike = False
     .MatchAllWordForms = False
  End With
  ' Perform the find and replace.
  Selection.Find.Execute Replace:=wdReplaceAll
End Sub

1 个答案:

答案 0 :(得分:0)

以下是按段落更改字体的示例:

Sub sof20319889ChangeFontsByParagraphs()
  Dim i As Long, objParag
  i = 0
  For Each objParag In ActiveDocument.Paragraphs
    'Debug.Print i & ": " & objParag.Range.Font.Name
    If (objParag.Range.Font.Name <> "Times New Roman") Then
      objParag.Range.Font.Name = "Verdana"
    End If
    i = i + 1
  Next
End Sub