• Create a User Form with drop-down checkboxes (Word 2003)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Create a User Form with drop-down checkboxes (Word 2003)

    Author
    Topic
    #448483

    Hi everyone. I need some help. I work for a law firm and the lawyers (about 75 of them) need to have their biographies done in a format that they can e-mail out to clients for marketing purposes. The biography consists of their picture and a blurb about where they went to school and their past cases that would be of interest. They need the ability to not only create their own, but choose other lawyers in that field to send as well. I created a new template with a table to hold the text and the picture in the proper position as well as the law firm information on the right hand side of the document. I created new file directories – one to save the picture as the lawyer’s initials (“AAA.jpg) and one with the blurb that holds their biography information (“AAA.doc”). What I want to do is create a macro so when they click on the button, a user form pops up and basically allows them to choose which lawyer’s biographies they want (drop-down list) or the option to create all of them. I can’t figure out what to use to have the ability to check many instances in a drop-down list from the control box. I know I have a drop-down list, but I can only choose one option, not check many. I am equating it to Word’s Customize Toolbars box where it gives you the options on the left and you can check as many toolbars as you want. Is there some way to duplicate that functionality in VBA?

    Thanks so much.

    Viewing 0 reply threads
    Author
    Replies
    • #1096803

      You can only select one item at a time from a combo box (aka dropdown list). For multi-select capability, you need to use a list box. A list box has a MultiSelect property that can have one of the following valuies:

      0 – fmMultiSelectSingle (default, the user can select one item)
      1 – fmMultiSelectMulti (the user can click on an item to add it to the selection, and click it again to remove it from the selection)
      2 – fmMultiSelectExtend (the user can select items the way you select files in Windows Explorer: click and Shift+Click to select a range, Ctrl+click to select or deselect individual items).

      If MultiSelect is set to 1 or 2, you can loop through the selected items as follows, for example in the On Click event procedure of a command button:

      Dim i As Integer
      For i = 0 To Me.ListBox1.ListCount - 1
      If Me.ListBox1.Selected(i) Then
      ' Your code here
      ...
      End If
      Next i

      • #1096820

        Thank you very much. What a relief!

        You guys are absolutely awesome!!1

        • #1097457

          I think that I am over my head with this. I am at work now and am trying to create the code for this macro. I have never used arrays before, and am having a tough time understanding. So here goes.

          Thank you for the User Form information. I now have that set up and it works great. In the Initialization of the UserForm, I have it referencing a folder and populating the List box with that information. That way, there should be no maintenance of the macro once it is completed (just add and delete the files from the folder, rather than the macro). That sounded really good to me until I actually tried the code. Anyway, here is the code I am using to populate the List Box:

          Private Sub UserForm_Initialize()
          ‘Create a new document based on the Lawyer Bio template
          Documents.Add Template:=STRMRTEMPLATES & “Lawyer Bio.dot”, newtemplate:=False
          strDocName = ActiveDocument.Name
          ‘Add all the contents of the BiosBio folder to listbox
          ffile = Dir(“MR_FileDataMacrodataBiosBio*.doc”)
          Count = 1
          Do While ffile “”
          If ffile “.” And ffile “..” Then
          ReDim Preserve FileArray(Count)
          FileArray(Count) = ffile
          Count = Count + 1
          ffile = Dir()
          End If
          Loop
          FileList.List() = FileArray
          End Sub

          The files consist of .doc. I want to insert this file into the template and then insert the corresponding picture (.jpg) as well. The problem is that i don’t know how to retrieve that information from the array. I get that it works off numbers, but the numbers will vary as I have it set so that the user can select as many lawyers as they want. The code that you gave me here returns a number, but I want it to return the value (the lawyer’s initials) so when I have the macro insert the file, it is looking for AAA.doc, for example. Then I want to be able to return only the value up to the “.” so that I can insert the picture called AAA.jpg from a different folder.

          Finally, I have a check box on the user form so the user can simply click that box in order to Create All the lawyer’s biographies. How do I get it to select all the entries displayed in the List Box?

          Is this even possible?

          Help!!!

          • #1097465

            Let’s say someone selects items 2 and 4 in the listbox. You can use the data in the listbox itself (e.g., name or initials) or you can match up the index number of the listbox with your array, because they are in the same order.

            (I haven’t done this for a while so I’m not going to add possibly incorrect code here.)

            • #1097467

              Okay, but how do I match up the data in the listbox with the index number? I have a programming book that just kind of drops off after the populating of the listbox, and doesn’t tell me how to retrieve the information from the listbox once its been selected. I’ve never done this before. I’ve been working on this all night and have gotten nowhere. When I try to just return the value, it is one number off of the one that I selected. I don’t get it.

            • #1097471

              Here’s a little sample. It checks whether a list item is selected and, if it is, it adds it to a string so it can display it to you in a message box. You could replace this with the code you want to run for that list item.

            • #1097555

              Thank you. That has gotten me a little bit further. I was using an Option Base 1 code at the top of the code window. Maybe that was why it was off by one number. However, now in the listbox I am getting a blank box. How do I get rid of that?

              Here is the code I have so far.
              Private Sub cmdOK_Click()
              Me.Hide
              StatusBar = “Please wait while Lawyer Bio is being assembled . . .”
              Application.ScreenUpdating = False
              Documents(strDocName).Activate

              For i = 0 To Me.FileList.ListCount – 1
              If Me.FileList.Selected(i) = True Then
              strReport = strReport & Me.FileList.List(i)
              ActiveDocument.Bookmarks(“Bio”).Select
              Selection.InsertFile FileName:=STRBIOS & strReport
              End If
              Next

              End Sub

              This works if I am only selecting one person from the list. However, if I select two or more, it returns the whole list. For example, it is trying to open H:MacrodataBiosBioJPR.docCPR.doc. And of course, the macro caves because there is no such file name. It should just be JPR.doc and then insert the file, goto the picture bookmark, insert the corresponding picture (JPR.jpg), move to the end of the template, insert a next page section break, insert the Table autotext (to get the formatted table), goto to the bookmark and start all over again for the next person selected. (Boy, I don’t ask for much, do I).

              Do I have to redim the array after the selections are made to only incude the selections and then break the array into separate string variables and only read the first part of the string (getting rid of the .doc) so that I can change it to .JPG? Can I do this? If so, how?

              Please help!!!

            • #1097556

              Instead of concatenating the selected items into one huge string, you should process each item as you encounter it, i.e. insert the document etc., then continue to the next item.

              But I don’t understand what you’re trying to do – there can only be one bookmark with a specific name in a document, and I don’t think repeatedly going to the same bookmark will do what you want.

            • #1097563

              Never mind. I got this part to work.

              I have a new question now. I want the macro to only go to the end of the document, insert a next page section break, and insert the autotext table when there is more than one person selected in the listbox. If there is only one, don’t do this. I tried:
              If Len(Me.FileList.Selected(i)) > 1 Then
              With Selection
              .EndKey unit:=wdStory
              .InsertBreak Type:=wdSectionBreakNextPage
              .TypeText Text:=”table”
              .Range.InsertAutoText
              End With
              End If
              But it gave me an error saying that I can’t use the Len code with this variable.

              Is there some way of returning the value of how many were selected so that I can do this?

              Thanks so much.

            • #1097564

              Two other options:

              (1) Set a variable (e.g., blnUsedFirstTable) so you can track whether you need to add a new section before inserting information; or

              (2) Remove the last section after you complete the inserts.

            • #1097567

              Duh! Thanks so much. I must be getting tired.

              I don’t know why I am having so much trouble with this macro. My brain seems to have shifted into neutral.

              You guys are great.

              Thanks again.

    Viewing 0 reply threads
    Reply To: Create a User Form with drop-down checkboxes (Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: