D
Dylan
I'm having a problem with this macro. It stops working after the second
formfield is inserted in the new row. It doesn't like the .Name I'm trying to
enter in Case 3.
Can someone please help me solve this? I haven't attempted the .Name fields
in the other Cases yet - so any help you can give towards my understanding of
the problem would be great.
Option Explicit
Dim response As Integer
Dim myRow As Long
Dim myCount As Long
Dim colCount As Long
Dim colIndex As Long
Dim myNewField As String
Dim myCelltxt As Range
Dim newrow As Row
Dim oldrow As Row
Dim rownum As Integer
Dim i As Integer
Dim bProtected As Boolean
Sub Addrow()
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
'On Error Resume Next
response = MsgBox("Add new row?", vbQuestion + vbYesNo)
If response = vbYes Then
ActiveDocument.Tables(5).Rows.Add
rownum = ActiveDocument.Tables(5).Rows.Count
colCount = ActiveDocument.Tables(5).Columns.Count
Else: GoTo closedown
End If
For i = 2 To colCount
ActiveDocument.FormFields.Add _
Range:=ActiveDocument.Tables(5).Cell(rownum, i).Range, _
Type:=wdFieldFormTextInput
Select Case i
Case 2
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = "InsCalendar"
.ExitMacro = ""
.Enabled = True
.TextInput.EditType Type:=wdDateText, Default:="",
Format:="dd/MM/yy"
.Name = "txt" & "Row" & rownum & "Cell" & i
End With
Case 3
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = "InsCalendar"
.ExitMacro = ""
.Enabled = True
.TextInput.EditType Type:=wdDateText, Default:="",
Format:="dd/MM/yy"
.Name = "txt" & "Row" & rownum & "Cell" & i
End With
Case 4
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.TextInput.EditType wdRegularText, "", "", True
.Name = ""
End With
Case 5
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = ""
.ExitMacro = "Addrow"
.Enabled = True
.TextInput.EditType Type:=wdNumberText, Default:="",
Format:= _
"£#,##0.00;(£#,##0.00)"
.CalculateOnExit = True
.Name = ""
End With
End Select
Next i
'Set oldRow =
With Selection.Tables(1)
Set myCelltxt = .Rows.Last.Previous.Cells(1).Range
myCelltxt.End = myCelltxt.End - 1
End With
'Set number of new row
With Selection.Tables(1)
Set newrow = .Rows.Last
newrow.Cells(1).Range.Text = Val(myCelltxt.Text) + 1
End With
closedown:
'Select hidden boomark
ActiveDocument.Tables(5).Range.FormFields("txtHidden").Select
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub
formfield is inserted in the new row. It doesn't like the .Name I'm trying to
enter in Case 3.
Can someone please help me solve this? I haven't attempted the .Name fields
in the other Cases yet - so any help you can give towards my understanding of
the problem would be great.
Option Explicit
Dim response As Integer
Dim myRow As Long
Dim myCount As Long
Dim colCount As Long
Dim colIndex As Long
Dim myNewField As String
Dim myCelltxt As Range
Dim newrow As Row
Dim oldrow As Row
Dim rownum As Integer
Dim i As Integer
Dim bProtected As Boolean
Sub Addrow()
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
'On Error Resume Next
response = MsgBox("Add new row?", vbQuestion + vbYesNo)
If response = vbYes Then
ActiveDocument.Tables(5).Rows.Add
rownum = ActiveDocument.Tables(5).Rows.Count
colCount = ActiveDocument.Tables(5).Columns.Count
Else: GoTo closedown
End If
For i = 2 To colCount
ActiveDocument.FormFields.Add _
Range:=ActiveDocument.Tables(5).Cell(rownum, i).Range, _
Type:=wdFieldFormTextInput
Select Case i
Case 2
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = "InsCalendar"
.ExitMacro = ""
.Enabled = True
.TextInput.EditType Type:=wdDateText, Default:="",
Format:="dd/MM/yy"
.Name = "txt" & "Row" & rownum & "Cell" & i
End With
Case 3
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = "InsCalendar"
.ExitMacro = ""
.Enabled = True
.TextInput.EditType Type:=wdDateText, Default:="",
Format:="dd/MM/yy"
.Name = "txt" & "Row" & rownum & "Cell" & i
End With
Case 4
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = ""
.ExitMacro = ""
.Enabled = True
.TextInput.EditType wdRegularText, "", "", True
.Name = ""
End With
Case 5
With ActiveDocument.Tables(5).Cell(rownum, i).Range.FormFields(1)
.EntryMacro = ""
.ExitMacro = "Addrow"
.Enabled = True
.TextInput.EditType Type:=wdNumberText, Default:="",
Format:= _
"£#,##0.00;(£#,##0.00)"
.CalculateOnExit = True
.Name = ""
End With
End Select
Next i
'Set oldRow =
With Selection.Tables(1)
Set myCelltxt = .Rows.Last.Previous.Cells(1).Range
myCelltxt.End = myCelltxt.End - 1
End With
'Set number of new row
With Selection.Tables(1)
Set newrow = .Rows.Last
newrow.Cells(1).Range.Text = Val(myCelltxt.Text) + 1
End With
closedown:
'Select hidden boomark
ActiveDocument.Tables(5).Range.FormFields("txtHidden").Select
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub