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