Prob Finding Text in Table

M

MikeZz

I get a 100 page document (rtf that has lots of tables) which lists numerous
product changes. Each product has a part number, description, etc... and I
want to search through the document and find all the part changes.

I'm pretty good at Excel VBA but never tried Word so this is what I did.

I recorded the following macro:
Searched for repeating known string at the beginning of each part number:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product/DRD FAM"
.Forward = True
.Wrap = wdFindContinue
End With

I know that once I find this text, I can just tab 7 times (data is in a
table) to get to the actual info I need so this is what VBA recorded:
Selection.MoveRight Unit:=wdCell ' but 7 times.

Tabbing 7 times gets me to the part number in the table.
Then the macro recorded:
Selection.Copy ' to copy the actual PN but I would put this into an
array.

Then I want to tab 2 more times to get the part description and pull that
info into the array as well.

After that's done, I want to find the next time "Product/DRD FAM" is found
and repeat my copying of info.

When I have all the info, I want to dump it into an excel file.

btw, the file format is an "rtf" file, if that makes a difference.

Thanks!
 
M

MikeZz

I forgot to mention that I want to start at the beginning of the file and
find each occurance of my text, then do a few "tabs" to pull out the required
data.

What happens when I record the macro is that every time I "tab", it
automatically highlights the entire text in the next cell and that's what I
want to copy into the array.

In other words, I want to find a string, tab 3 cells over, and get the
string in that cell. Then find the next string and repeat.

Sub PNs()
Dim EWOPNs As Variant

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Product/DRD FAM"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Call MoveRightX(1)
If Selection.Text = "Current Part" Then
'True = Found Body of EWO.
Selection.MoveRight Unit:=wdCell
EWOPNs(1, 1) = Selection.Text
End If
 
M

MikeZz

When I run my macro from the beginning of the word document, I get the
following error:

Run-time error '4605'

This method or property is not available because some or all of the object
does not refer to a table.
 
M

MikeZz

I cleaned up the macro and ran through it step by step.
There are no errors, but it seems like the macro doesn't even scan through
to find the text.

How do I code vba to call the entire document...'the selection' without
actually highlighting it? I think that's the first step.
 
D

Dave Lett

Hi Mike,

Why not manually copy the table, open Excel, paste it in, and delete the
columns of data that you don't want?

Okay, if for some reason that's not a viable option, then maybe the
following will get you started:

Dim oRngRow As Range
Dim oRngPlus7 As Range
Dim oRngPlus9 As Range

Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "Product/DRD FAM"
.Replacement.Text = ""
.Forward = True
Do While .Execute
Set oRngRow = Selection.Rows(1).Range

Set oRngPlus7 =
Selection.Rows(1).Cells(Selection.Rows(1).Cells(1).ColumnIndex + 7).Range
oRngPlus7.MoveEnd Unit:=wdCharacter, Count:=-1

Set oRngPlus9 =
Selection.Rows(1).Cells(Selection.Rows(1).Cells(1).ColumnIndex + 9).Range
oRngPlus9.MoveEnd Unit:=wdCharacter, Count:=-1

Debug.Print oRngPlus7.Text & vbTab & oRngPlus9.Text
Loop
End With
End Sub


HTH,
Dave
 

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