#1
|
|||
|
|||
Checkboxes to Inupt AutoText
Word 2016. Will be a protected template on a network folder (legacy forms being used).
I have multiple checkboxes that, when checked, I would like the stored AutoText to display at the various bookmarks. If the box is not checked, I don't want anything displayed (and I also don't want any blank spaces in the document between paragraphs). ie: Checkbox1 Checkbox2 Checkbox3 AutoText1 {Bookmark1} AutoText2 {Bookmark2} AutoText 3 {Bookmark3}. I am admittedly new to macros/codes in word but can recognize the variables once they are in front of me. I've been across multiple examples online already but can't figure out how to make it work. Would anyone be able to provide be with the basic code for - check this box and AutoText comes to this bookmark location? I will be able to go from there. My question though: will AutoText work on a file saved as a template on a network folder or should I be considering a different? TIA. |
#2
|
||||
|
||||
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 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 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 |
#3
|
|||
|
|||
Thanks gmayor. Just to confirm with the first set of code:
Code:
'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 |
#4
|
||||
|
||||
Not really, you call the functions from your macro e.g.
AutoTextToBM "bookmark name", the template the autotext is in, "the autotext name" or FillBM "bookmark name", "value to insert"
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Checkboxes in Excel | asmanokhchi | Excel | 0 | 04-28-2015 11:13 PM |
checkboxes | smohap | Word | 1 | 06-19-2011 09:24 PM |
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 |