Word: input fields, create and use (content controls)
In Microsoft Word, you can to build easily your own input fields in a document or form, in which users enter data.
These fields are content controls and be done in vba Word macros as ContentControls
This ContentControls can be queried at run time according to the entered text.
Thus, you can use the content controls as document variables.
' < Init >
Dim sContentControl_Name As String
sContentControl_Name = "ctlFeld1"
' < / init >
|
You can find the content controls under menu > developer tools > AA [plain text content control]
If you inserted an entry field, then this is highlighted with a small label, marking
Pure text entry fields are added under AA plain text content controls
If you marked the Eingebefeld and click on properties, then represented the possible settings for this input field
In the field: title specifying the text, which appear above the input field, if you have marked the content control
In the field: day you give a unique name to the box, which you can find in the vba macro code
For some operations, it makes sense, the option field: the content control cannot be deleted to enable.
As a result, you can not accidentally delete this field. But the field can not change then also with 'Cut & paste'
Use the vba macro
The use of the content controls becomes important when you checking this via macro.
This macro asks for example the value of an input field off and displays the value in a message.
You will find the content control under the ContentControls of a document.
While using the vba macro, you must first check whether the field is present at all:
'< check Control exists >
Dim bControl_Exists As Boolean
bControl_Exists = False
Dim control As ContentControl
For Each control In doc.ContentControls
If control.Tag = sContentControl_Name Then
bControl_Exists = True
Exit For
End If
Next
If bControl_Exists = False Then
MsgBox "Das Eingabefeld existiert nicht", vbCritical, "Eingabefeld fehlt"
Exit Sub
End If
'</ check Control exists >
|
Übernommer, a field can be with
Dim control As ContentControl
Set control = doc.ContentControls("Eingabefeld01")
|
Then, it reads the text from the box.
Queried with the input text in a content control
Here of the complete vba macro code as a sample
Sub Demo_Get_Inputfield()
'-----------------< Demo_Get_Inputfield() >-----------------
Dim doc As Document
Set doc = Application.ActiveDocument
'----< Jump to Bookmark >----
'< Init >
Dim sContentControl_Name As String
sContentControl_Name = "ctlFeld1"
'</ Init >
'< check Control exists >
Dim bControl_Exists As Boolean
bControl_Exists = False
Dim control As ContentControl
For Each control In doc.ContentControls
If control.Tag = sContentControl_Name Then
bControl_Exists = True
Exit For
End If
Next
If bControl_Exists = False Then
MsgBox "Das Eingabefeld existiert nicht", vbCritical, "Eingabefeld fehlt"
Exit Sub
End If
'</ check Control exists >
MsgBox "Eingabe ist=" & control.range.Text
'-----------------</ Demo_Get_Inputfield() >-----------------
End Sub
|
Video Tutorial
|