Jamie H
Jamie H

Reputation: 1

How to refer to the in-built Default value for a text FormField in Excel VBA

I am trying to write some code in Excel which loops through a number of documents and extracts details from FormFields. The documents are all produced from the same template and users can only enter details into the set fields (everything else in the document is locked).

Some of the form fields are optional and so are left blank. But the code extracts a value of " " and inputs that into the cells, which is then causing issues later in the code. I want it to store the value as "".

[I am using Excel 2010]

I have tried the following:

If ActiveDocument.FormFields(1).Range.Text = vbNull Then

If ActiveDocument.FormFields(1).Range.Text = "" Then

If ActiveDocument.FormFields(1).Range.Text = "     " Then

and

If ActiveDocument.FormFields(1).TextInput.Default = True Then

And none of them seem to work.

Anyone know how to refer the defaul Default value in a FormField?

Upvotes: 0

Views: 76

Answers (1)

taller
taller

Reputation: 18943

  • If the default value was NOT altered during the insertion of a form field, you can identify its default value using the Result property.

enter image description here

Option Explicit
Sub demo()
    Dim FF As FormField
    For Each FF In ActiveDocument.FormFields
        If Len(FF.Result) = 0 Then
            Debug.Print "Default value is in " & FF.Name
        Else
            Debug.Print "Users input is in " & FF.Name
        End If
    Next
End Sub

Microsoft documentation:

FormField.Result property (Word)

Upvotes: 0

Related Questions