wdtitleword - use vba to apply to more than one word

前端 未结 1 1044
迷失自我
迷失自我 2021-01-28 06:08

In Word it\'s easy to use vba to apply a highlight to more than one string, word, sentence etc after the user of the document has Ctrl-selected a bunch of them. However, when I

相关标签:
1条回答
  • 2021-01-28 06:31

    Please try this:

    Sub changeNonContigCase()
    
        ' Find the non-contig selection
        If Selection.Font.Shading.BackgroundPatternColor = wdColorAutomatic Then
            Selection.Font.Shading.BackgroundPatternColor = whtcolor
        End If
    
        ' Find and process each range with .Font.Shading.BackgroundPatternColor = WhtColor
        ActiveDocument.Range.Select
        Selection.Collapse wdCollapseStart
    
        With Selection.Find
            .Font.Shading.BackgroundPatternColor = whtcolor
            .Forward = True
            .Wrap = wdFindContinue
    
            Do While .Execute
                ' Do what you need
                Selection.Range.Case = wdTitleWord
    
                ' Reset shading as you go
                Selection.Font.Shading.BackgroundPatternColor = wdColorAutomatic
    
                ' Setup to find the next selection
                Selection.Collapse wdCollapseEnd
            Loop
        End With
    
    End Sub
    

    This works, but is indirect. I don't think there is a more direct way to achieve this. You can modify to avoid resetting existing formatting that you need to preserve. Until now I didn't even know that it was possible to select a non-contiguous range in MS Word, pity it is not easier to work with in VBA.

    0 讨论(0)
提交回复
热议问题