简体   繁体   中英

How to change font size excluding headings,sub headings, TOC ,Title using VBA macro in word document

My scenario is that I want to change font size of content body excluding title,headings,sub headings,TOC using VBA macro ,simply means change the font actual content body using macro ( Normal style applied to actual content)

Here is my VBA code:

Private Sub Document_Open()
   With ActiveDocument.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Text = ""
        .Replacement.Text = ""
        .Execute Replace:=wdReplaceAll
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Name = "Arial"
        .Replacement.Font.Name = "Calibri"
        .Execute Replace:=wdReplaceAll         
         .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Name = "Times New Roman"
        .Replacement.Font.Name = "Calibri"
        .Execute Replace:=wdReplaceAll      
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Size = 11
        .Replacement.Font.Size = 10
        .Execute Replace:=wdReplaceAll                
    End With
End Sub

using this code the whole document font size is changing but font name changing only actual content only using above macro.

Is it possible using VBA macro o change actual contents of document?

Can you suggest me how can I do it using VBA Macro?

If you are sure that the style Normal is applied to the document and you want to change the font properties of those parts with Normal style, then you can specify the style in Find as below:

ActiveDocument.Content.Find.Style = ActiveDocument.Styles("Normal") .

Also .Replacement.Text = "" will remove all the matching text from your document. Make sure that you use .Replacement.Text = "^&" . ^& will replace the same text as found.

Sub FormatNormal()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Style = ActiveDocument.Styles("Normal")
        .Text = ""
        .Replacement.Text = "^&"
        .Replacement.Font.Size = 10
        .Replacement.Font.Name = "Calibri"
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM