none
setting preferred width of table in word ONLY works with breakpoint RRS feed

  • Question

  • I am running Office 2007 in Windows XP Professional.

    I have a Macro, written in Excel VBA, which is used to import data from an excel spreadsheet into a Word document. For one particular table, I want most of the columns to be autofitted to their contents, except that I want the fifth column to be a particular width, so that when printed, it has room for hand-written comments.

    In the following segment of code, myWordDoc is a variable that I set to the word document I am editing earlier in the code, and myTable is a variable that I set to the table I'm editing, at the time when I insert it, and in this case has 7 columns:

    myWordDoc.Sections(6).Range.Font.Name = "Calibri"
    myTable.Range.Font.Size = 8
    myTable.Range.ParagraphFormat.Space1
    myTable.AutoFitBehavior(1)
    myTable.Columns.AutoFit
    myWordDoc.Sections(6).Range.Paragraphs(1).Range.Font.Size = 10
    myTable.Columns(5).PreferredWidthType = 2
    myTable.Columns(5).PreferredWidth = 25
    myTable.Columns.Last.AutoFit
    myWordDoc.Save

    This segment of code works only if I add a breakpoint on the line that reads

    myTable.Columns(5).PreferredWidth = 25

    If I let it run up to the breakpoint, then just click the run button again and let it finish, it works perfectly. If I do not add a breakpoint, the fifth column retains its autofitted width, which is very narrow, because there is no text in it. Why would this happen?!?! Any thoughts or suggestions are greatly appreciated.

    Thanks,

    -L

    Friday, April 11, 2014 4:27 PM