This macro turns the markers you place in Lawlift into real, fillable checkbox and text fields, applies write protection to the rest of the document, and saves the result as a clean .docx file.
Checkboxes and text fields must be marked with a character in Lawlift at the points where they are to be included in the text.
Fields marked with {□} are automatically converted into ‘real’ checkbox fields. Fields marked with {…} are to be automatically converted into “real” text fields. The file becomes read-only except for the checkbox fields and except for the text fields, the fields remain fillable or clickable. In the end, the file converts from a .docm to a .docx file (without macro).
Example:
{□} Option 1
{□} Option 2
Name: {…}
Adress: {…}
Attention: When the macro is executed, it automatically converts the .docm file into a .docx file and saves it in the same folder where the .docm file was saved.
Important: The password for document protection is currently “1234” and can be changed at the beginning of the code.
Note: If you want the macro to run automatically the first time you open it, replace the name “CreateContentControls” with “AutoOpen”.
Important:
When you insert “…” in Word it typically converts it to a single object. So be sure to press the backspace key in order to prevent this automated formatting.
2. Code example
Tip: If you want the macro to run automatically the first time you open the generated Word file, please replace the name “CreateContentControls” in the first line with “AutoOpen”.
Sub CreateContentControls()
Dim CheckboxStrg As String
Dim TextboxStrg As String
Dim myRange As Range
Dim Testrange As Range
Dim StartPos As Long
Dim EndPos As Long
Dim Password As String
Dim Deletepath As String
Password = "1234"
CheckboxStrg = "{" & ChrW(&H25A1) & "}"
TextboxStrg = "{...}"
Offset = 0
StartPos = InStr(1, ActiveDocument.Range.Text, CheckboxStrg, vbTextCompare) - 1
Do Until StartPos = -1
Set myRange = ActiveDocument.Range
Set Testrange = ActiveDocument.Range
Testrange.SetRange Start:=1, End:=StartPos
Offset = Testrange.ContentControls.Count * 2
EndPos = StartPos + Len(CheckboxStrg)
myRange.SetRange Start:=StartPos + Offset, End:=EndPos + Offset
myRange.Select
myRange.Text = ""
myRange.ContentControls.Add (wdContentControlCheckBox)
StartPos = InStr(StartPos, ActiveDocument.Range.Text, CheckboxStrg, vbTextCompare) - 1
Loop
StartPos = InStr(1, ActiveDocument.Range.Text, TextboxStrg, vbTextCompare) - 1
Do Until StartPos = -1
Set myRange = ActiveDocument.Range
Set Testrange = ActiveDocument.Range
Testrange.SetRange Start:=1, End:=StartPos
Offset = Testrange.ContentControls.Count * 2
EndPos = StartPos + Len(TextboxStrg)
myRange.SetRange Start:=StartPos + Offset, End:=EndPos + Offset
myRange.Select
myRange.Text = ""
myRange.ContentControls.Add (wdContentControlText)
StartPos = InStr(StartPos, ActiveDocument.Range.Text, TextboxStrg, vbTextCompare) - 1
Loop
Application.ActiveDocument.Protect wdAllowOnlyFormFields, Password:=Password
Deletepath = ActiveDocument.FullName
ActiveDocument.SaveAs2 FileName:=Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) - 5) & ".docx", FileFormat:=wdFormatDocumentDefault
Documents.Open (Left(ActiveDocument.FullName, Len(ActiveDocument.FullName) - 5) & ".docx") Kill (Deletepath)
End Sub
You can download a sample file here. Attention, when you run the macro, the .docm in the same folder automatically converts to a .docx file.
Note: There are restrictions in the functioning of the macro if the document contains function fields (e.g. tables of contents) or clickable URLs (“::url”). In addition, difficulties may arise if the above-mentioned expressions ”{□}” or ”{…}” are used in tables. An alternative to this is shown in the fields, tables & URLs macro.
Run the macro: see How to insert and run a Word macro.