Need macro to locate table uniquely, insert blank row and date and end table

M

Marrick13

I have a Word template that contains one table of 1-4 rows and 4 columns (the
table is to record the document's revision history). I have a macro that
inserts a blank row and the current date in the first column of that row, but
if I run the macro again, the date goes outside the table. The macro is
executed as an AutoNew command so that it runs as soon as a document is
created from the template. It works fine for that one-time run at document
creation.

However, once the new document has been created, there will be a need to run
a macro to insert a blank row in the same table. If this macro is run twice,
it puts the date outside the table because the cursor is still in the table.
Also, there may be another one or more tables in the document when it is
edited, and I would prefer to have the macro locate only that table by some
unique identifier, such as a bookmark. I've seen code that locates tables
according to specific text as well as the first table in a document (as shown
below), but I have not been able to determine if there is a way to find a
table uniquely.

The row insertion code is:

ActiveDocument.Tables(1).Rows.Add
rownum = ActiveDocument.Tables(1).Rows.Count

Date insertion code is:

Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False

So, can someone help me with a macro that will:

1. Find a table by a unique identifier
2. Insert a blank row at the end and the current date in its first column
3. Be able to do 1 and 2 more than once in a document created from a template.


Thank you.
 
J

Jezebel

Use a bookmark to identify the table (ie open the template, select the
table, define a bookmark).

With ActiveDocument.Bookmarks("DocHistory").Range.Tables(1)
.Rows.Add
.Cell(.Rows.Count, 1) = format(Now, "yyyy-mm-dd")
End with
 
M

Marrick13 via OfficeKB.com

Thank you, Jezebel. Only trouble is that I get a compile error with "Invalid
use of property" message on the .Cell(.Rows.Count, 1) = format(Now, "yyyy-mm-
dd") line. I don't understand the property well enough to debug, can you
assist?
Use a bookmark to identify the table (ie open the template, select the
table, define a bookmark).

With ActiveDocument.Bookmarks("DocHistory").Range.Tables(1)
.Rows.Add
.Cell(.Rows.Count, 1) = format(Now, "yyyy-mm-dd")
End with
I have a Word template that contains one table of 1-4 rows and 4 columns
(the
[quoted text clipped - 42 lines]
Thank you.
 
J

Jezebel

My mistake. Should be

.Cell(.Rows.Count, 1).Range = format(Now, "yyyy-mm-dd")


Marrick13 via OfficeKB.com said:
Thank you, Jezebel. Only trouble is that I get a compile error with
"Invalid
use of property" message on the .Cell(.Rows.Count, 1) = format(Now,
"yyyy-mm-
dd") line. I don't understand the property well enough to debug, can you
assist?
Use a bookmark to identify the table (ie open the template, select the
table, define a bookmark).

With ActiveDocument.Bookmarks("DocHistory").Range.Tables(1)
.Rows.Add
.Cell(.Rows.Count, 1) = format(Now, "yyyy-mm-dd")
End with
I have a Word template that contains one table of 1-4 rows and 4 columns
(the
[quoted text clipped - 42 lines]
Thank you.
 
M

Marrick13 via OfficeKB.com

Jezebel,
That did the trick. Thanks so much!!
My mistake. Should be

.Cell(.Rows.Count, 1).Range = format(Now, "yyyy-mm-dd")
Thank you, Jezebel. Only trouble is that I get a compile error with
"Invalid
[quoted text clipped - 16 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top