How Do I Add A New Row In Word Table?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

How Do I Add A New Row In Word Table?

Post by AlFlo » 05 Jun 2023, 23:39

I'm using AHK V1 and the current version of Microsoft Word (part of the most current Microsoft Office 365 Family edition).

I'm going nuts trying to figure out how to add a new row at the end of the table. Specifically, I add new entries daily to my table, and want to automate data entry via AHK input boxes.

I know the VBA to add a new row is

Code: Select all

WordDoc.Tables.(1).Rows.Add
And I know that the VBA to count the existing number of rows is

Code: Select all

WordDoc.Tables(1).Rows.count
And I know the VBA to DELETE the last row is

Code: Select all

ActiveDocument.Tables(1).Rows.Last.Cells.Delete
And I figured out that this will add a new CELL at the end of the table:

Code: Select all

ActiveDocument := ComObjActive("Word.Application").ActiveDocument

intCount := 1 
tblNew := ActiveDocument.Tables(1) 
ActiveDocument.Tables(1).Rows.Last.Cells.Add
But I can't figure out how to write an AHK script to ADD a new ROW at the end.

And this forum discussion from 2013 adds rows (as well as unhelpful text) to the BEGINNING of the table: https://www.autohotkey.com/board/topic/98894-entering-a-row-into-a-ms-word-table-with-the-help-of-ahk/:

Code: Select all

ActiveDocument := ComObjActive("Word.Application").ActiveDocument

intCount := 1 
tblNew := ActiveDocument.Tables(1) 
rowNew := tblNew.Rows.Add( tblNew.Rows(1) )
Cells := rowNew.Cells
Loop % Cells.Count {
	celTable := Cells.Item(A_Index)
	celTable.Range.InsertAfter( "Cell " . intCount )
	intCount := intCount + 1
}

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How Do I Add A New Row In Word Table?

Post by flyingDman » 06 Jun 2023, 00:33

Try:

Code: Select all

oWord := ComObjActive("Word.Application")
tbl1 := oWord.ActiveDocument.Tables(1)
tbl1.Rows.Last.Select
oWord.Selection.InsertRowsBelow
14.3 & 1.3.7

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: How Do I Add A New Row In Word Table?

Post by AlFlo » 06 Jun 2023, 00:36

flyingDman,

THANK YOU, THANK YOU, THANK YOU!!! I can't tell you how grateful I am for your help!!!

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How Do I Add A New Row In Word Table?

Post by flyingDman » 06 Jun 2023, 12:38

You're welcome.

BTW as this does not appear gaming related, the Moderators might want to move this to the regular V1 Help section.
14.3 & 1.3.7

gregster
Posts: 8999
Joined: 30 Sep 2013, 06:48

Re: How Do I Add A New Row In Word Table?

Post by gregster » 06 Jun 2023, 15:18

flyingDman wrote:
06 Jun 2023, 12:38
BTW as this does not appear gaming related, the Moderators might want to move this to the regular V1 Help section.
Thank you. Good point :thumbup:
Moved.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How Do I Add A New Row In Word Table?

Post by flyingDman » 06 Jun 2023, 23:31

Easier:

Code: Select all

ComObjActive("Word.Application").ActiveDocument.Tables(1).Rows.Add
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”