Yancey
Yancey

Reputation: 1

Adding a column to my Word table fails with run-time error 5941

When I run this VBA code in macros, it reports

"Run-time error '5941': The requested member of the collection does not exist"

on the highlight line

Sub addColumn()
'
' addColumn
'
'
Application.Templates.LoadBuildingBlocks
Dim myDoc As Document
Set myDoc = Word.ActiveDocument
Dim myTable As Table
With myDoc
  iCount = myDoc.Tables.Count
  For i = 1 To iCount
    Set myTable = myDoc.Tables(i)
    myTable.Columns.Add BeforeColumn:=myTable.Columns(6)
  Next
End With
End Sub

Could anyone give me some advice?

I wrote this code for dealing with a word.doc which contains lots of tables(606 tables) and other characters. I want to use macros to add a column at the end of each table in the batch. Did I miss something?

enter image description here

Upvotes: 0

Views: 70

Answers (1)

macropod
macropod

Reputation: 13515

If all you want to do is to add a new column after the last column in each table, all you need is:

Sub Demo()
Application.ScreenUpdating = False
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
  Tbl.Columns.Add
Next
Application.ScreenUpdating = True
End Sub

Upvotes: 1

Related Questions