#1
|
|||
|
|||
Auto populate building block autotext on dependent drop down exit
Hello,
I have a drop down menu created with 7 different options that I would like to insert different paragraphs of text saved as a building block dependent on which dropdown is selected. e.g. If dropdown "A" is selected, it will insert building block of text "1", dropdown selection "B" will insert text "2" etc. Thank you |
#2
|
|||
|
|||
The most casual search of this forum will indicate this question has been asked and answered before. In fact, a stating point would be the question asked by Mightymegs and answered by macropod just 7 posts below yours!
I'm not sure if Paul (macropod) addresses the building block in that thread (you should check) but when you know what was selected in the dropdown, you can insert some "named" building block in some "named" range using something like this: Code:
Sub InsertBBatRange(BBName As String) Dim oTmp As Template Dim oRng As Range Set oTmp = NormalTemplate Set oRng = ActiveDocument.SelectContentControlsByTitle("Conditional Text").Item(1).Range oTmp.BuildingBlockEntries(BBName).Insert oRng, True End Sub |
#3
|
|||
|
|||
I did view Mightymegs post preceding mine, as well as ran some additional searches, but they weren't specific to using building blocks. In fact, the first thing I used was a tutorial I found on what looks to be your website "https://gregmaxey.com/word_tip_pages/content_control_magic_dropdowns.html" which was a great starting point. However, calling myself inexperienced with VBA would be an understatement - this is the first time I am using it.
I used the code in the tutorial section titled, "Display Defined Value" which I got working; however, I realized the pre-defined text was too large and getting cut off, so I continued on to the "Display a Pre-defined Building Block" section and copied the code there, and tried to modify the existing code to be tailored to my document. It worked for the first two, but when I added additional lines as I need 7 total drop down options, I was getting errors. Thank you for replying with the code you provided, but as a total beginner here, can you provide any additional detail on what parts of that code I need to change to fit my document? I can see the part that says (BBName) which is where I am assuming I change to the title of the building blocks I have saved, but otherwise I'm lost. Thanks again |
#4
|
|||
|
|||
Jay,
Okay. The problem here is that you have shown no code to change or review. As you have studied previous examples, you should at least have code in the ThisDocument module Document_ContentContorlOnExit event which triggers on CC Exit and evaluates which of the seven selections you made. Here, I assume you have a dropdown list titled "DDL" which contains four entires "A,B,C and D" When one of those are selected we call a variation of the procedure I posted earlier to populate both a named range at a document rich text content control titled "Conditional Content" and a named range at a document bookmarked titled "bmConditionalContent" Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Title Case "DDL" Select Case ContentControl.Range.Text Case "A": InsertBB_atRTCC_Range "Conditional Content", "Text1": InsertBB_atBookmarkRange_Range "bmConditionalContent", "Text1" Case "B": InsertBB_atRTCC_Range "Conditional Content", "Text2": InsertBB_atBookmarkRange_Range "bmConditionalContent", "Text2" Case "C" Case "D" '...so on. Case Else: InsertBB_atRTCC_Range "Conditional Content": InsertBB_atBookmarkRange_Range "bmConditionalContent" End Select End Select lbl_Exit: Exit Sub End Sub Sub InsertBB_atRTCC_Range(CCTitle As String, Optional BBName As String = vbNullString) Dim oTmp As Template Dim oRng As Range Set oTmp = NormalTemplate Set oRng = ActiveDocument.SelectContentControlsByTitle(CCTitle).Item(1).Range If Not BBName = vbNullString Then oTmp.BuildingBlockEntries(BBName).Insert oRng, True Else oRng.Text = vbNullString End If lbl_Exit: Exit Sub End Sub Sub InsertBB_atBookmarkRange_Range(BMName As String, Optional BBName As String) Dim oTmp As Template Dim oRng As Range Set oTmp = NormalTemplate Set oRng = ActiveDocument.Bookmarks(BMName).Range If Not BBName = vbNullString Then Set oRng = oTmp.BuildingBlockEntries(BBName).Insert(oRng, True) Else oRng.Text = vbNullString End If ActiveDocument.Bookmarks.Add BMName, oRng lbl_Exit: Exit Sub End Sub |
#5
|
|||
|
|||
Thanks for your reply, I appreciate the clarification. The title of my dropdown is "Trigger Statements", with a dropdown entry titled "SAS". When SAS is selected from the dropdown I want it to populate a building block from the autotext gallery in the general category which was saved and named as "SASText". Is the below code the correct way to implement that?
Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Title Case "Trigger Statements" Select Case ContentControl.Range.Text Case "SAS": InsertBB_atRTCC_Range "Conditional Content", "SASText": InsertBB_atBookmarkRange_Range "bmConditionalContent", "SASText" Case Else: InsertBB_atRTCC_Range "Conditional Content": InsertBB_atBookmarkRange_Range "bmConditionalContent" End Select End Select lbl_Exit: Exit Sub End Sub Sub InsertBB_atRTCC_Range(CCTitle As String, Optional BBName As String = vbNullString) Dim oTmp As Template Dim oRng As Range Set oTmp = NormalTemplate Set oRng = ActiveDocument.SelectContentControlsByTitle(CCTitle).Item(1).Range If Not BBName = vbNullString Then oTmp.BuildingBlockEntries(BBName).Insert oRng, True Else oRng.Text = vbNullString End If lbl_Exit: Exit Sub End Sub Sub InsertBB_atBookmarkRange_Range(BMName As String, Optional BBName As String) Dim oTmp As Template Dim oRng As Range Set oTmp = NormalTemplate Set oRng = ActiveDocument.Bookmarks(BMName).Range If Not BBName = vbNullString Then Set oRng = oTmp.BuildingBlockEntries(BBName).Insert(oRng, True) Else oRng.Text = vbNullString End If ActiveDocument.Bookmarks.Add BMName, oRng lbl_Exit: Exit Sub End Sub |
#6
|
|||
|
|||
Jay,
Provided the buildingblock is defined in the NormalTemplate and you are populating to both a rich text CC titled "Conditional Content" and a bookmark titled "bmConditionalContent" then yes, that is how you would code it. The buildingblock has to go somewhere. That somewhere is a range which you must define. In the example I've provide that range is a) a content control and b) a bookmark. You might want to populate to a table cell or whatever. You have to determine and define that range. If the buildingblock isn't in the NormalTemplate then you have to define the template. If the buildingblocks themselves are a table or contain tables you have to tweak the code a bit. I'm attaching a file with a working example where the BBs are defined in the template file itself. |
#7
|
|||
|
|||
Is there a way I can do this without populating the text to another CC called "Conditional Content"? as I am not trying to populate data to a different destination such as a table cell. Once a user has selected the dropdown option, I want it to replace the option with the text.
e.g. in the "Display a Pre-defined Building Block" section of your tutorial (Content Control ADCAP (Advanced Capability) Dropdowns) there is a drop down where if you select "Gettysburg Address" it replaces Gettysburg Address in the drop down with the entire paragraph building block of text in its place. My document is a written report and depending on the nature of the report, there are different standardized opening statements that need to be stated. For example, in the "Trigger Statements" drop down there is an entry titled "SAS", if the user selects "SAS", I'm looking for SAS to be replaced by the building block of text in the same way in which your Gettysburg example works |
#8
|
|||
|
|||
Jay,
I don't know how much clearer the code in that tutorial document can be. You have to convert the CC from a DDL to a RT or PT control. Something like this. See attached file. Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Title Case "DDL" ContentControl.Type = 0 Select Case ContentControl.Range.Text Case "SAS": InsertBB_atRTCC_Range "DDL", "SAS" Case "A": InsertBB_atRTCC_Range "DDL", "Text1" Case "B": InsertBB_atRTCC_Range "DDL", "Text2" Case "C": InsertBB_atRTCC_Range "DDL", "Text3" Case "D": InsertBB_atRTCC_Range "DDL", "Text4" Case Else: InsertBB_atRTCC_Range "DDL" End Select ContentControl.Type = 4 End Select lbl_Exit: Exit Sub End Sub Sub InsertBB_atRTCC_Range(CCTitle As String, Optional BBName As String = vbNullString) Dim oTmp As Template Dim oRng As Range Set oTmp = ThisDocument.AttachedTemplate Set oRng = ActiveDocument.SelectContentControlsByTitle(CCTitle).Item(1).Range If Not BBName = vbNullString Then oTmp.BuildingBlockEntries(BBName).Insert oRng, True Else oRng.Text = vbNullString End If lbl_Exit: Exit Sub End Sub |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
auto populate multiple text boxes dependent on a drop down | angelaschultz | Word VBA | 7 | 03-18-2017 03:00 PM |
Auto Populate Text On Dependent Drop Down Exit | GregStewartPTC | Word VBA | 2 | 02-27-2017 05:37 AM |
How to populate dependent dropdowns and auto-fill text fields simultaneously? | vera | Word VBA | 1 | 10-07-2016 07:41 PM |
VBA Word - Building Block (AutoText) - Array - Replace Text with Specific Building Blocks | jc491 | Word VBA | 7 | 01-03-2016 10:34 PM |
Building blocks show correctly in dropdown, but wrong building block populates in doc | wordgirl123 | Word | 0 | 10-03-2013 08:30 AM |