#  Other Applications & Softwares  > Word Formatting & General >  > [SOLVED] Loop trough table cells, identify alignment and indent according to conditions (VBA)

## christensen

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,

----------


## macropod

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.
```

----------


## christensen

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,

----------


## macropod

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?

----------


## christensen

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.

----------


## macropod

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.

----------


## christensen

Brilliant! Thank you very much!

Cheers,

----------

