Results 1 to 12 of 12
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Manipulating Tables with VBA (Word XP)

    Greetings. I have been working on some code and I've found some limitations (and perhaps downright obfuscation in the Help File) that I'd like to share.

    First, I spent quite some time cursing the Microsoft gods for what seems to be an egregious deception when trying to implement the .Split method on the first row of a table. (I had a document with a table as the very first thing on page one and I needed to put a carriage-return/VBCRLF in front of it.) After wasting time typing and stepping through umpteen variations on the code that supposedly should do the job (but which threw a "bad parameter" error anyway), I discovered that you can't use ranges in VBA to split a table at row 1! (Sorry if I seem frustrated, but I couldn't find mention of that caveat anywhere, and I wasted a lot of good trick or treating time til i found out.) So, even though you can put your cursor in cell one/row one and split it manually, you apparently can't do that through code without using the selection object.

    Similary, I could find no equivalent command for Table: Insert > Column to the Right that can use the range object (instead of the selection object.)

    Has anyone been up against these sorts of issues before? Is there a way to work around them without resorting to the Selection object?
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  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

    Re: Manipulating Tables with VBA (Word XP)

    For the first problem, you are trying to insert a blank paragraph before the table? Perhaps you can use .InsertBefore type methods for that?

    For the second problem, is there an .Add method for the table's Columns collection? If so, does it have the same effect?

  3. #3
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Manipulating Tables with VBA (Word XP)

    Unfortunately, in the first situation, the type method ends up putting the text (or paragraph return) in the first cell. If the table is the first thing in the document, the object ActiveDocument.Paragraphs(1) refers to the text-range in row 1, column 1. I couldn't stand the heat, so I got out of the kitchen by setting my working-range to the text that was to be converted into a table; then I added an extra paragraph in front of it, using the .InsertBefore method, and then I used the .MoveStart method to moved the start of the range (which by now included the new paragraph at the beginning) down one paragraph.

    In the second situation, the .Add method requires a BeforeColumn argument (which makes it more of an .Insert method, don't you think?) and it was NOT happy when I tried to fake it out and gave it a column number equal to the number of columns in the table +1. In that instance, I just gave up and used the Selection object. (Pick yer battles, ya know?)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  4. #4
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Manipulating Tables with VBA (Word XP)

    Had the same problem with a table as the first thing on page 1. Simplest solution I found: select the entire table, cut it, put 1 or more paragraphs at the top, place the cursor in the second paragraph or lower, as required, and paste the table back into the document.

  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: Manipulating Tables with VBA (Word XP)

    I would surrender on the first one, too, but on the second one, the parameter is optional. If omitted, it should add to the end by default.

  6. #6
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Manipulating Tables with VBA (Word XP)

    Most excellent! And it works with my range object, too! (It's the little things in life...)

    Thanks.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Manipulating Tables with VBA (Word XP)

    I get to add content above a table at the start of a document by either of two methods.
    1. Select the table and drag n drop it lower down the page (somewhere after the first trailing paragraph mark
    2. Put my cursor at the top of the table and press Ctrl-Enter and then backspace. This puts in a hard page break followed by an empty paragraph above the table. Pressing backspace gets rid of the hard page break but leaves the empty paragraph.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Manipulating Tables with VBA (Word XP)

    It's slightly simpler to place the cursor at the beginning of the first cell of the table and type <Enter> and an Up Arrow. Enter at the beginning of the first cell of a table is the same as selecting Split Table from the Table menu.

    But of course my solution is exactly what doesn't work for the original poster, when he is trying to manipulate ranges in code.

    StuartR

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Manipulating Tables with VBA (Word XP)

    Hi Andrew:
    I still use Word 2000, but can't you put a paragraph mark above the table just by putting the cursor at the beginning of the table & pressing <enter>>? That always works in Word 2000.

  10. #10
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Manipulating Tables with VBA (Word XP)

    Thanks Stuart and Phil

    I didn't know that would work. Isn't it funny how you can use software for so long and still not know very simple things. <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

    Of course, we aren't helping Mike to solve his problem but at least I've learnt something today.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  11. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Manipulating Tables with VBA (Word XP)

    Hi Mike,

    For problem 1, you could avoid selecting by doing something along the lines of:

    Sub PreTablePara()
    If ActiveDocument.Tables.Count > 0 Then
    With ActiveDocument.Tables(1)
    .Rows.Add BeforeRow:=ActiveDocument.Tables(1).Rows(1)
    With .Rows(1)
    .Cells.Merge
    .ConvertToText
    End With
    End With
    End If
    End Sub

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  12. #12
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Manipulating Tables with VBA (Word XP)

    Thanks Stuart. Learned something new here. Again...!

Posting Permissions

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