Word VBA: Can I order bookmarks on Ms. Word

R

rachitm

I have an insert button on my page, which inserts a row in a table with

6 columns (and 6 formfields) everytime I press it. What I want to know
is
that everytime I insert a row, is there a way I could make sure that
the formfields have ordered bookmarks?

In the 6th column formfield I need to do a calculation which depends on

the 4th column formfield. Is there a way I could do that too (with the
insert row method) ?


Thanks
 
G

Greg Maxey

Provided that the selection is in that added row following the
insertion, you may be able to adapt something like this:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim oFrmFlds As FormFields
Dim pRowNum As Long
Dim pIndex As Long
Set oRng = Selection.Rows(1).Range
Set oFrmFlds = oRng.FormFields
pRowNum = oRng.Information(wdEndOfRangeRowNumber)
pIndex = 0
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For pIndex = 1 To oFrmFlds.Count 'Should be 6 in your case
oFrmFlds(pIndex).Select
Select Case oFrmFlds(pIndex).Type
Case wdFieldFormTextInput
With Dialogs(wdDialogFormFieldOptions)
.Name = "TextRow" & pRowNum & "_" & pIndex
.Execute
End With
If pIndex = 6 Then
oFrmFlds(pIndex).TextInput.EditType wdCalculationText,
"=TextRow" & pRowNum & "_4 + 10" 'value of the newly bookmarked current
row cell 4 + 10
End If
Case wdFieldFormCheckBox
'Do Nothing
Case wdFieldFormDropDown
'Do Nothing
Case Else
'Do Nothing
End Select
Next pIndex
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
 
R

rachitm

Thank you for your help!!


Greg said:
Provided that the selection is in that added row following the
insertion, you may be able to adapt something like this:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim oFrmFlds As FormFields
Dim pRowNum As Long
Dim pIndex As Long
Set oRng = Selection.Rows(1).Range
Set oFrmFlds = oRng.FormFields
pRowNum = oRng.Information(wdEndOfRangeRowNumber)
pIndex = 0
On Error Resume Next
ActiveDocument.Unprotect
On Error GoTo 0
For pIndex = 1 To oFrmFlds.Count 'Should be 6 in your case
oFrmFlds(pIndex).Select
Select Case oFrmFlds(pIndex).Type
Case wdFieldFormTextInput
With Dialogs(wdDialogFormFieldOptions)
.Name = "TextRow" & pRowNum & "_" & pIndex
.Execute
End With
If pIndex = 6 Then
oFrmFlds(pIndex).TextInput.EditType wdCalculationText,
"=TextRow" & pRowNum & "_4 + 10" 'value of the newly bookmarked current
row cell 4 + 10
End If
Case wdFieldFormCheckBox
'Do Nothing
Case wdFieldFormDropDown
'Do Nothing
Case Else
'Do Nothing
End Select
Next pIndex
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
 

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