#1
|
|||
|
|||
How do i insert autotext using a contol toolbox checkbox
I am wanting to use a checkbox from the control toolbox toolbar that when it is checked it will insert an "autotext entry" in a certain location in the document (say for instance a bookmark or similiar)
If someone can help with writing a macro i would really appreciate it. Thanks |
#2
|
||||
|
||||
The attached document demonstrates the technique. It uses a checkbox titled 'MyCheckBox', a bookmark named 'Destination', and a ContentControlOnExit macro to trigger the updating.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul,
Are you able to convert the file into word 2003 format please? The file didnt convert properly from 2007 to 2003. Cheers, |
#4
|
||||
|
||||
Hi NP85,
I hadn't noticed that you're using Office 2003. Instead, I infeered from your reference to 'a checkbox from the control toolbox toolbar' that you meant a content control checkbox, which is only applicable to Office 2010 & later. The attached document demonstrates the technique using a checkbox formfield, which Office 2003 can use. The checkbox is linked to an 'on exit' macro that updates a custom document property named 'ChkState' and a DOCPROPERTY field in the document to reflect the changed content. Do be aware that using formfields requires the use of Word's forms protection, which restricts editing to the filling in of formfields in the document and/or typing in unprotected Sections of the document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
RE
Sorry to be a pain; I am using a checkbox from the "Control toolbox" toolbar as opposed to the formsfield toolbar on the basis as you mentioned above you need to protect the form when using the formsfield.
Is it possible to do what im trying to do with the "control toolbox" toolbar? I appreciate your help. Cheers, |
#6
|
||||
|
||||
OK, here it is with an ActiveX checkbox, using a bookmark named 'Destination', and a macro triggered by clicking on it to do the updating.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thanks heaps thats so awesome!!!
If instead of inserting "Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus" as shown below i want to run a macro i have previously recorded (this macro automatically inserts specific tables and words and i want them to be inserted at the bookmark "destination"), is there a way of calling up the macro within that loop? (the macro names is 'AutoSubdivision')?? Code:
Option Explicit Private Sub CheckBox13534191_Click() If CheckBox13534191.Value = True Then Call UpdateBookmark("Destination", "Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus." & vbCr) Else Call UpdateBookmark("Destination", "") End If End Sub Sub UpdateBookmark(BmkNm As String, NewTxt As String) Dim BmkRng As Range With ActiveDocument If .Bookmarks.Exists(BmkNm) Then Set BmkRng = .Bookmarks(BmkNm).Range BmkRng.Text = NewTxt .Bookmarks.Add BmkNm, BmkRng End If End With Set BmkRng = Nothing End Sub Last edited by macropod; 11-15-2012 at 10:45 PM. Reason: Added code tags & formatting |
#8
|
||||
|
||||
Where is your content coming from? And what is the existing code for it?
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
The content is coming from the normal.dot file and the macro i have recorded (also in the normal.dot file) is:
Code:
Sub AutoSubdivision() Selection.TypeText Text:="Subdivision" Selection.Range.InsertAutoText End Sub |
#10
|
|||
|
|||
I tried using the code tags but didnt appear to do anything. Apologies for my ignorance.
|
#11
|
||||
|
||||
Try:
Code:
Private Sub CheckBox1_Click() If CheckBox1.Value = True Then Call UpdateBookmark("Destination", "Subdivision") Else Call UpdateBookmark("Destination", "") End If End Sub Sub UpdateBookmark(BmkNm As String, NewTxt As String) Dim BmkRng As Range With ActiveDocument If .Bookmarks.Exists(BmkNm) Then Set BmkRng = .Bookmarks(BmkNm).Range BmkRng.Text = NewTxt .Bookmarks.Add BmkNm, BmkRng If BmkRng.Text = "Subdivision" Then BmkRng.InsertAutoText End If End With Set BmkRng = Nothing End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
This thread may be long dead, but hopefully not!
The code modification to allow an autotext entry to be inserted via the ActiveX Checkbox is great. My question is how to make the autotext disappear if the checkbox is then unchecked after having already been checked. The previous example codes (with plain text instead of autotext) do this, but when modified for autotext it stops this functionality. Any ideas? Thanks! |
#13
|
|||
|
|||
Here is an adaptation of Paul's code that should work with a named autotext entry stored in the document's attached template:
Code:
Private Sub CheckBox1_Click() If CheckBox1.Value = True Then Call UpdateBookmark("Destination", "ATEntry" & vbCr) Else Call UpdateBookmark("Destination") End If End Sub Sub UpdateBookmark(BmkNm As String, Optional strATName As String) Dim BmkRng As Range With ActiveDocument If .Bookmarks.Exists(BmkNm) Then Set BmkRng = .Bookmarks(BmkNm).Range If strATName <> vbNullString Then Set BmkRng = ActiveDocument.AttachedTemplate.AutoTextEntries(strATName).Insert(Where:=BmkRng) Else BmkRng.Text = vbNullString End If .Bookmarks.Add BmkNm, BmkRng End If End With Set BmkRng = Nothing End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Autotext | jafrapatty | Word | 2 | 05-09-2014 04:30 AM |
Word 2003 Document Opens In control toolbox Edit Mode | rangasamy007 | Word | 1 | 09-16-2011 04:12 AM |
AutoText question | harbor11 | Word | 0 | 12-16-2009 09:42 AM |
Control Toolbox | tjc154 | Word | 0 | 03-29-2009 07:13 PM |
Excel ->VB code for Checkbox (control toolbox) | kirkstyle | Excel | 0 | 08-16-2006 04:17 PM |