Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    table code testing 2K to XP (O2K and XP)

    Hi everyone -

    I have a macro that works fine in 2000, but seems to behave differently when run in Word XP. Can a few of you test this in one or the other to verify the behavior?

    Here's the code where I think things go wacky:

    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= 2, DefaultTableBehavior:=wdWord8TableBehavior
    Selection.Tables(1).PreferredWidth = InchesToPoints(5)

    In 2000, this inserts a table that spans the width of the current page, and then shrinks it down to 5 inches in overall width. This doesn't seem to happen for my XP user.

    I'm sure this has to do with how XP thinks about tables. I'm guessing that the PreferredWidth tag doesn't behave the same. How do I achieve the same objective in XP?

    Thanks,
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: table code testing 2K to XP (O2K and XP)

    The code works OK for me on Word 2002 SP-2. I get a table of 5 inches overall width.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: table code testing 2K to XP (O2K and XP)

    Works for me, too, running the two commands in the VBE's Immediate window. Is there any circumstance under which the insertion point would not be in the table after inserting it? (I guess the user would tell you there was an error message if that were the case.) Nested tables are more difficult to debug; if the user is somehow already in a table of someone else's design at the time this is run, who knows what might happen?

  4. #4
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: table code testing 2K to XP (O2K and XP)

    Thanks for testing. In the full macro, I test to see if the cursor's in a table and move it out if necessary.

    The problem must be somewhere else in the code. Here's a bit more context:

    Dim aTable
    With Selection
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:=2, DefaultTableBehavior:=wdWord8TableBehavior
    Selection.Tables(1).PreferredWidth = InchesToPoints(5)
    Set aTable = Selection.Tables(1)
    aTable.PreferredWidthType = wdPreferredWidthAuto
    aTable.PreferredWidth = 0
    aTable.AllowPageBreaks = False
    End With

    I can't remember now why I included the .PreferredWidthType and .PreferredWidth = 0 lines, so maybe they aren't necessary. I think I was trying to match the formatting of an existing table, or perhaps I recorded those statements. The table works fine in 2000 with those lines commented out.

    Bob
    Writing Consultant / Word Template Designer
    Words Connect

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: table code testing 2K to XP (O2K and XP)

    As a footnote on the With...End With construct, your code could be simplified a bit to this:

    Dim aTable
    With Selection
    ActiveDocument.Tables.Add Range:=.Range, NumRows:=4, NumColumns:=2, DefaultTableBehavior:=wdWord8TableBehavior
    .Tables(1).PreferredWidth = InchesToPoints(5)
    Set aTable = .Tables(1)
    aTable.PreferredWidthType = wdPreferredWidthAuto
    aTable.PreferredWidth = 0
    aTable.AllowPageBreaks = False
    End With

    Alternatively, you can create the object reference to the table earlier in your code:

    Dim aTable As Word.Table
    Set aTable = ActiveDocument.Tables.Add( _
    Range:=Selection.Range, _
    NumRows:=4, NumColumns:=2, _
    DefaultTableBehavior:=wdWord8TableBehavior)
    With aTable
    .PreferredWidth = InchesToPoints(5)
    .PreferredWidthType = wdPreferredWidthAuto
    .PreferredWidth = 0
    .AllowPageBreaks = False
    End With
    ' Other stuff with aTable
    If Not (aTable Is Nothing) Then Set aTable = Nothing

  6. #6
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: table code testing 2K to XP (O2K and XP)

    Thanks Jefferson. I've been cutting and pasting stuff into that macro from a couple of different sources, and I hadn't gone back to look for ways to simpliffy. That's one of those tasks that I let slide too often. <sigh>

    I just heard back from my XP user, and apparently commenting out those PreferredWidth options fixed the problem. I wonder if in XP the PreferredWidth is a bit more, er, strongly preferred than in 2000.

    Thanks again!
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

Posting Permissions

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