+ Reply to Thread
Results 1 to 8 of 8

VBA word (2003) codes to check if a table fits in a single page of the word document

  1. #1
    Registered User
    Join Date
    08-10-2012
    Location
    germany
    MS-Off Ver
    Excel 2003
    Posts
    34

    VBA word (2003) codes to check if a table fits in a single page of the word document

    HI Guys,
    need your help again. I have this word(2003) template where the users type in the number of tables and the number of rows each table should have. And then a button on the template is pressed which triggers a macro and creates those many tables with specified number of rows each table should have. The macro not only creates the tables but also formats it by merging and colouring certian cells and so on. Everything works well untill there is enough space for the whole table to fit on a single page.
    I would like a VBA word (2003) code that checks if the table fits on the page or not, if the table doesnt look like its going to fit, a new pages is to be inserted and then the cursor jumpes to the first line of that newly created page(where the table is to be created). I hope my problem seems clear.

    Thanks in advance
    Sam.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: VBA word (2003) codes to check if a table fits in a single page of the word document

    Hi Sam,

    Why not set the table's paragraphs' 'keep together' and 'keep with next' properties, on all except the last line. That way, if the table won't fit on the current page, it will automatically move to the next page. This will then only ever become an issue if the table is too hight to fit on a table even if it's the only thing on thta page.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    08-10-2012
    Location
    germany
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: VBA word (2003) codes to check if a table fits in a single page of the word document

    Hello Paul Edstein,
    I have done exactly what you said(the codes are below). I think my problem is a bit strange, its got to do with timing.

    the codes below run in a loop for more than one tables. I have explained in three steps.
    Please Login or Register  to view this content.
    Sometimes the codes run so fast that the third step is reachd before word pushes the created table down(when the whole table does not fit on a single page) to fit in the new page.
    That is why i wanted to have an IF loop that checks if theres enough space for the whole table before the first step itself avoiding the second step.

    Cheers,
    Sam

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: VBA word (2003) codes to check if a table fits in a single page of the word document

    Working with selections when you don't need to is very inefficient. Try it this way:
    Please Login or Register  to view this content.
    Note that the last row doesn't get the 'keep' formats.

  5. #5
    Registered User
    Join Date
    08-10-2012
    Location
    germany
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: VBA word (2003) codes to check if a table fits in a single page of the word document

    Hello Paul Edstein,
    Thanks a lot, it seems to work. I will try to create the tables with different configuration and let you know if the codes are working..

    Cheers,
    Sam.

  6. #6
    Registered User
    Join Date
    08-10-2012
    Location
    germany
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: VBA word (2003) codes to check if a table fits in a single page of the word document

    Hello Paul Edstein,
    The codes are working, but there exists some problem still

    Please Login or Register  to view this content.
    can you please suggest if i am doing anything wrong in the third step(as you said "selection" is inefficient). There are a lot of merging of cells to be done, i have provided just a small piece of the whole code here.

    thanks
    Sam.

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: VBA word (2003) codes to check if a table fits in a single page of the word document

    The problem is that you're still trying to work with selections in the table (which my code does not select) after a range object has already been defined. You really should learn to work with range objects:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-10-2012
    Location
    germany
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: VBA word (2003) codes to check if a table fits in a single page of the word document

    Hi Paul Edstein,
    Thanks for the codes.. will incorporate them in mine. I dont think there should be any problem now.

    Thanks once again
    Sam..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1