VBA Code
Dim oCC As ContentControl
oCC = ActiveDocument.ContentControls.Add(wdContentControlType.wdContentControlDate)
oCC.Range.Text = VBA.Date()
ID | Readonly. Unique identifier for this content control in this file. This ID is not globally unique across files |
Type | Readonly. The type of content control |
Location | Readonly. The location of the content control in the document. The possible locations are: MainDocument, Header, Footer, Comments, End Notes, FootNotes |
Title | Editable. The title of the control. This is visible when you hover over the control |
Tag | Editable. The tag for containing useful information |
XPath | Editable. This refers to the leaf element or attribute in a custom XML part to create the mapping between the surface document and the data store. You should really drag nodes from the "Custom XML Parts" task pane to automatically create these mappings |
PrefixMappings | Editable. The prefix mapping is the namespace shorthand and the actual namespace used in the XPath. |
Custom XML ID | Editable. The globally unique identifier for the Custom XML Part |
Namespace | Readonly, The namespace |
objRange.ContentControls
objDocument.SelectContentControlByTag
objDocument.SelectContentControlByTitle
objContentControl.BuildingBlockCategory
objContentControl.BuildingBlockType
objContentControl.DateCalendarType
objContentControl.DefaultTextStyle
objContentControl.DropDownListEntries
objContentControl.ID
objContentControl.LockContentControl
objContentControl.LockContents
objContentControl.MultiLine
objContentControl.MultiLine
objContentControl.ParentContentControl
objContentControl.PlaceholderText
objContentControl.Range
objContentControl.Tag
objContentControl.Temporary
objContentControl.Title
objContentControl.Top
objContentControl.Type
objContentControl.XMLMapping
Document.BuildingBlockInsert
Design Mode
If (ActiveDocument.FormsDesign = True) Then
MsgBox("Document is in design mode")
End If
Dim control As CommandBarControl
Set control = Application.CommandBars.FindControl(ID:=1605)
return control.state
The Protect Form button on the Forms toolbar is a shortcut for (Tools > Protection) and select Filling in Forms.
The FormFields collection is 1 based.
Resetting the Form Fields
The form fields will be automatically reset when the following operations are performed:
A datasource is attached to a document.
The document is protected and the "NoReset" parameter is not explicitly set to True.
How to assign a name to a FormField that does not have a name
The following line causes an error if the formfield does not already have a name
Selection.FormFields(1).Name = "myname"
The following code does work
With Dialogs(wdWordDialog.wdDialogFormFieldOptions)
.Name = "Myname"
.Execute
End With
How to find the name of the current formfield
If you need to know which formfield the user tabbed out of in a macro you are using as an on-exit macro you need to take into account that you cant always simply use the command Selection.FormFields(1).Name.
This line will not work with a text formfield although it works with checkboxes and drop-downs.
If you need code to find which formfield was tabbed out of, that works with every type of formfield
If Selection.FormFields.Count = 1 Then
'no text box but a checkbox or drop-down
Call MsgBox(Selection.FormFields(1).Name)
ElseIf Selection.FormFields.Count = 0 And _
Selection.Bookmarks.Count > 0 Then
Call MsgBox(Selection.Bookmarks(Selection.Bookmarks.Count).Name))
End If
How to hide a "Print" commandbutton on a document form when a user clicks on it
When creating a document/template form you may want to include a commandbutton on the document to print the form
But how do you hide the commandbutton so it is not printed
1) Insert a "drawing" textbox and maybe set the borders
2) Insert a commandbutton in the document from the Control toolbox toolbar
3) Right click the commandbutton and change the caption to "Print Document"
4) Cut the commandbutton and paste it into the textbox
5) Make sure you are in Design Mode
6) Double click the commandbutton and add the following code
Private Sub CommandButton_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
Inserting a Form Field
Selection.FormFields.Add Range:=Selection.Range, _
Type:=wdFieldType.wdFieldFormTextInput
Selecting a Form Field
If you want to select a form field using a macro, you can use a variety of different methods
These two lines work but if the form field is empty then it will become highlighted
ActiveDocument.FormFields("Text1").Range.Select
ActiveDocument.FormFields("Text1").Select
This line will select the form field (similar to if you had pressed Tab) even when it is empty.
Selection.GoTo What:=wdGoToBookmark, Name:="Text1"
This line will often cause the screen to jump in a really annoying way depending on the vertical position of the fields.
This line will work and will highlight the entire field.
The screen will only jump if the field is off the screen.
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Select
How to find the name of the current form field
If you need to know which formfield the user tabbed out of in a macro you're using as on-exit macro you need to take into account that you can't always simply use the command Selection.Formfields(1).Name. This line won't work with a textformfield although it works with checkboxes and dropdowns.
If you need code to find out which formfield was tabbed out of, that works with every type of formfield, use:
If Selection.FormFields.Count = 1 Then
'No textbox but a check- or listbox
MsgBox Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
How to set the result of a text formfield using VBA, if the string is longer then 256 characters
You get an error: "String too long" (a ridiculous "design" feature, given that you can do it manually without problems!).
Dim FmFld As FormField, Str1 As String
Str1 = (a long string > 256 characters)
Set FmFld = ActiveDocument.FormFields(1)
FmFld.Result = Str1
Same if you use:
ActiveDocument.Formfields("Text1").Result = Str1
You can get round this by using:
ActiveDocument.Unprotect
FmFld.Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdProtectionType.wdAllowOnlyFormFields, _
NoReset:=True
Or if you're referring to the formfield by name:
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdProtectionType.wdAllowOnlyFormFields, _
NoReset:=True
How to set the result of a text formfield in a Word 2000 table, using VBA, if the string contains carriage returns
The following works in Word 97:
ActiveDocument.FormFields("Text2").Result = "Line1" & vbCr + "Line2"
Unfortunately in Word 2000, if the formfield is in a table, the carriage returns are converted to invalid ascii characters:
There are two workarounds:
One is to use line breaks instead of paragraph marks. The following works in Word 97 and above:
ActiveDocument.FormFields("Text1").Result = "Line1" & Chr$(11) & "Line2"
Or you can use the strangely named vbVerticalTab constant instead of Chr$(11)
ActiveDocument.FormFields("Text1").Result = "Line1" & vbVerticalTab & "Line2"
The other workaround is to use the following:
ActiveDocument.Unprotect
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = "Line1" & vbCr + "Line2"
ActiveDocument.Protect Type:=wdProtectionType.wdAllowOnlyFormFields, _
NoReset:=True
This also works in all versions of Word VBA. As well as allowing you to use real paragraph marks, this method has the advantage that it works with strings longer than 256 characters; but the disadvantage that it you have to unprotect and reprotect.
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext