Share via

Changing table borders via VBA - error 5843

Anonymous
2013-02-12T19:09:26+00:00

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. (I originally tried doing this using a table style, but there were too many things that don't seem to be modifiable using that route.)

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.

Any suggestions would be very much appreciated!

Thanks,

Jen

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2017-02-24T20:16:04+00:00

    Greg - it's interesting you should say "This can be caused by a LineStyle not being defined."

    My code generated the same error:

    Selection.Borders.OutsideLineWidth = wdLineWidth150pt

    Selection.Borders.OutsideColor = RGB(250, 192, 143)

    Selection.Borders.OutsideLineStyle = wdLineStyleDashSmallGap

    When I read your response I moved the last line above the other two and the macro worked.

    (It's for putting a border around images so any whitespace of the image stands off from the page's background white.)

    I haven't tried exiting Word and rebooting, but it's a start! (Now I have to make it work in Outlook!)

    Thanks!

    (I wonder if it worked for the OP.)

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-08-21T14:07:52+00:00

    This can be caused by a LineStyle not being defined.

    You might try:

    If Not .Borders(wdBorderTop).LineStyle = wdLineSingle Then

      .Borders(wdBorderTop).LineSytle = wdLineSingle

    End If

    .Borders(wdBorderTop).LineWidth = wdLineWidth050pt

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-03-19T22:25:36+00:00

    Just bumping this up as it's still a big problem for me and I can't find an answer!

    Thanks!

    Jen

    Was this answer helpful?

    0 comments No comments