Script for changing fonts in a Word document

前端 未结 3 1694
粉色の甜心
粉色の甜心 2021-01-24 10:44

I have a Word 2007 file and I want to change all usage of the Courier New font into a Lucida Console font. I need a script that find all words formatted in that font and change

相关标签:
3条回答
  • 2021-01-24 11:23

    Inside Word, you can record a macro by doing it yourself. Then you open the VBA editor, optionally remove some useless thing (usually too many selections or moves within the file) and you've got your script.

    Edited: moved the contents of a comment here, to answer the author's comment.

    When recording the macro, within the find and replace dialog, click on 'Replace All'. Then stop recording. The generated macro looks like:

    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
      .Text = "tarte au pomme" 
      .Replacement.Text = "t aux pruneaux" 
      .Wrap = wdFindContinue 
      .MatchCase = False 
      ' removed some stuff 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
    

    From that, you can create the VBScript macro. You need to get the value of wdReplaceAll and wdFindContinue in the Object Browser.

    0 讨论(0)
  • 2021-01-24 11:35

    Perhaps you can use OpenXML SDK 2.0? You should be able to change the style (which is what contains the font information, I believe).

    0 讨论(0)
  • 2021-01-24 11:35

    This will do it for you:

    Sub ChangeFonts() 
    Dim doc As Document 
    Set doc = ActiveDocument
        For i = 1 To doc.Range.Characters.Count 
            If doc.Range.Characters(i).Font.Name = "Courier New" Then 
                doc.Range.Characters(i).Font.Name = "Lucida Console" 
            End If 
        Next 
    End Sub
    
    0 讨论(0)
提交回复
热议问题