PDA

View Full Version : [SOLVED:] Listbox Content Control - Reset default?



KilpAr
08-07-2017, 03:03 PM
Let's say we have a list box. We have there a prompt "Pick value" and the options are "1", "2" and "3". User picks value "3". Now in order to reset this value to "Pick value", what do I need?

This

Dim obj
Set obj = ActiveDocument.ContentControls(2)
obj.Range.Text = obj.PlaceholderText.Value

fails to "You are not allowed to edit this selection because it is protected".

gmayor
08-07-2017, 08:20 PM
It would be better to select the control by name or tag, however


Dim obj As ContentControl
Set obj = ActiveDocument.ContentControls(2)
obj.DropdownListEntries(1).Select

KilpAr
08-07-2017, 11:37 PM
It would be better to select the control by name or tag, however


Dim obj As ContentControl
Set obj = ActiveDocument.ContentControls(2)
obj.DropdownListEntries(1).Select

Ok, thanks, but this obviously only returns one of the entries, not the default/prompt.

gmayor
08-08-2017, 01:23 AM
If the content control is inserted from the dialog, the default prompt is the first line.

KilpAr
08-08-2017, 04:37 AM
This works:


With ActiveDocument.ContentControls(2)
.Type = wdContentControlText
.Range.Text = ""
.Type = wdContentControlDropdownList
End With

gmayor
08-08-2017, 04:51 AM
Indeed it does - and the credit goes to Paul (Macropod) at http://www.msofficeforums.com/word-vba/36377-listbox-content-control-reset-default-prompt.html for coming up with this suggestion.

KilpAr
08-08-2017, 05:27 AM
Indeed it does - and the credit goes to Paul (Macropod) for coming up with this suggestion.

Yes, directly from his solution copy-paste.

gmaxey
08-08-2017, 07:03 AM
Yes, directly from his solution copy-paste.

Yes, it is a great mystery why CCDDLs created with VBA are absent the first "Choose and item" default entry. Perhaps it was a smart guy or gal on the VBA side who realized the folly of leaving a "printed" field in a document that displays placeholder text.

If you really want that "Choose and item" look you can create a buildingblock of the a basic CCDDL in your template and use:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 8/8/2017
Dim oRng As Range
Dim oCC As ContentControl
'From a building block of a CCDDL created from the dialog.
Set oRng = ActiveDocument.AttachedTemplate.BuildingBlockEntries("BuiltInCCDDL").Insert(Selection.Range, True)
Set oCC = oRng.ContentControls(1)
With oCC.DropdownListEntries
.Add "A", "A"
.Add "B", "B"
.Add "C", "C"
End With
oCC.DropdownListEntries(1).Select
lbl_Exit:
Exit Sub
End Sub

For such CCs the oCC.DropdownListEntries(1).Select works to reset.

macropod
08-08-2017, 05:31 PM
Cross-posted at: http://www.msofficeforums.com/word-vba/36377-listbox-content-control-reset-default-prompt.html
Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

KilpAr
08-09-2017, 03:05 PM
Yeah, should have included a link. I posted the answer here though as soon as I received an answer.