C# Snippets


FormFeldsReplaceFieldsWithParameters

Public Shared Function FormFeldsReplaceFieldsWithParameters() As String(,)
'this is to get around a known MSFT bug KB 286841

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim objFormField As Word.FormField
Dim arFormFieldsArray As String(,)
Dim iTextFormCount As System.Int32

' Redim array to hold contents of text field.
ReDim Preserve arFormFieldsArray(2, gApplicationWord.ActiveDocument.FormFields.Count - 1)

For Each objFormField In gApplicationWord.ActiveDocument.FormFields
Select Case objFormField.Type
Case Word.WdFieldType.wdFieldFormTextInput
arFormFieldsArray(0, iTextFormCount) = "Text"
arFormFieldsArray(1, iTextFormCount) = objFormField.Result
arFormFieldsArray(2, iTextFormCount) = objFormField.Name

' Select the form field and replace it with placeholder text.
objFormField.Select()
gApplicationWord.Selection.TypeText("")

iTextFormCount = iTextFormCount + 1

Case Word.WdFieldType.wdFieldFormCheckBox
arFormFieldsArray(0, iTextFormCount) = "Checkbox"
arFormFieldsArray(1, iTextFormCount) = CType(objFormField.CheckBox.Value, String)
arFormFieldsArray(2, iTextFormCount) = objFormField.Name

' Select the form field and replace it with placeholder text.
'objFormField.Select()
'gApplicationWord.Selection.TypeText("")

iTextFormCount = iTextFormCount + 1

Case Else
End Select
Next objFormField

' Redim array to hold the exact number of text form fields
ReDim Preserve arFormFieldsArray(2, iTextFormCount - 1)

FormFeldsReplaceFieldsWithParameters = arFormFieldsArray

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("FormFeldsReplaceFieldsWithParameters", msCLASSNAME, _
"replace all the form fields with equivalent parameters.", _
mobjCOMException, mobjException)
End If
End Try
End Function

FormFeldsReplaceParametersWithFields

Public Shared Sub FormFeldsReplaceParametersWithFields(ByVal arFormFieldsArray(,) As String)

Try
If clsError.ErrorFlag() = True Then Exit Sub

Dim objFormField As Word.FormField
Dim ifieldcount As System.Int32

gApplicationWord.Selection.HomeKey(Unit:=Word.WdUnits.wdStory) 'Go to top of document.

gApplicationWord.Selection.Find.ClearFormatting()
With gApplicationWord.Selection.Find
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue

For ifieldcount = 0 To arFormFieldsArray.GetUpperBound(1)
Do While .Execute(FindText:="") = True

' Replace the placeholder with the form field.
objFormField = gApplicationWord.Selection.FormFields.Add(Range:=gApplicationWord.Selection.Range, _
Type:=Word.WdFieldType.wdFieldFormTextInput)

' Restore form field contents and bookmark name.
objFormField.Result = arFormFieldsArray(1, ifieldcount)
objFormField.Name = arFormFieldsArray(2, ifieldcount)
Loop
gApplicationWord.Selection.HomeKey(Unit:=Word.WdUnits.wdStory) 'Go to top of document for next find.

'Do While .Execute(FindText:="") = True

' ' Replace the placeholder with the form field.
' objFormField = gApplicationWord.Selection.FormFields.Add(Range:=gApplicationWord.Selection.Range, _
' Type:=Word.WdFieldType.wdFieldFormCheckBox)

' ' Restore form field contents and bookmark name.
' objFormField.CheckBox.Value = CType(arFormFieldsArray(1, ifieldcount), Boolean)
' objFormField.Name = arFormFieldsArray(2, ifieldcount)
'Loop
'gApplicationWord.Selection.HomeKey(Unit:=Word.WdUnits.wdStory) 'Go to top of document for next find.

Next ifieldcount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("FormFeldsReplaceParametersWithFields", msCLASSNAME, _
"replace all the parameters with their original form fields.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top