Need macro to add up characters

S

stevewy

This is a request for some help programming a short, but rather odd,
Word macro.

I have a large Word table of four columns, extracted from a database,
and the fourth column contains text up to 2,100 characters long. I
need to spell check this fourth column of text, then import it all back
into the database (which doesn't have its own spell checker).

The trouble is, after I have spell checked the text, some cells are
liable to be slightly longer or slightly shorter than they were
originally, due to the spell check corrections, and may even run over
the 2,100 character limit. If I am to succesfully import the text back
into the database after spell checking, I would need to adjust the
defined limit for the text in the database. So I need to tell if the
2,100 limit gets exceeded after the spell checking.

I need a little macro that, once I have selected the entire column,
will work down each cell in the selection, and count the amount of
characters in each cell. Word will already do this manually
cell-by-cell using the "Word Count" function in the Tools menu, so
obviously this property exists in Word, but I am not certain how to
access it from VBA "in bulk".

The macro will only need to keep a track of the amount of characters in
the longest cell found, so it only needs to give me a "maximum" total
at the end of the macro play.

So at the end of the counting, a message box would pop up saying
"Longest amount of characters in any one cell = 2,227 characters", or
whatever.

Could anyone help with a small macro that would accomplish this? Many
thanks.

Steve Wylie
Canterbury,
United Kingdom
 
J

Jezebel

Dim pCell As Word.Cell
Dim pCount As Long

For Each pCell In Selection.Tables(1).Columns(4).Cells
If Len(pCell.Range) > pCount Then
pCount = Len(pCell.Range)
End If
Next
MsgBox "Longest cell = " & pCount - 2
 
S

stevewy

Wow! That's great, Jezebel. All in a few lines of code - the power of
VBA eh?

Thanks for your prompt reply. I can assure you that the macro will be
well used!

Steve Wylie
 

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