+ Reply to Thread
Results 1 to 7 of 7

Loop trough table cells, identify alignment and indent according to conditions (VBA)

  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    Gothenburg, Sweden
    MS-Off Ver
    Excel 2007/Excel 2010
    Posts
    14

    Loop trough table cells, identify alignment and indent according to conditions (VBA)

    Hi,

    I have a bit of experience writing macros for excel, but this is my first time trying to write one for microsoft word, so I thought I'd ask for some help.

    I want to write a macro which loops through all cells in all tables in a document. I want the macro to identify if the text in the table cell is left, center or right aligned. If the text is left or center aligned, I don't want the macro to do anything at all, but if it is right aligned I want the macro to identify the width of the specific cell and left indent the text in the cell with 1/3 of its total width.

    This seems quite straightforward and I would have no problem at all writing this macro in excel in five minutes, however, the objects in word is making me utterly confused. I'm not even sure if you can check the alignment of text in a cell as the alignment is a paragraph property?

    Cheers,

  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: Loop trough table cells, identify alignment and indent according to conditions (VBA)

    Hi christensen,

    It's not clear whether the 1/3 indent refers to the cell width or the text width. The following macro provides for both. If you want to use the text width, un-comment all the commented-out code and delete/comment out the line '.LeftIndent = TblCell.Width / 3'. I've also assumed you want to change the right-aligned text to left-aligned; otherwise the indenting doesn't seem to make much sense (the most it would do is force a line wrap).
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    06-12-2012
    Location
    Gothenburg, Sweden
    MS-Off Ver
    Excel 2007/Excel 2010
    Posts
    14

    Re: Loop trough table cells, identify alignment and indent according to conditions (VBA)

    Thanks! That was exactly what I needed. Tweaked it a bit, and it worked like a charm.

    ---------- Post added at 10:22 AM ---------- Previous post was at 10:12 AM ----------

    However, on the same note, I tried to write a script that inserts a page break when it hits all cells with the font size 11, but the loop ends if the first cell in the table (the document consists of just one big table, which this script is supposed to fix automatically), which kind of makes sense, because before the insertion of page breaks, there's just one table in the document (so looping through all the tables won't make much sense.

    Please Login or Register  to view this content.
    So, basically, my question is, is there anyway to skip the very first cell in the table with an if-condition of some sort?

    Cheers,

  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: Loop trough table cells, identify alignment and indent according to conditions (VBA)

    Before we go down that path:
    a) do you realise that the page break will break the table into two separate tables?
    b) what effect are you trying to achieve?

  5. #5
    Registered User
    Join Date
    06-12-2012
    Location
    Gothenburg, Sweden
    MS-Off Ver
    Excel 2007/Excel 2010
    Posts
    14

    Re: Loop trough table cells, identify alignment and indent according to conditions (VBA)

    a) Yes
    b) When the tables are pasted in Word from Excel (we're talking about probably 50-60 tables) they will be pasted as one table. I want to break this large table (that actually consists of many small tables) into the intended small, separate tables, without having to insert a page break at each table heading (font size 11) manually.

  6. #6
    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: Loop trough table cells, identify alignment and indent according to conditions (VBA)

    OK, try the following:
    Please Login or Register  to view this content.
    Note: If you omit '.Range.Characters.Last.Next.Next.InsertBreak wdPageBreak' the tables will be split but there won't be a page break between them. This, plus some additional code to keep all of each table on the same page, can be useful if you could have tables that can fit on the same page as others.

  7. #7
    Registered User
    Join Date
    06-12-2012
    Location
    Gothenburg, Sweden
    MS-Off Ver
    Excel 2007/Excel 2010
    Posts
    14

    Re: Loop trough table cells, identify alignment and indent according to conditions (VBA)

    Brilliant! Thank you very much!

    Cheers,

+ 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