Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-20-2020, 11:26 AM
jayevan4 jayevan4 is offline Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2010
Novice
Auto populate building block autotext on dependent drop down exit
 
Join Date: May 2020
Posts: 4
jayevan4 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 05-20-2020, 12:29 PM
gmaxey gmaxey is online now Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 05-21-2020, 04:58 AM
jayevan4 jayevan4 is offline Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2010
Novice
Auto populate building block autotext on dependent drop down exit
 
Join Date: May 2020
Posts: 4
jayevan4 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 05-21-2020, 06:51 AM
gmaxey gmaxey is online now Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 05-21-2020, 07:43 AM
jayevan4 jayevan4 is offline Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2010
Novice
Auto populate building block autotext on dependent drop down exit
 
Join Date: May 2020
Posts: 4
jayevan4 is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 05-21-2020, 08:11 AM
gmaxey gmaxey is online now Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
Attached Files
File Type: dotm CC DDL conditional BuildingBlock to CC or BM.dotm (31.8 KB, 22 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 05-21-2020, 09:14 AM
jayevan4 jayevan4 is offline Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2010
Novice
Auto populate building block autotext on dependent drop down exit
 
Join Date: May 2020
Posts: 4
jayevan4 is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 05-21-2020, 10:25 AM
gmaxey gmaxey is online now Auto populate building block autotext on dependent drop down exit Windows 10 Auto populate building block autotext on dependent drop down exit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
Attached Files
File Type: dotm CC DDL convert to RichText with BuildingBlock.dotm (30.7 KB, 28 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto populate building block autotext on dependent drop down exit auto populate multiple text boxes dependent on a drop down angelaschultz Word VBA 7 03-18-2017 03:00 PM
Auto populate building block autotext on dependent drop down exit Auto Populate Text On Dependent Drop Down Exit GregStewartPTC Word VBA 2 02-27-2017 05:37 AM
Auto populate building block autotext on dependent drop down exit 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

Other Forums: Access Forums

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


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