Insert a column into all tables of the current document using VBA

Dear Experts:

I would like to add an additional column between the third and fourth column of all the tables of the current document (using VBA). All the tables have 4 columns

Moreover the newly added column should have a page field '{page}' in all of the cells , vertical and horizontal aligment is to be centered.

Help is much appreciated. Thank you very much in advance. Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Andreas

Try this.
Option Explicit

Sub InsertColumn()
Dim doc As Document
Dim tbl As Table
Dim col As Column
Dim cl As Cell
Dim rng As Range

    Set doc = ActiveDocument
    
    For Each tbl In doc.Tables
        tbl.Columns.Add tbl.Columns(4)
        Set col = tbl.Columns(4)
        
        col.Cells.VerticalAlignment = wdCellAlignVerticalCenter
        
        For Each cl In col.Cells
            Set rng = cl.Range
            rng.End = rng.End - 1
            doc.Fields.Add Range:=rng, Type:=wdFieldEmpty, Text:="PAGE  ", PreserveFormatting:=True
        Next cl
        
    Next tbl
    
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredCommented:
Hi Andreas.
When I saw the text of this question, I guessed that it was from you:
Sub InsertColumn()
    Dim tbl As Table
    Dim col As Column
    Dim cl As Cell
    Dim rw As Row
    Dim rng As Range
    
    For Each tbl In ActiveDocument.Tables
        Set col = tbl.Columns(4)
        tbl.Columns.Add col
        For Each rw In tbl.Rows
            Set rng = rw.Cells(4).Range
            rng.MoveEnd wdCharacter, -1
            ActiveDocument.Fields.Add rng, wdFieldEmpty, "Page", False
            rng.ParagraphFormat.Alignment = wdAlignParagraphCenter
            rng.Cells.VerticalAlignment = wdCellAlignVerticalCenter
        Next rw
    Next tbl
End Sub

Open in new window

0
GrahamSkanRetiredCommented:
Snap (almost)
Congratulation Norie. You were 27 seconds faster
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Máté FarkasDatabase Developer and AdministratorCommented:
Something like this:
Sub AddColumns()
    Dim T As Word.Table
    Dim R As Word.Row
    
    For Each T In ActiveDocument.Tables
        T.Columns(T.Columns.Count).Select
        Selection.InsertColumnsRight
        For Each R In T.Rows
            R.Cells(R.Cells.Count).Select
            Selection.TypeText Text:="{page}"
        Next
    Next T
End Sub

Open in new window

0
Andreas HermleTeam leaderAuthor Commented:
Dear both,

thank you very much to both of you for your swift and professional help. Interesting to have a look at the different approaches. Both work just fine.

Although Norie was a little bit quicker and therefore all points should be awarded to him, I will also award some points to Graham, since he also considered my 3rd requirement, i.e. the horizontal alignment of the page field. I know it was inadvertently overlooked by Norie.

Again, thank you very much for your superb help. This saves me hours of tedious work.

Thank you, Regards, Andreas
0
Andreas HermleTeam leaderAuthor Commented:
Norie hope you do not mind, splitting the points.

Again, thank you very much for your great help and the time taken.
0
NorieAnalyst Assistant Commented:
Andreas

Not a problem, I realised I'd missed the horizontal alignment right after I posted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.