That is close to a solution I used recently. Four tables have four slightly
different fields and structures. So I have AT enries for the different
fields. My macro unlocks the form, inserts the AutoText entries with the
proper fields in the right place, relocks the form, and selects the first
field just inserted.
Here is one of the macros (assigned a keyboard shortcut and on a toolbar)
Sub AddTimeRow()
'
' AddTimeRow Macro
' Macro written 12/01/2003 by Charles Kyle Kenyon
' Revised 01/16/2004 by Charles Kyle Kenyon
'
' Triggered by F2 key
'
UnprotectDocumentMacro
Dim sTime As String
sTime = ActiveDocument.Bookmarks("TotalIn").Range.Text
If sTime = "0.0" Then
sTime = ActiveDocument.Bookmarks("TotalOut").Range.Text
If sTime = "0.0" Then
ActiveDocument.Bookmarks("TimeTitle").Select
ProtectDocumentMacro
Exit Sub
End If
End If
' Unprotected document
'
'
Application.ScreenUpdating = False
Dim oTemplate As Template
Set oTemplate = Templates(ThisDocument.FullName)
With Selection
.GoTo What:=wdGoToBookmark, Name:="Total1"
.MoveUp Unit:=wdLine, Count:=1
#If VBA6 Then
' Procedure for later versions
.InsertRowsBelow 1
.HomeKey Unit:=wdLine
#Else
' Procedure for Word 97
.InsertRows 1
.HomeKey Unit:=wdLine
.MoveDown Unit:=wdLine, Count:=1
.HomeKey Unit:=wdLine
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
.Extend
.EndKey Unit:=wdLine
.MoveRight Unit:=wdCharacter, Count:=3
.Copy
.Delete Unit:=wdCharacter, Count:=1
.MoveUp Unit:=wdLine, Count:=1
.Paste
.MoveDown Unit:=wdLine, Count:=1
#End If
' oAutoText("TimeLine").Insert
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("zTimeDescription").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeOutOfCourt").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeInCourt").Insert _
Where:=.Range
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
End With
Application.ScreenUpdating = True
Application.ScreenRefresh
ProtectDocumentMacro
End Sub
It calls the UnProtectDocumentMacro and ProtectDocumentMacro. It has
separate code for Word 97 and later versions.
The macro below inserts a row above the current row. It has to sense which
table it is in to choose the proper fields.
Sub InsertRowAboveMe()
'
' InsertRowAboveMe Macro
' Macro written 12/01/03 by Charles Kyle Kenyon
'
' Unprotect document
UnprotectDocumentMacro
'
'
Dim sAutoTextEntry1 As String
Dim sAutoTextEntry2 As String
Dim oTemplate As Template
Set oTemplate = Templates(ThisDocument.FullName)
'
With Selection
.SelectRow
' Test for Table 2
' -------------------------Table 2 - Time -----------------
If ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count = 2 Then
' Test for time row (4 columns)
If .Columns.Count = 4 Then
'
'' Add row if in a table
' If Selection.Information(wdWithInTable) = True Then
' Selection.Rows.Add BeforeRow:=Selection.Rows(1)
' End If
.InsertRows 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("zTimeDescription").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeOutOfCourt").Insert _
Where:=.Range
.MoveRight Unit:=wdCell
oTemplate.AutoTextEntries("zTimeInCourt").Insert _
Where:=.Range
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
.MoveLeft Unit:=wdCell
End If ' 4 Columns
End If ' Table 2
'
' Test for Table 3
' -------------------------Tables 3 & 4 -
Disbursements ----------------------
If ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count > 2 Then
If ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count = 3 Then
sAutoTextEntry1 = "zExpenseDescription"
sAutoTextEntry2 = "zExpenseAmount"
Else ' Table 4 - Payments
sAutoTextEntry1 = "zPaymentDescription"
sAutoTextEntry2 = "zPaymentAmount"
End If
' Test for entry row (3 columns)
If .Columns.Count = 3 Then
'
'' Add row if in a table
' If Selection.Information(wdWithInTable) = True Then
' Selection.Rows.Add BeforeRow:=Selection.Rows(1)
' End If
.InsertRows 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
End If ' 3 Columns
End If ' Table 3
End With ' Selection
ProtectDocumentMacro
End Sub
Hope these help,
--
Charles Kenyon
See the MVP FAQ: <URL:
http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
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.
Jean-Guy Marcil said:
Hi Tonya,
Would it be possible to change your approach?
If you want a really short macro (thus easier to maintain and modify...) why
not create an autotext representing a row with all form fields already in
the autotext.
Then, all the macro would do is place the cursor under the table an insert
the autotext, then move up to select the first cell...
Is that possible for your context?
--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org