Do While loop

D

denapple

I'm a newcomer to this forum. Trying to review/learn more about VBA i
Word with "Using Microsoft Word 97" book, which has some VBA in th
back. I think the book has a typo or two, because everytime I run it
it goes into an infinite loop. Where do I make the condition tur
false? This is supposed to go through a Word doc looking for tables
and them applying the autoformat to each of them.

Sub FormatTableContemporary()
Selection.HomeKey Unit:=wdStory
Application.Browser.Target = wdBrowseTable
MoreTables = True
Do While MoreTables = True
Application.Browser.Next
On Error Resume Next
Selection.Tables(1).Select
If Err = 1594 Then
MoreTables = False
Else
Selection.Tables(1).AutoFormat
Format:=wdTableFormatContemporary, _
applyborders:=True, _
applyshading:=True, _
applyfont:=True, _
applycolor:=True, _
applyheadingrows:=True, _
applylastrow:=False, _
applyfirstcolumn:=True, _
applylastcolumn:=False, _
AutoFit:=True
Selection.MoveDown Unit:=wdLine, Count:=1
Loop
End Sub

Also, I like to type things in lower case, knowing the VB editor wil
capitalize words it recognizes as needed. Sort of a spellcheck fo
myself. This one only capitalized a few of the properties. I
something wrong?

Thanks for your help
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

I would do it this way:

Dim atable As Table
For Each atable In ActiveDocument.Tables
atable.AutoFormat Format:=wdTableFormatContemporary, _
applyborders:=True, _
applyshading:=True, _
applyfont:=True, _
applycolor:=True, _
applyheadingrows:=True, _
applylastrow:=False, _
applyfirstcolumn:=True, _
applylastcolumn:=False, _
AutoFit:=True
Next atable


--
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
 
J

Jonathan West

Hi Denapple

Doug's answer is correct. Your immediate problem was that this line

Selection.Tables(1).AutoFormat

should have a line continuation character on the end of it.

You'll find it much easier to understand code if you format it well, with
code inside loops indented. Take a look here for some more tips on making
your code easier to understand & maintain.

The art of defensive programming
http://word.mvps.org/FAQs/MacrosVBA/MaintainableCode.htm
 

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