LBound and UBound

G

Greg

Hello Masters,

I was using the following lines of code to supply the ".Find" text in a
macro. The words to be found are in single column table with the
heading "Find" and stored in another document.

For i = LBound(ListArray) To UBound(ListArray) - 1 Step 2
With rngstory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ListArray(i)
On Error GoTo Done
.Replacement.Text = Format(Left(ListArray(i), 1), ">") &
Right(ListArray(i), Len(ListArray(i)) - 1)
.Replacement.Font.Bold = True
.Execute Replace:=wdReplaceAll
End With
Next i
Done:

The object of the macro was to look for these words in a document. If
found, First Cap the word and bold the word.
It works, but there are/were two problemns. First, if the table had
more rows than words and error is generated at the .Replacement line.
Error handling is my weakest link. I got around this by simply
skipping out of the macro with the On Error GoTo line. Is this the
right way?

Second problem was the heading in the table. If the word "find" was
included in the document text it was being processed as well. How do
you tell the macro to skip the first row? This is what I came up with:

For i = 2 To UBound(ListArray) - 1 Step 2

but I don't really understand why or how it works. Thanks for any
enlightenment you can offer.
 
H

Helmut Weber

Hi Greg,

is listarray an array filled with cell contents?
What is step 2 good for?
Process only each second entry?
Do you feed empty cells into the listarray?
Not that I think so, but can't think of something else either.

LBound and UBound?

Well, there must be a question behind the question!

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/



Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg

Helmut

I have a macro that will perform a multiword find and replace operation
where the Find and Replace words are listed in a two column table
stored as a separate document. The Find words are listed on the left
under a column headed FIND. The Replace words on the right. These
words are used to build an array. Like this:

Set WordList = Documents.Open(fileName:="C:\Find and Replace List.doc")
ListArray = WordList.Tables(1).Range.Text
ListArray = Split(ListArray, Chr(13) & Chr(7))
WordList.Close

The Find and Replace code is:

For i = LBound(ListArray) To UBound(ListArray) - 1 Step 3
With rngstory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ListArray(i)
.Replacement.Text = ListArray(i + 1)
.Execute Replace:=wdReplaceAll
End With
Next i

For the project I am playing with now, I only need FIND words and so I
adapted the code as I posted earlier. While experimenting with this I
noticed that if my text to be processes contains the word FIND (my
column header) then it is also processed which is what I don't want.

I guess what I am asking is how would you write the For i statement
such that it includes everything in the array except the first entry?

Thanks
 
H

Helmut Weber

Hi Greg,

For i = LBound(ListArray) + 1 To UBound(ListArray) - 1 Step 3

+ 1 ' !

Could that be all that is to it?

I must be missing the crucial point.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Helmut Weber

Hi Greg,
I see, hopefully.
That's new stuff to me.
I thought, maybe
ListArray = WordList.Tables(1).Range.Text
ListArray = Split(ListArray, Chr(13) & Chr(7))

would create a 2-dimensional array.
But it obviously doesn't.

So the offset would have to be equal to the number
of columns in the table, which is 2.

Or not?

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg

Helmut,

Through trial and error I found that this will work to exclude the
header words Find and Replace:

For i = 3 To UBound(ListArray) - 1 Step 3
 
H

Helmut Weber

Hi Greg,
so it is Lbound + offset
where offset is NumberOfColumns.

By the way, I couldn't find a method to format
a table programmatically with a heading.
Not to speak of repeating headings in case of pagebreaks.

I wonder, what ListArray = WordList.Tables(1).Range.Text
will do in that case.

OT:
An uncle of mine has survived World War II as a submariner.
He's 80 + by now.
Really tough guy.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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