Set to proper case specific word and ask to bold

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Set to proper case specific word and ask to bold

Post by SmallFry »

In a document the word "Note:" needs to bold, but at times, the word has been capitalized.

This macro will ask if I want to bold the word "Note:", but can I also find the all caps "NOTE:" and ask for confirmation to set it to proper along with the asking to bold?

Code: Select all

Sub BoldNote()
    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    With Selection.Find
        .ClearFormatting
        .Forward = True
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Text = "Note:"
        While .Execute
            If MsgBox(Prompt:="Bold this note?", _
                    Buttons:=vbYesNo + vbQuestion, _
                    Title:="Emphasize References") = vbYes Then
                Selection.Style = ActiveDocument.Styles("Strong")
            End If
            Selection.Collapse wdCollapseEnd
        Wend
    End With
End Sub

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set to proper case specific word and ask to bold

Post by HansV »

For example:

Code: Select all

Sub BoldNote()
    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    With Selection.Find
        .ClearFormatting
        .Forward = True
        .Wrap = wdFindStop
        .MatchCase = False
        .MatchWildcards = False
        .Text = "Note:"
        Do While .Execute
            If MsgBox(Prompt:="Bold this note?", _
                    Buttons:=vbYesNo + vbQuestion, _
                    Title:="Emphasize References") = vbYes Then
                Selection.Style = ActiveDocument.Styles("Strong")
            End If
            If Selection.Text = "NOTE:" Then
                If MsgBox(Prompt:="Convert NOTE to Note?", _
                        Buttons:=vbYesNo + vbQuestion, _
                        Title:="ALL CAPS") = vbYes Then
                    Selection.Text = "Note:"
                End If
            End If
            Selection.Collapse wdCollapseEnd
        Loop
    End With
End Sub
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Set to proper case specific word and ask to bold

Post by SmallFry »

Thank you Hans. This works great.

I am expanding this to an array of words which works well with the code below. Question though on the line in bold.

Is there a way to say, IF MyList(i) = Upper case instead of the Or?

Code: Select all

Sub BoldNote()
    
    Dim MyList As Variant: MyList = Array("Note:", "Report:")
    Dim i As Integer
    
    For i = 0 To UBound(MyList)
    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
        With Selection.Find
            .ClearFormatting
            .Forward = True
            .Wrap = wdFindStop
            .MatchCase = False
            .MatchWildcards = False
            .Text = MyList(i)
            Do While .Execute
                If MsgBox(Prompt:="Bold this reference?", Buttons:=vbYesNo + vbQuestion, Title:="Bold Reference") = vbYes Then
                    Selection.Style = ActiveDocument.Styles("Strong")
                End If
                [b]If Selection.Text = "NOTE:" Or Selection.Text = "REPORT:" Then[/b]
                    If MsgBox(Prompt:="Convert to proper case?", Buttons:=vbYesNo + vbQuestion, Title:="All Caps") = vbYes Then
                        Selection.Text = MyList(i)
                    End If
                End If
                Selection.Collapse wdCollapseEnd
            Loop
        End With
    
    Next i
    
End Sub

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set to proper case specific word and ask to bold

Post by HansV »

Change

Code: Select all

If Selection.Text = "NOTE:" Or Selection.Text = "REPORT:" Then
to

Code: Select all

If Selection.Text = UCase(MyList(i)) Then
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Set to proper case specific word and ask to bold

Post by SmallFry »

Thanks again Hans. Works great.