Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Apr 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MSO Word Highlight Row based on Keyword VB Script Macro

    [ Solved by HansV (www.eileenslounge.com) on http://answers.microsoft.com/ ]

    Thanks HansV, that was fast.

    I need help highlighting rows based on keyword Office Word Macro.

    It's a puzzle. I'm not used to VB, and don't clearly understand it. There's a problem with the loop so once it finds and highlights the first row, it gets stuck looping through that row instead of continuing through the document for new occurrences of the keyword.

    Thanks to anyone who figures this out.

    Code:
    ' ###################################
    
    Option Explicit
    Sub AHighLightRowWSpecifiedText()
    
    
        Dim sText As String
        
        
        sText = "DelTrig" ' InputBox("Enter text for Row to be deleted")
    
        
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = sText
            .Wrap = wdFindContinue
        End With
        
    
        Do While Selection.Find.Execute
            
        Call HighLightRow
    
        Loop
    
    End Sub
    
    
    ' ###################################
    
        
    Sub HighLightRow()
    
            If Selection.Information(wdWithInTable) = True Then
    
            Selection.SelectRow
            
            Selection.Shading.BackgroundPatternColorIndex = wdYellow
            
           '  Selection.Collapse This needed changed to :
    		 Selection.Collapse Direction:=wdCollapseEnd
            ' & Problem solved !!!
    
    
            Exit Sub
            
            End If
            
    End Sub
    
    ' ###################################
    Last edited by satrow; 2016-04-24 at 16:46.

  2. #2
    New Lounger
    Join Date
    Apr 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I updated the code to work with all text, including outside of tables, for anyone interested :
    Code:
    ' ###################################
    
    Option Explicit
    Sub AHighLightRowWSpecifiedText()
    
    
        Dim sText As String
    	
    	
    	sText = "DelTrig" ' InputBox("Enter text for Row to be deleted")
    
    	
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = sText
            .Wrap = wdFindContinue
        End With
    	
    
        Do While Selection.Find.Execute
    		
    	Call HighLightRow
    
    	Loop
    
    End Sub
    
    
    ' ###################################
    
    	
    Sub HighLightRow()
    
            If Selection.Information(wdWithInTable) = True Then
    
            Selection.SelectRow
    		
    		Selection.Shading.BackgroundPatternColorIndex = wdYellow
    		
    		 Selection.Collapse Direction:=wdCollapseEnd
    		
    		Else
    		
    		Selection.Paragraphs.Format.Shading.BackgroundPatternColorIndex = wdYellow	
    		
    		 Selection.Collapse Direction:=wdCollapseEnd
    		
    		Exit Sub
    		
    		End If
    		
    End Sub
    
    ' ###################################
    Last edited by RetiredGeek; 2016-04-24 at 16:28. Reason: Added code tags

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    ItsJustMe: Evidently, you've posted about the same topic on at least one other forum. Please read our policy on Cross-Posting in rule #14:
    http://windowssecrets.com/forums/faq...n#crossposting
    and provide the relevant cross-post links. You should also do likewise for the other forums involved.

    It would also be appreciated if you could clarify what isse, if any, you are now trying to resolve.

    Finally, when posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen. Without them, your code loses much of whatever structure it had.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •