#1
|
|||
|
|||
Multiple entries in dropdown lists with building blocks
Hi
In his thread (https://www.msofficeforums.com/word-...own-lists.html) Paul gave a solution to have a control content filled with text after a listbox choice. This works great for small texts. For larger texts (multiple paragraphs) this doesn't work unfortunately. Linking the choice of the listbox to building blocks should work, but I have no idea how to achieve this. I have the building blocks, I have a building block content control, but I have no link to the list box. Any help is more than welcome. Chris |
#2
|
||||
|
||||
You could use a simple function to fill a rich text content control with the formatted text from the autotext entry, according to the value selected in the list box (see attached template) e.g. as follows - Create a new document from the template
Code:
Sub AutoTextToCC(strCCName As String, oTemplate As Template, strAutotext As String) 'Graham Mayor - https://www.gmayor.com - Last updated - 10 Apr 2019 'strCCName is the ContentControl title 'oTemplate is the template with the autotext entry 'strAutotext is the name of the autotext entry Dim oCC As ContentControl On Error GoTo lbl_Exit For Each oCC In ActiveDocument.ContentControls If oCC.TITLE = strCCName Then oCC.LockContentControl = True oTemplate.AutoTextEntries(strAutotext).Insert Where:=oCC.Range, RichText:=True Exit For End If Next oCC lbl_Exit: Exit Sub End Sub You may find Insert Content Control Add-In useful for entering and/or modifying the content controls.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Hi Graham
Thanks a lot. This is what I was looking for. One small itch: if there is a text with multiple paragraphs (with bullet) the outline at the front is not correct. The first two paragraphs are the control content, the 3rd en 4rd are the autotext (as the outline should be). Any solution? Thanks Chris |
#4
|
||||
|
||||
That shouldn't happen if the autotext and the body text of the target document use the same styles. Try inserting the Roundhouse autotext entry after the content control in the example and the inserted text should align with the content control text.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Hi Graham
forget the previous post. I deleted the autotext I had and made a new one. Problem solved. But I have another question: if I need this somewhere else in my document with a different list box, won't there be a conflict in the VBA if I make a second Private Sub Document_ContentControlOnExit or do I have to integrate my second listbox in the existing VBA? regards Chris |
#6
|
||||
|
||||
You need to combine the code. You can use Select Case to process as many controls as you wish by addressing them by their title property e.g.
Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim i As Long Dim oCC As ContentControl With ContentControl Select Case .Title Case Is = "Client" If .ShowingPlaceholderText = True Then Set oCC = ActiveDocument.SelectContentControlsByTitle("ClientDetails").Item(1) oCC.LockContentControl = True oCC.Range.Text = "" Else For i = 1 To .DropdownListEntries.Count If .DropdownListEntries(i).Text = .Range.Text Then Select Case .DropdownListEntries(i).Text Case "Roundhouse Nurseries" AutoTextToCC "ClientDetails", ActiveDocument.AttachedTemplate, "Roundhouse" Case "Smiths Wholesales" AutoTextToCC "ClientDetails", ActiveDocument.AttachedTemplate, "Smith" End Select Exit For End If Next End If Case Is = "Another listbox" If .ShowingPlaceholderText = True Then Set oCC = ActiveDocument.SelectContentControlsByTitle("CC to be processed").Item(1) oCC.LockContentControl = True oCC.Range.Text = "" Else For i = 1 To .DropdownListEntries.Count If .DropdownListEntries(i).Text = .Range.Text Then Select Case .DropdownListEntries(i).Text Case "List Item 1" AutoTextToCC "CC to be processed", ActiveDocument.AttachedTemplate, "Autotext Name 1" Case "List Item 2" AutoTextToCC "CC to be processed", ActiveDocument.AttachedTemplate, "Autotext Name 2" End Select Exit For End If Next End If End Select End With End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Thanks Graham, I will give that a try.
|
#8
|
|||
|
|||
Hi Graham
I must be doing something wrong because I can't get it to work. I've attached your first dotm file, but changed the VBA according to your last post. The first list box works fine, the second doesn't. Could you please check what is wrong with it? My colleagues like the idea very much and even would like to go one step further: 'List box 1' has two options: 'list item 1' and 'list item 2'. When you choose 'list item 1' there is another listbox 'list box 2' with two options: 'box2_item1' and 'box2_item2'. When you choose 'box_item1' a rich text content control is filled with autotext1, when you choose 'box_item2' that rich text content control is filled with autotext2 (is like what we had after your last post). Is something like this even possible? Chris |
#9
|
||||
|
||||
It was no use posting my template which worked if you wanted me to check yours.
The code I posted works, if you match the content control names, the values in the list and the autotext names - see attached
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#10
|
|||
|
|||
Sorry, my mistake . I did attach the wrong file. Everything is working great now, so thanks. Could you give me a hint on how to get the cascade of my previous post? BTW, this should be a 3rd listbox situation in my document.
|
#11
|
||||
|
||||
You can add more case statements to work with the as many list boxes as you want, however if you want list boxes that are only present and dependent on an earlier selection then it gets altogether more complicated, especially when we only have a vague outline of what it is that you are trying to achieve.
Personally if that is the aim I would go instead form a userform with cascading list boxes and write only the final values to the document based on what is selected - see Populate Userform Listbox or Combobox
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#12
|
|||
|
|||
Hi Graham, this is what I would like to achieve:
I have a list box 'Products' with two products: ProductA en ProductB. Each product can be used under two conditions: ConditionA and Condition B. Therefore I have a second list box 'Conditions'. For each condition there is an explanation that should come in a rich text CC, so I have therefore ProdA_Cond1 autotext, ProdB_Cond2 autotext, ... E.g. I choose ProductB in list box 'Products'. A new list box 'Conditions' becomes available. In list box 'Conditions' I choose Condition 1 and then in a rich text CC ProdB_Cond1 should appear. And this is all in the same document as the one with which you helped me already a great deal |
#13
|
|||
|
|||
While I'm still struggling with my previous question , I encountered a problem with something that works fine (thanks to Graham) on two locations in my document, but fails in a third location.
I've attached a test file, where 'Kies een item' en 'Niet in de zomer' are doing what they are supposed to do: give text in a rich text CC, and where 'Enkel in de winter' refuses to give the required text. I've tried several other control names, ... but no luck. (in the code of the doc6.dotm file are also the two other locations that work just fine) Anyone? Thx |
#14
|
||||
|
||||
With regard to your template, your code is
Code:
Case Is = "Zomer/winter" If .ShowingPlaceholderText = True Then Set oCC = ActiveDocument.SelectContentControlsByTitle("WinterTekst").Item(1) oCC.LockContentControl = True oCC.Range.Text = "" Else For i = 1 To .DropdownListEntries.Count If .DropdownListEntries(i).Text = .Range.Text Then Select Case .DropdownListEntries(i).Text Case "Kies een item." AutoTextToCC "WinterTekst", ActiveDocument.AttachedTemplate, "TipKeuze" Case "Enkel in de winter" AutoTextToCC "WinterTekst", ActiveDocument.AttachedTemplate, "PrimerAnhydriet" Case "Niet in de zomer" AutoTextToCC "WinterTekst", ActiveDocument.AttachedTemplate, "TipZomer" End Select Exit For End If Next End If There is no "PrimerAnhydriet" building block, so that one is never going to work, and Case "Kies een item." is superfluous as that is the placeholder text and always produces nothing because of the other half of the IF statement.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#15
|
|||
|
|||
Stupid me . Of course it works when you do it right.
Still struggling with the multiple dependent list boxes. Probably it is simple, but I don't see it. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Multiple entries in dropdown lists | paul_pearson | Word VBA | 151 | 10-18-2023 04:23 PM |
Muliple entries in Multiple dropdown lists | Haygordon | Word VBA | 7 | 04-04-2019 06:28 AM |
VBA Word - Building Block (AutoText) - Array - Replace Text with Specific Building Blocks | jc491 | Word VBA | 7 | 01-03-2016 10:34 PM |
Open Quickpart file normalemail.dotm - edit multiple building blocks | rollno | Word | 7 | 02-24-2014 04:29 PM |
Building blocks show correctly in dropdown, but wrong building block populates in doc | wordgirl123 | Word | 0 | 10-03-2013 08:30 AM |