简体   繁体   中英

How to delete all non-bold words from a Word Document

I have a document with a number of terms in bold, and a number of terms not in bold. I'm trying to write a macro that deletes all of the latter, leaving the terms in bold in a nice list.

I've found this quite easy to do outside of VBA, using the advanced find function, however I really need this to be in a macro.

I then recorded a macro with the exact steps I take, generating this code:

Sub notbold()
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = False
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchByte = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
   Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

However, if I try and run this macro on a different document it doesn't work at all - it'll delete the first letter of the document

When I recorded it, it was different: And it works

Sub DelBold()
    Selection.Find.ClearFormatting
    Selection.Find.Font.Bold = False
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
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