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
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