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?
 
NorieConnect With a Mentor VBA ExpertCommented:
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
 
GrahamSkanConnect With a Mentor RetiredCommented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
 
NorieVBA ExpertCommented:
Andreas

Not a problem, I realised I'd missed the horizontal alignment right after I posted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.