Tables inside a Range object

L

LaVerne Defazio

I have a Word Range. It is a section of the document that
contains a table and about a page of text total. I am
doing various text find/replacements in this range object
with no problems. The trouble I have is with a table that
lives in my range. The table has three columns. In the
first column, I have a text string. In the 2nd and 3rd
columns I have no text.

Using this oRange object, I want to "select" the text
string and replace it with a different string
(find/replace). Then I want to tab to the next cell,
insert text, tab to the next cell and insert text. So the
original text string is gone, it has been replaced with
the new string and the entire row of the table has been
populated. Other rows of the same table have the same
setup, so I will be running this same process a couple
times.

In the original code, I used a Selection object for this.
It worked like a champ, but as the document grew to 50 or
100 pages or more it just crawled. So now we're on the
Selection object. I dont need to do a "find" on the entire
document, just within the range. But I cannot get this
table code switched over to the Range. The old code was:

'find the text:
oWord.Selection.Find.Text = str_Original

'replace the text and move out of this cell
oWord.Selection.TypeText(Text:=str_One)
'note: str_Original is gone at this point altho you
'did not use the "replacment" method

oWord.Selection.MoveRight(Unit:=Word.WdUnits.wdCell)

'in the next cell, type some text, move to next cell
oWord.Selection.TypeText(Text:=str_Two))
oWord.Selection.MoveRight(Unit:=Word.WdUnits.wdCell)

'in the third cell, type some more text
oWord.Selection.TypeText(Text:=str_Three))

This "type text" stuff worked great, but I cannot seem to
get it switched over to use the Range object.

I am having a heck of a time getting this logic to run
correctly and have been posting many related questions. If
someone could give me some help on this particular
problem, that would be much appreciated. Thanks.

Signed,
LaVerne
 
J

Jezebel

You might find it easier to deal with the table separately. One way is to
use cell objects:

Dim pCell As Word.Cell
Dim pText As String

For Each pCell In MyRange.Tables(1).Range.Cells
pText = pCell.Range.Text 'Get the cell
contents
pText = Left(pText, Len(pText) - 2) 'Discard the end of cell
marker

pCell.Range.Text = Replace(pText, FindString, ReplaceString, , ,
vbTextCompare)
Next
 
D

Doug Robbins - Word MVP

Hi Laverne,

The following will do what you want

Dim i As Integer, j as integer, myrange As Range
For j = 1 To ActiveDocument.Tables.Count
For i = 1 To ActiveDocument.Tables(j).Rows.Count
Set myrange = ActiveDocument.Tables(j).Cell(i, 1).Range
myrange.End = myrange.End - 1
If myrange.Text = str_Original Then
myrange.Text = str_One
ActiveDocument.Tables(j).Cell(i, 2).Range.Text = str_Two
ActiveDocument.Tables(j).Cell(i, 3).Range.Text = str_Three
End If
Next i
Next j

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi LaVerne,

Dim i As Integer, j as integer, myrange As Range
For j = 1 To ActiveDocument.Tables.Count
For i = 1 To ActiveDocument.Tables(j).Rows.Count
Set myrange = ActiveDocument.Tables(j).Cell(i, 1).Range
myrange.End = myrange.End - 1
If myrange.Text = str_Original Then
MsgBox "The string is in Table " & j
Exit Sub
End If
Next i
Next j

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - 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