View Single Post
 
Old 01-16-2018, 11:44 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

This will not work unless the area the bookmarks are in are in an unprotected part of the form. Once you protect the form, you can only write to the form fields.

If the bookmarks are in an unprotected part of the form you can run a macro on exit from the check boxes e.g. for Checkbox1

Code:
Sub Check1_onExit()
    If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
        AutoTextToBM "bmAutoText1", ActiveDocument.AttachedTemplate, "Autotext1"
    Else
        FillBM "bmAutoText1", ""
    End If
lbl_Exit:
    Exit Sub
End Sub

Private Sub FillBM(strbmName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        Set oRng = .Bookmarks(strbmName).Range
        oRng.Text = strValue
        oRng.Bookmarks.Add strbmName
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub

Private Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
    On Error GoTo lbl_Exit
    With ActiveDocument
        Set oRng = .Bookmarks(strbmName).Range
        Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
                   (Where:=oRng, RichText:=True)
        .Bookmarks.Add Name:=strbmName, Range:=oRng
    End With
lbl_Exit:
    Exit Sub
End Sub
It would be better if you used check box content controls and did not protect the form. Then you could use the exit event from the content control to insert the autotext.

One way which may work (though it potentially leaves the text open to view) is to insert the texts in the document and create a series of character styles - one for each check box with the hidden font attribute e.g. in the following example - the style name is Hidden1. Apply that style to the text to be hidden for checkbox 1.
Code:
Sub Check1_onExit()
    ActiveWindow.View.ShowHiddenText = False
    If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
        ActiveDocument.Styles("Hidden1").Font.Hidden = False
    Else
        ActiveDocument.Styles("Hidden1").Font.Hidden = True
    End If
lbl_Exit:
    Exit Sub
End Sub
In both cases you actually have to leave the checkbox fields in order to run the macros.

Personally I would not use any of these hit and miss methods. The better way, given that you are going to use macros anyway is to create a userform. Put the options on the userform then build the document based on the results of the userform. The document doesn't then need to be protected, and you can use the FillBM and AutoTextToBM macros to fill the bookmarks as appropriate - either with autotexts or just text.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote