Setting properties for multiple table cells, vba

F

Fred Holmes

Word 2003, WinXP, SP2

The following code works just fine to set properties in a single cell,
setting only the first cell if multiple cells have been selected.

With Selection.Cells(1) ' Processes single cell.
.TopPadding = InchesToPoints(0.02)
.BottomPadding = InchesToPoints(0#)
.LeftPadding = InchesToPoints(0.05)
.RightPadding = InchesToPoints(0.05)
.WordWrap = True
.FitText = False
End With


I'm trying to get the code to run on all selected cells. The
following code and other, simliar tries bombs.

Dim c As Cell
For Each c In Selection.Cells
.TopPadding = InchesToPoints(0.02)
.BottomPadding = InchesToPoints(0#)
.LeftPadding = InchesToPoints(0.05)
.RightPadding = InchesToPoints(0.05)
.WordWrap = True
.FitText = False
Next c

"Compile error:
Invalid or unqualified reference"

Thanks for your help,

Fred Holmes
 
H

Helmut Weber

Hi Fred,
a workaround only:
Sub Test143()
Dim r As Integer ' row
Dim c As Integer ' column
Dim cStart As Integer ' column start
Dim cEnd As Integer ' column end
Dim rStart As Integer ' range start
Dim rEnd As Integer ' range end
Dim oCll As Cell
cStart = Selection.Information(wdStartOfRangeColumnNumber)
cEnd = Selection.Information(wdEndOfRangeColumnNumber)
rStart = Selection.Information(wdStartOfRangeRowNumber)
rEnd = Selection.Information(wdEndOfRangeRowNumber)
For r = rStart To rEnd
For c = cStart To cEnd
Selection.Tables(1).Cell(r, c).Select
With Selection.Cells(1)
.TopPadding = InchesToPoints(0.02)
.BottomPadding = InchesToPoints(0#)
.LeftPadding = InchesToPoints(0.05)
.RightPadding = InchesToPoints(0.05)
.WordWrap = True
.FitText = False
End With
Next
Next
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
K

Klaus Linke

Hi Fred,

Not too sure, but maybe try

Dim myCell As Cell
For Each myCell In Selection.Cells
myCell.TopPadding = _
Selection.Tables(1).TopPadding
myCell.BottomPadding = _
Selection.Tables(1).BottomPadding
myCell.LeftPadding = _
Selection.Tables(1).LeftPadding
myCell.RightPadding = _
Selection.Tables(1).RightPadding
Next myCell

This should set the cells back to the table default. It may be slow for
large tables... Maybe there's a quicker way, perhaps using WordBasic.

Regards,
Klaus
 

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