Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2017, 07:47 AM
OfficeAssociate99 OfficeAssociate99 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 2010 64bit
Novice
Word VBA to search for bold text within a table
 
Join Date: May 2017
Posts: 19
OfficeAssociate99 is on a distinguished road
Default

Hey man, this amazing, thanks for all your help. It works (almost) as intended with one exception. Every time I run it, I get runtime error 91 on this line:
Code:
Loop Until InStr(myKeyTerms, mySecondRange) > 0
However, if I add a non-defined category, then it works as intended (cuts the category) before once again returning runtime error 91 "object not defined"


Any ideas? I'm running some basic tests, but I'm sure you have a much better idea of possible causes...

EDIT 1:
I think I've figured it out. If an undefined category is inserted at the end, the code outputs an error, because there is no defined category to use as a reference against.

EDIT 2:
I think I have a solution - I changed the line to this:
Code:
Loop Until InStr(myKeyTerms, mySecondRange) > 0 Or myFirstRange Is Nothing
I'll run a few more tests, but the end is sight! Thanks again
Reply With Quote
  #2  
Old 06-20-2017, 08:29 AM
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

An undefined category at the end of the document is an edge case I didn't consider.

But there is also a serendipitous error.

Loop Until InStr(myKeyTerms, mySecondRange)

should actually be

Loop Until InStr(myKeyTerms, mySecondRange.Text)

BUT the default property fpr the range object is text therefore the two statements above are syntactivally the same (you don't need to use the default property name to get the value).

That leaves an issue as its not possible now to use range.text a the loop terminator is the range can be nothing.

To get around this we need to move the tests for ending the loop inside the do loop so we can do each test separately and then use a separate boolean variable as a flag to end the loop.

We also need to handle the case of mySecondRange = nothing to ensure that the outer do loop ends correctly, hence the addition of the if statements after the end of the innder loop.

Here is the revised code, it just replaces the sbSearchRowsForBold code given above.

Code:
Sub sbSearchRowsForBold()

' Consolidate into a single string so we can search using instring  to checkif 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 seperator
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 myFirstRange            As Range
Dim mySecondRange           As Range
Dim myRemoveRange           As Range
Dim SecondRangeFlag         As Boolean
    For Each myTable In ActiveDocument.Tables
        Set myFirstRange = Nothing
        SecondRangeFlag = False
        Do
            If myFirstRange Is Nothing Then
                Set myFirstRange = fnFindBold(mySearchRange:=myTable.Range.Rows(1).Range)
            Else
                Set myFirstRange = fnFindBold(mySearchRange:=myFirstRange.Next(unit:=wdRow))
            End If
            ' two possible cases for myFirstrange
            ' 1. a found range
            ' 2. nothing - which means we have searched the whole table.
            If Not myFirstRange Is Nothing Then
                If InStr(myKeyTerms, myFirstRange.Text) = 0 Then
                    ' Found bold text that is not a defined category (key term)
                    Set mySecondRange = myFirstRange.Duplicate
                    Do
                        Set mySecondRange = fnFindBold(mySecondRange.Next(unit:=wdRow))
                        If mySecondRange Is Nothing Then
                            SecondRangeFlag = True
                        Else
                            If InStr(myKeyTerms, mySecondRange.Text) > 0 Then
                                SecondRangeFlag = True
                            End If
                        End If
                    Loop Until SecondRangeFlag
                    'We have now found text that is a defined category key term
                    Set myRemoveRange = myFirstRange.Duplicate
                    If mySecondRange Is Nothing Then
                        myRemoveRange.End = myTable.Range.End
                        Set myFirstRange = Nothing
                    Else
                        myRemoveRange.End = mySecondRange.Previous(unit:=wdRow).End
                        Set myFirstRange = mySecondRange
                    End If
                    myRemoveRange.Select
                    myRemoveRange.Cut
                    ' the destiny of the cut text is left to the requirements of the user
                End If
            End If
        Loop Until myFirstRange Is Nothing
            
    Next myTable
    
End Sub
Reply With Quote
Reply

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 05:01 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