PDA

View Full Version : VBA in UserForm for Word



Paskie_be
03-01-2018, 07:40 AM
Dear all,

Here I am back, seeking advice. I am attaching the .docm template featuring the coding of my UserForm: it works perfectly but there's still room for improvement. One of the aspects I would like to see improved is the length of the coding. As you can see, there are Cases inside cases where, at times, users can chose any number of items out of a list of seven. You can imagine the number of combinations possible and, hence, the number of coding lines. What I would like to achieve it to have what choices that have been made inserted at the successive bookmarks, bearing in mind that each choice is linked to a buildingblock that will have to be inserted and not the text of the choice as such.
Let me explain using the simplest example, with only two options:
In ComboBoxDocType, the end-user will choose a type of document, which will trigger the popping-up of certain fields. In Case Is = 0, there is a choice between "General Comments" and/or "Specific Comments".
If the user chooses only "General Comments", the corresponding BuildingBlock ("GeneralComments") will go at bookmark "EVTBookMark01".
If the user chooses only "Specific Comments", the corresponding BuildingBlock ("SpecificComments") will go at bookmark "EVTBookMark01".
If the user chooses only "General Comments" and "Specific Comments", the corresponding BuildingBlocks ("GeneralComments" and "SpecificComments") will go at bookmarks "EVTBookMark01" and "EVTBookMark02", respectively.
This is the present code:
Select Case ComboBoxDocType.ListIndex
Case Is = 0 'First item in list
Select Case ListBoxCoC.ListIndex
Case Is = "General Comments"
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
Case Is = "Specific Comments"
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "SpecificComments"
Case Else
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
End Select

How could I avoid encoding every single possible combination of choices and have whichever options selected entered at consecutive bookmarks.

I must add that the bookmarks are already in the template document and are called EVTBookMark01 to EVTBookMark11.

Thank you for your input.

Paskie_be21722