Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 01-16-2018, 11:44 PM
gmayor's Avatar
gmayor gmayor is offline Checkboxes to Inupt AutoText Windows 10 Checkboxes to Inupt AutoText 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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkboxes in Excel asmanokhchi Excel 0 04-28-2015 11:13 PM
Checkboxes to Inupt AutoText checkboxes smohap Word 1 06-19-2011 09:24 PM
Checkboxes to Inupt AutoText Excel Checkboxes theresamille699 Excel 3 04-12-2011 08:08 PM
Ticking Checkboxes screid Word 1 06-08-2010 02:04 AM
Manipulating checkboxes in XML Ivo Word 0 12-06-2005 09:04 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:50 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft