How do I Search Doc for String, Get Table# that follows string?

M

MikeZz

Hi,
I've been programing a long time in Excel VBA but am a beginner in word VBA
so please bear that in mind. I know very little and definatly nothing about
the required Word Syntax.

I have an exel application that need to search through a long word document
which has a lot of tables. The word document can be over 500 pages long with
several thousand tables.

I'm trying use VBA to find a particular table which as a Title above it. I
think the Title is not part of the table but I really don't know how ranges
work in word.

So, how can I search through a file looking for a string:
"FindThisTitleString"
I'd like the option to start at the end of the file or at the beginning.

Below is a hybrid of some code I used many years ago and some I just found
in this forum. I'm including my old code at the top so you can see how I had
referenced the word objects in the past. This seems to work but I don't know
how to modify the Forum code to work with my references to the word objects.

Any help would be greatly apprciated!
Thanks,
MikeZz


Sub test1234()
'My Added Code from here until you see this line:
'###### Forum Suggested CODE HERE:

Dim oWord As Word.Application
Dim WordWasNotRunning As Boolean
Dim oDoc As Word.Document

'Get existing instance of Word if it's open; otherwise create a new one
On Error Resume Next

Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = New Word.Application
WordWasNotRunning = True
End If

On Error GoTo Err_Handler

oWord.Visible = True
oWord.Activate
'Search$ = "Matching"


oWord.Selection.Find.ClearFormatting
With oWord.Selection.Find
.Text = "UPC Prefix"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
oWord.Selection.Find.Execute

'###### Forum Suggested CODE HERE:

Dim rDcm As Range
Set rDcm = ActiveDocument.Range
ResetSearch
With rDcm.Find
.Text = "ferrites"
If .Execute Then
rDcm.End = ActiveDocument.Range.End
If rDcm.Tables.Count > 0 Then
rDcm.Tables(1).Select
End If
End If
End With
ResetSearch
End Sub

' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
G

Graham Mayor

You should be able to adapt the following to your requirements.
The following will find the first instance of the text string "UPC Prefix"
and select the next table after the found text

Dim oRng As Range
Dim oTable As Table
Dim sText As String
sText = "UPC Prefix"
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
Do While .Execute(findText:=sText, _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set oRng = Selection.Range
oRng.End = ActiveDocument.Range.End
Set oTable = oRng.Tables(1)
'Do what you want with the table here e.g.
oTable.Select
'***************
Exit Sub
Loop
End With
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top