PDA

View Full Version : Table borders - error 5843



BiotechJen
03-27-2013, 08:35 AM
Hello! I am new here - I have written a bit of simple code but am definitely not a programmer, so please be gentle! :hi:

I frequently edit documents from several different people, and the table formatting is never consistent. I am trying to write a quick VBA macro that will automatically format the table to match my most frequently used styles.

It works great, except for the table borders. Often the tables I receive have weird border thicknesses, and it isn't apparent until I either print or zoom in to >120%. I'd love to set all the borders to always be 1/2 point. However, I don't seem to be able to do this using VBA - I consistently get run-time error 5843: "One of the values passed to this method or property is out of range".

To troubleshoot, I tried recording just the border part of the macro to see how Word would write the code. Lo and behold, even though I set all the borders to 1/2 point, the recording sets the horizontal and vertical borders to 3/4 point.

Is there a bug with setting table width programmatically?

Here is my code...it's very rudimentary so please be kind! :)

Sub tableFix()
With Selection.Tables(1)
.AllowAutoFit = False
.PreferredWidth = False
.AllowPageBreaks = False
.Borders(wdBorderTop).Color = wdColorBlack
.Borders(wdBorderLeft).Color = wdColorBlack
.Borders(wdBorderBottom).Color = wdColorBlack
.Borders(wdBorderRight).Color = wdColorBlack
.Borders(wdBorderHorizontal).Color = wdColorBlack
.Borders(wdBorderVertical).Color = wdColorBlack
.Borders(wdBorderTop).LineWidth = wdLineWidth050pt
.Borders(wdBorderLeft).LineWidth = wdLineWidth050pt
.Borders(wdBorderBottom).LineWidth = wdLineWidth050pt**
.Borders(wdBorderRight).LineWidth = wdLineWidth050pt
.Borders(wdBorderHorizontal).LineWidth = wdLineWidth050pt
.Borders(wdBorderVertical).LineWidth = wdLineWidth050pt
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
With .Rows
.Height = 14.4
.WrapAroundText = False
.AllowBreakAcrossPages = False
End With
End With

With Selection
.Font.Size = 11
.Font.Name = "Arial"
.Paragraphs.LineSpacingRule = wdLineSpaceSingle
.Paragraphs.SpaceAfter = 0
.Paragraphs.SpaceBefore = 0
.Paragraphs.Alignment = wdAlignParagraphCenter
.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End With

End Sub

**Notably, the error is coming on this line - i.e., the previous two worked just fine. Argh. And sometimes when I run the code on different tables, it will stop at one of the other line width lines.

Any suggestions would be very much appreciated! I'm working in Word 2010, Windows 7.

Thanks,
Jen

gmaxey
03-27-2013, 09:31 AM
The problem is the border type line style is undefined

Debug.Print .Boders(wdBorderLeft).LineStyle returns 99999. So if there is no line style defined then you can't define line width:

Sub tableFix()
Dim lngIndex As Long
With Selection.Tables(1)
.AllowAutoFit = False
.PreferredWidth = False
.AllowPageBreaks = False
With .Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleSingle
End With
For lngIndex = -8 To -1
.Borders(lngIndex).Color = wdColorBlack
If .Borders(lngIndex).LineStyle = wdLineStyleSingle Then
.Borders(lngIndex).LineWidth = wdLineWidth050pt
End If
Next lngIndex
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
With .Rows
.Height = 14.4
.WrapAroundText = False
.AllowBreakAcrossPages = False
End With
End With
With Selection.Tables(1).Range
.Font.Size = 11
.Font.Name = "Arial"
.Paragraphs.LineSpacingRule = wdLineSpaceSingle
.Paragraphs.SpaceAfter = 0
.Paragraphs.SpaceBefore = 0
.Paragraphs.Alignment = wdAlignParagraphCenter
.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End With
lbl_Exit:
Exit Sub
End Sub

BiotechJen
03-27-2013, 03:23 PM
Thanks so much Greg - I think that might have done it! No problems yet, but I'll post again if I run into problems.

Jen