Question

Im am using Word VBA to create a form field and then insert a value into that field.

Problem is I need to change the data type of the field I have created.

Code:

  ActiveDocument.Bookmarks("Loan_Amount").Select
  ActiveDocument.FormFields.Add FFHere, wdFieldFormTextInput
  ActiveDocument.FormFields("Text1").Result = Value

Had a hunt around but can't find anything on the topic. It would also be nice to change the name of the bookmark of the field (calls itself "Text1" by default).

Was it helpful?

Solution

It would also be nice to change the name of the bookmark of the field (calls itself "Text1" by default).

Like this?

Sub Sample()
    Dim ff As FormField

    ActiveDocument.Bookmarks("Loan_Amount").Select

    Set ff = ActiveDocument.FormFields.Add(FFHere, wdFieldFormTextInput)
    ff.Name = "BlahBlah"
End Sub

And here is an example which inserts a Text field but changes it to Date Field

Sub Sample()
    Dim ff As FormField
    Set ff = ActiveDocument.FormFields.Add(FFHere, wdFieldFormTextInput)
    ff.Select
    With Selection.FormFields(1)
        '~~> Change Name Here
        .Name = "BlahBlah"
        '~~> Change Type here
        With .TextInput
            .EditType Type:=wdCurrentDateText, Default:="", Format:=""
            .Width = 0
        End With
    End With
End Sub

Other type that you can change it to are

  1. wdCurrentDateText
  2. wdCalculationText
  3. wdCurrentTimeText
  4. wdDateText
  5. wdNumberText (This is what your question requested for)

NOTE: I am assuming that FFHere is a valid range

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top