Home > Software engineering >  Need a VBA code for autotext from dropdown menu
Need a VBA code for autotext from dropdown menu

Time:01-18

I'm writing a code to select a specific autotext from a dropdown list. I've not written code since the late 1970s the old Basic A...I've forgotten most of it.

The dropdown list is called HVAC the text to insert is in an autotext file called "Split Systems" or whichever text associated with the dropdown items
The error message is "5941 the requested member of the collection does not exist"

I've tried using the value of 1,2,3,or 4 in the if statement line instead of the "Split...", it doesn't work either

Where is issue or how should I code it.

MY code follows:

    Sub one()

    If ActiveDocument.Formfields(hvac).DropDown.Value = "Split Systems" Then GoTo 10 Else GoTo 20

    10
    ActiveDocument.Content.Select  
    Selection.Collapse Direction:=wdCollapseEnd
    ActiveDocument.AttachedTemplate.AutoTextEntries("Split Systems").Insert _
    Where:=Selection.Range, RichText:=True
    GoTo 100
    20

    If ActiveDocument.Formfields(hvac).DropDown.Value = "Packaged Systems" Then GoTo 25 Else GoTo 30
 
    25
    ActiveDocument.Content.Select
    Selection.Collapse Direction:=wdCollapseEnd
    ActiveDocument.AttachedTemplate.AutoTextEntries("Packaged System").Insert _
    Where:=Selection.Range, RichText:=True
    GOto 100
    30

    If ActiveDocument.Formfields(hvac).DropDown.Value = Central Heating System" Then GoTo 35 Else GoTo 40

    35
    ActiveDocument.Content.Select
    Selection.Collapse Direction:=wdCollapseEnd
    ActiveDocument.AttachedTemplate.AutoTextEntries("Central Heating System").Insert _
    Where:=Selection.Range, RichText:=True
    GoTo 100
    40
    If ActiveDocument.Formfields(hvac).DropDown.Value = "PTACs" Then GoTo 45
    45
    ActiveDocument.Content.Select
    Selection.Collapse Direction:=wdCollapseEnd
    ActiveDocument.AttachedTemplate.AutoTextEntries("Central Heating System").Insert _
    Where:=Selection.Range, RichText:=True

    100

    End Sub

Tried everything I know

CodePudding user response:

Word now has Building Block Gallery Content Controls that eliminate the need to add code to dropdowns. Choose the Developer tab, then choose Building Block Gallery Content Control in the Controls group. Use the Properties button in the Controls group to point the control at AutoText. The only significant drawback is that this control doesn't currently work in Word for Mac.

CodePudding user response:

Why are you trying to reinvent the wheel?

Look at AutoTextList and Building Blocks Gallery Content Control

These two features built into Word create drop downs of AutoText or other Building Blocks. What are you trying to do that cannot be accomplished using one of them?

Here is my writing on the building blocks gallery control screen shot

Here is a link to vba to insert AutoText/Building Blocks. (It varies depending on the location of the AutoText.)

Note: all links are to my writing.

  • Related