Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jul 2002
    Location
    Hampshire, United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a word macro that reformats tables, setting things like line widths, line colours, background colours, etc. Occasionally word will crash repeatedly when trying to format a particular table. I have managed to isolate the failing code to a single line and can reproduce it at will with an appropriate table. Typing the following into the VBA immediate window in word 2007 for the attached table produces the crash for me:

    Selection.Tables(1).Borders(wdBorderHorizontal).Li neWidth = wdLineWidth050pt

    It only seems to occur with Word 2007, 2003 works fine.

    I would be interested to know if anyone has any suggestions for a workaround. There is obviously some table setting that causes this problem as the problem does not occur if I first convert the table to text and then back again, but I need a workaround that I can put into my macro. Any suggestions?

    Thanks

    Pete
    Attached Files Attached Files

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    I think the problem is related to some inconsistency between the rows. If you run this little diagnostic in your document, you see that the Line Style for the horizontal border is 9999999 (also known as wdUndefined):

    Code:
    Sub ListBorders()
    Dim tbl As Word.Table
    With ActiveDocument
        Set tbl = .Tables(1)
        With .Content
            .InsertAfter vbCrLf
            .Paragraphs(.Paragraphs.Count).TabStops.Add Position:=InchesToPoints(2.5), Alignment:=wdAlignTabRight
            .Paragraphs(.Paragraphs.Count).TabStops.Add Position:=InchesToPoints(3.5), Alignment:=wdAlignTabRight
            .InsertAfter "BorderConstant" & vbTab & "LineStyle" & vbTab & "LineWidth" & vbCrLf
            .InsertAfter "wdBorderTop" & vbTab & tbl.Borders(wdBorderTop).LineStyle & vbTab & tbl.Borders(wdBorderTop).LineWidth & vbCrLf
            .InsertAfter "wdBorderLeft" & vbTab & tbl.Borders(wdBorderLeft).LineStyle & vbTab & tbl.Borders(wdBorderLeft).LineWidth & vbCrLf
            .InsertAfter "wdBorderBottom" & vbTab & tbl.Borders(wdBorderBottom).LineStyle & vbTab & tbl.Borders(wdBorderBottom).LineWidth & vbCrLf
            .InsertAfter "wdBorderRight" & vbTab & tbl.Borders(wdBorderRight).LineStyle & vbTab & tbl.Borders(wdBorderRight).LineWidth & vbCrLf
            .InsertAfter "wdBorderHorizontal" & vbTab & tbl.Borders(wdBorderHorizontal).LineStyle & vbTab & tbl.Borders(wdBorderHorizontal).LineWidth & vbCrLf
            .InsertAfter "wdBorderVertical" & vbTab & tbl.Borders(wdBorderVertical).LineStyle & vbTab & tbl.Borders(wdBorderVertical).LineWidth & vbCrLf
            .InsertAfter "wdBorderDiagonalDown" & vbTab & tbl.Borders(wdBorderDiagonalDown).LineStyle & vbTab & tbl.Borders(wdBorderDiagonalDown).LineWidth & vbCrLf
            .InsertAfter "wdBorderDiagonalUp" & vbTab & tbl.Borders(wdBorderDiagonalUp).LineStyle & vbTab & tbl.Borders(wdBorderDiagonalUp).LineWidth & vbCrLf
        End With
    End With
    Set tbl = Nothing
    End Sub
    Perhaps you can address the problem by first assigning .LineStyle=wdLineStyleSingle?

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I used to use a long series of code to format tables but found that each individual border command was noticeably slow. Nowdays you can replace them all with a simple table style assuming you have modified a style to suit your purpose. This much faster and less error prone.

    Selection.Tables(1).Style = "Table Normal"
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    New Lounger
    Join Date
    Jul 2002
    Location
    Hampshire, United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for both these responses. The diagnostic code was very helpful in seeing what was happening and looking for wdUndefined for a border linestyle will help identify tables that have a potential problem in my macro. But no matter what I did I to set the linestyle in the failing table for the wdBorderHorizontal components, despite it looking correct, it still returned wdUndefined in the diagnostic. However with Andrew's suggestion of using table styles I found that this did actually remove all the instances of wdUndefined. My formatting macro provides more sophisticated control over the final table than what I can achieve with table styles, but now that I have a way for the macro to detect problem tables and fix problem tables I can let my macro do it's stuff. So thank you once again.

    Pete

  5. #5
    WS Lounge VIP
    Join Date
    Mar 2006
    Location
    Maryland, USA
    Posts
    690
    Thanks
    17
    Thanked 66 Times in 56 Posts
    Quote Originally Posted by Andrew Lockton View Post
    I used to use a long series of code to format tables but found that each individual border command was noticeably slow. Nowdays you can replace them all with a simple table style assuming you have modified a style to suit your purpose. This much faster and less error prone.

    Selection.Tables(1).Style = "Table Normal"
    Applying Table Normal or an unmodified Table Grid is the same as clearing all table formatting. It's a good first step before formatting any table—if you didn't create it and don't know who did what to it, which is usually my situation.

    Pam
    Pam Caswell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •