Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 06-19-2017, 12:25 PM
slaycock slaycock is offline Word VBA to search for bold text within a table Windows 7 64bit Word VBA to search for bold text within a table Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

I'm afraid your code does have a lot of errors both structural and logical so it would never work as you intended.

What do you want to happen if no bold text with a key term is found in a row?

What do you want to happen if a key term in bold is found?

Some hints are

1. Use F1 to get help on any VBA term
2. In the IDE use Tools.options and tick all the boxes under Code Settings

The code below might help to point you in the right direction

Code:
Sub sbSearchRowsForBold()

' Consolidate into a single string so we can search using instring  to check if the found text is a Key Term
' If Selection.Find.Text = "Organization" Or Selection.Find.Text = "Date" Or Selection.Find.Text = "Description" Or Selection.Find.Text = "Aerospace, Space & Defence" Or Selection.Find.Text = "Automotive" Or Selection.Find.Text = "Manufacturing" Or Selection.Find.Text = "Life Sciences" Or Selection.Find.Text = "Information Communication Technologies / Digital" Or "Natural Resources / Energy" Or Selection.Find.Text = "Regional Stakeholders" Or Selection.Find.Text = "Other Policy Priorities" Then
' Some of the text entries include ',' so + is used as a separator
Const myKeyTerms            As String = _
    "OrganizationDate+Description+Aerospace, Space & Defence+Automotive+Manufacturing+Life Sciences+Information Communication Technologies / Digital+Natural Resources / Energy+Regional Stakeholders+Other Policy Priorities"

Dim myTable                 As Table
Dim myRow                   As Row
Dim myRange                 As Range

    For Each myTable In ActiveDocument.Tables
        For Each myRow In myTable.Rows
            ' If successful myrange is moved to the found text
            Set myRange = myRow.Range
            ' Search parameters are persistent so you only need to change them if the search parameters change
            With myRange.Find
                .Font.Bold = True
                .Text = ""
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .Wrap = wdFindStop
                .Forward = True
                ' Stop when the range is searched
                ' .Execute returns true if the search is found
                Do While .Execute
                    ' myRange is now the found term
                    Debug.Print "Bold text is found ->" & myRange.Text
                    ' for learning purposes the select statement below shows the new range for myrange
                    myRange.Select
                    If InStr(myKeyTerms, myRange.Text) > 0 Then
                        'Actions to do if the row contained a key term in bold
                        Debug.Print "Found a key term in bold " & myRange.Text
                        ' Continue searching
                        
                    Else
                        'Actions to do is the row does not contain bold text
                        Debug.Print "No key terms found in bold text"
                    End If
                    ' now we need to reset myRange to encompass the rest of the row
                    myRange.Start = myRange.End + 1
                    myRange.End = myRow.Range.End
                    ' again for learning see where myRange has moved
                    myRange.Select
                Loop
            End With
        Next myRow
    Next myTable
End Sub
Good luck with your reading
Reply With Quote
 

Tags
search rows



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word VBA to search for bold text within a table VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color jc491 Word VBA 8 09-30-2015 06:10 AM
Word VBA to search for bold text within a table Text in #1 is made bold, rest of the document is edited, text in #1 is now not bold footer-assistance Word 1 06-29-2015 03:49 AM
VBA Search Table for Text/Select Text/Insert Hyperlink sldrellich Word VBA 3 03-24-2015 01:09 PM
how to search and replace BOLD text >> font color change? dylansmith Word 4 03-12-2013 09:51 PM
Word VBA to search for bold text within a table exporting bold text from excel to word? stella@happisburgh.net Excel 3 12-05-2010 08:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft