Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2018, 02:23 PM
prezdead prezdead is offline Checkboxes to Inupt AutoText Windows 10 Checkboxes to Inupt AutoText Office 2016
Novice
Checkboxes to Inupt AutoText
 
Join Date: Jan 2018
Posts: 2
prezdead is on a distinguished road
Default 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.
Reply With Quote
  #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,103
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 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
  #3  
Old 01-17-2018, 08:56 AM
prezdead prezdead is offline Checkboxes to Inupt AutoText Windows 10 Checkboxes to Inupt AutoText Office 2016
Novice
Checkboxes to Inupt AutoText
 
Join Date: Jan 2018
Posts: 2
prezdead is on a distinguished road
Default

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
With the bookmark and autotext - do I just changed the bolded parts?
Reply With Quote
  #4  
Old 01-18-2018, 02:36 AM
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,103
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 of
Default

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
Reply With Quote
Reply



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:53 AM.


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