Macro for Cap. Letters and Periods in Cells

W

Wynn

I have a table set-up in Word for lawyers diary entries.
There are four columns and the third column has the
description of what they did that day. For that column, I
want to make a capital letter at the beginning and a
period at the end. Sometimes there are cap. letter and
periods, sometimes there aren't. Does anyone know how to
do this? Thanks!
 
J

Jezebel

If these are lawyers, surely the column will always be blank or unprintable?

Iterate the cells in the column. If you are confident that the tables will
always be uniform (no merged or split cells) you can use something like

for pIndex = 1 to Table.Rows.Rows.Count
pText = Table.Cell(i,3).Range

'Strip off the non-printing characters
pText = Left(pText, len(pText)-2)

'Make first character uppercase (does nothing if it's non-alpha)
pText = ucase(left(pText,1)) & mid(pText,2)

'Make sure there's a period at the end
If Right(pText,1) <> "." then
pText = pText & "."
End if

'Put it back into the cell
Table.Cell(i,3).Range = pText
Next


Note that this method will discard any character formatting (bold, italic,
etc).
 
W

wynn

Perhaps it's just my own inexperience w/ VBA, but I get an
error message as soon as the Macro hits the first line of
this. Am I doing something wrong?
 
J

Jezebel

You have to declare the variables, and you have to tell the macro which
table you want to work with. If you start with the cursor anywhere within
the table, you could use:

Dim pIndex as long
Dim pText as string
Dim pTable as Word.Table

Set pTable = Selection.Tables(1)
for pIndex = 1 to pTable.Rows.Count
pText = pTable.Cell(i,3).Range
:

Or if you want to do all tables in the document:

For each pTable in ActiveDocument.Tables
For pIndex = 1 to pTable.Rows
:
Next
Next
 

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