This adapted macro creates checkboxes and text fields in documents that also contain function fields, tables or clickable URLs.
If your document is to contain other function fields in addition to footnotes, such as tables of contents or clickable links (“::url”), or if the expressions ”{□}” or ”{…}” are to be placed in tables, it is necessary to use an appropriately adapted macro. In the following we provide you with a sample code:
Sub CreateContentControls()
Dim CheckboxStrg As String
Dim TextboxStrg As String
Dim myRange As Range
Dim Testrange As Range
Dim myPara As Paragraph
Dim StartPos As Long
Dim EndPos As Long
Dim Password As String
Dim Deletepath As String
Password = "1234"
CheckboxStrg = "{" & ChrW(&H25A1) & "}"
TextboxStrg = "{...}"
Offset = 0
For Each myPara In ActiveDocument.Paragraphs
StartPos = InStr(1, myPara.Range.Text, CheckboxStrg, vbTextCompare) - 1
Do Until StartPos = -1
Set myRange = myPara.Range
Set Testrange = myPara.Range
Testrange.SetRange Start:=myRange.Start, End:=myRange.Start + StartPos + 1
Offset = Testrange.ContentControls.Count * 2
EndPos = StartPos + Len(CheckboxStrg)
myRange.SetRange Start:=myRange.Start + StartPos + Offset, End:=myRange.Start + EndPos + Offset
myRange.Select
myRange.Text = ""
myRange.ContentControls.Add (wdContentControlCheckBox)
StartPos = InStr(StartPos + 1, myPara.Range.Text, CheckboxStrg, vbTextCompare) - 1
Loop
Next
For Each myPara In ActiveDocument.Paragraphs
StartPos = InStr(1, myPara.Range.Text, TextboxStrg, vbTextCompare) - 1
Do Until StartPos = -1
Set myRange = myPara.Range
Set Testrange = myPara.Range
Testrange.SetRange Start:=myRange.Start, End:=myRange.Start + StartPos + 1
Offset = Testrange.ContentControls.Count * 2
EndPos = StartPos + Len(TextboxStrg)
myRange.SetRange Start:=myRange.Start + StartPos + Offset, End:=myRange.Start + EndPos + Offset
myRange.Select
myRange.Text = ""
myRange.ContentControls.Add (wdContentControlText)
StartPos = InStr(StartPos + 1, myPara.Range.Text, TextboxStrg, vbTextCompare) - 1
Loop
Next
Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:=Password
Deletepath = ActiveDocument.FullName
ActiveDocument.SaveAs2 FileName:=Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 5) & ".docx", FileFormat:=wdFormatDocumentDefault
Documents.Open (Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 5) & ".docx")
Kill (Deletepath)
End Sub
ATTENTION: Since the macro searches each paragraph individually for the expressions, it is important that there are only expressions OR function fields or hyperlinks (“::url”) in a paragraph. If this is not the case, display problems may occur. In addition, care must be taken that the marking of the check boxes and text fields with the expressions ”{□}” or ”{…}” always occurs within a paragraph and not across several paragraphs.
Run the macro: see How to insert and run a Word macro.