Inserting AutoText row at end of table via macro

C

clh

I am trying to insert a table row (which has been saved
as an AutoText entry) at the end of a specific table via
macro. The row contains text and fields, so I can't just
add a row at the end of the table in the normal way.

At the top of the table I want to add the row to is a
checkbox which my macro is linked to. If the checkbox is
checked, when the user tabs out of the checkbox, the
macro will run and the new row needs to be added using
the AutoText entry. If the checkbox is not checked,
nothing should happen when the user tabs out of the
checkbox.

When I run the template and the "check" the checkbox, I
end up with an error on the line of code that is supposed
to insert the AutoText entry. I can't figure out why I'm
getting the error. The AutoText entry is saved to the
template I am working on, not to the Normal template.

Here's my code for this macro:

- - - - - - - - - - - - - - - - - - - - - - - -
Sub Add1Row()

Dim intRowsInTable As Integer

' If checkbox is checked, add AutoText row to end of
table 5
If ActiveDocument.FormFields("checkbox1").CheckBox.Value
= True Then

' Unlock the document
ActiveDocument.Unprotect
' Count the number of rows in the table
intRowsInTable = ActiveDocument.Tables(1).Rows.Count

' Select the first column in the last row
ActiveDocument.Tables(1).Cell(intRowsInTable,
1).Select
Selection.MoveDown unit:=wdLine, Count:=1
Selection.InsertRows 1

' Insert AutoText entry
ActiveDocument.AttachedTemplate.AutoTextEntries
("AddRowATEntry").Insert where:= _
Selection.Range, RichText:=True

' Lock the document
ActiveDocument.Protect Type:=wdAllowOnlyFormFields

End If

' If checkbox is not checked, do nothing
If ActiveDocument.FormFields("checkbox1").CheckBox.Value
= False Then
' Nothing
End If

End Sub

- - - - - - - - - - - - - - - - - - - - - - - -

I did get some help in response to my initial post, but
I'm still doing something wrong (obviously). Any help
would be GREATLY appreciated!!!

Thanks!

Cheryl
 
A

Astrid

Hi Cheryl,

That's because the cursorposition is inside a row and therefor can't add an autotextentry with a row. Your command
Selection.InsertRows 1
adds a new row where the cursor is in.
Just position the cursor under the table and it should work.

If you step the code in the VBE with F8, you execute one line at a time. If you position both VBE and document on your screen, you can see exactly what happens at each line. This helps me always in debugging.
------------------------------------------------------------------
Sub Add1Row()
Dim oTable As Table

If ActiveDocument.FormFields("checkbox5").CheckBox.Value = True Then
Set oTable = ActiveDocument.Tables(1)
ActiveDocument.Unprotect
'Position cursor under table
oTable.Range.Rows(oTable.Rows.Count).Range.Select
Selection.Collapse direction:=wdCollapseEnd
'Insert AutoText entry
ActiveDocument.AttachedTemplate.AutoTextEntries("A").Insert _
where:=Selection.Range, RichText:=True
' Lock the document
ActiveDocument.Protect Type:=wdAllowOnlyFormFields

End If
Set oTable = Nothing

End Sub
------------------------------------------------------------------

In the example above I've used the Selection object like you did. If you want the cursor to hide instead of flickering when selecting, try a range object. The code below does exactly the same but without selecting text in the document:
------------------------------------------------------------------
Sub AddRowAlternative()
Dim oTable As Table
Dim oRange As Range

If ActiveDocument.FormFields("checkbox5").CheckBox.Value = True Then
Set oTable = ActiveDocument.Tables(1)
ActiveDocument.Unprotect
'Position cursor under table
Set oRange = oTable.Range.Rows(oTable.Rows.Count).Range
oRange.Collapse direction:=wdCollapseEnd
'Insert AutoText entry
ActiveDocument.AttachedTemplate.AutoTextEntries("A").Insert _
where:=oRange, RichText:=True
' Lock the document
ActiveDocument.Protect Type:=wdAllowOnlyFormFields

End If

Set oTable = Nothing
Set oRange = Nothing
End Sub
------------------------------------------------------------------

One other note. You say the autotextentry contains a row with fields. Because you unprotect the document, I guess we're talking about formfields here. Formfields are identified by their name - the bookmark. Note that you can only have formfield with identical bookmarks. This wouldn't be a problem, only if you depend on the names if you use them in code or use REF fields to show their result in other places in the documents.

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/
 

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