Yes, this is possible. You would want your macro to first decide if it is in
a table. Then, if it is, create an additional row below. I use such a macro
in protected forms to unprotect the form, create a new row, add form fields
in the row, and then reprotect the form and select the first field in the
new row for user input.
If not in a table, then you would want it to create a table for you. I would
suggest that rather than coding the creation of the table, create the table
you want manually and then save that table as an AutoText entry. Have your
macro insert the AutoText. This makes it a lot easier to modify your table
settings in the future (or at least it would be easier for me).
The following macro is to add a row and insert appropriate AutoText entries:
Private Sub AddRow97(Optional lTable As Long = 2)
'
' AddRow97 Macro
' Macro recorded 11/24/03 by Charles Kyle Kenyon
'
Dim oTemplate As Template
Set oTemplate = Templates(ThisDocument.FullName)
Dim sAutoTextEntry1 As String
Dim sAutoTextEntry2 As String
If lTable = 3 Then
sAutoTextEntry1 = "zExpenseDescription"
sAutoTextEntry2 = "zExpenseAmount"
Else ' Table 4 - Payments
sAutoTextEntry1 = "zPaymentDescription"
sAutoTextEntry2 = "zPaymentAmount"
End If
'
Dim lRowCount As Long
Dim rRow As Range
UnprotectDocumentMacro
'
lRowCount = ActiveDocument.Range.Tables(lTable).Rows.Count
ActiveDocument.Tables(lTable).Rows(lRowCount - 1).Select
With Selection
.Copy
.Paste
ActiveDocument.Tables(lTable).Rows(lRowCount).Select
.Delete Unit:=wdCharacter, Count:=1
.HomeKey Unit:=wdLine
Application.DisplayAutoCompleteTips = True
With AutoCorrect
.CorrectInitialCaps = True
.CorrectSentenceCaps = True
.CorrectDays = True
.CorrectCapsLock = True
.ReplaceText = True
End With
oTemplate.AutoTextEntries("zDateField").Insert Where _
:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries(sAutoTextEntry1).Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries(sAutoTextEntry2).Insert _
Where:=.Range
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
ActiveDocument.Tables(lTable).Rows(lRowCount).Select
With .Cells
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With ' .Cells
End With ' Selection
ActiveDocument.Range.Tables(lTable).Rows(lRowCount).Select
Selection.HomeKey Unit:=wdLine
ProtectDocumentMacro
End Sub
This is called from other procedures. The parameter tells it which table is
being changed.
It calls protect/unprotect procedures which follow:
Sub UnprotectDocumentMacro()
' Unprotect document
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
End If
End Sub
Sub ProtectDocumentMacro()
If ActiveDocument.ProtectionType <> wdAllowOnlyFormFields Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
End If
End Sub
Hope this is of some help. Note, I wrote these a long time ago and they work
in several of my billing and invoicing templates.
--
Charles Kenyon
Word New User FAQ & Web Directory:
http://addbalance.com/word
Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide)
http://addbalance.com/usersguide
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.