Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 27 of 27

Thread: Placing Building Blocks at Bookmarks

  1. #21
    Define what you mean by 'system blocks'. The code you posted does alternative things depending on the selection in ComboBoxDocType. What is it that is not happening that you expect to happen?

    Select Case ComboBoxDocType.ListIndex
        Case Is = 0    'First item in list
            If ListBoxCoC.Text = "General Comments" Then
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
            End If
            If ListBoxCoC.Text = "Specific Comments" Then
                AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
            End If
        Case Is = 1    'Second item in list
            AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "Table"
        Case Else    'Nothing selected
    End Select
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  2. #22
    Quote Originally Posted by gmayor View Post
    Define what you mean by 'system blocks'. The code you posted does alternative things depending on the selection in ComboBoxDocType. What is it that is not happening that you expect to happen?

    Select Case ComboBoxDocType.ListIndex
        Case Is = 0    'First item in list
            If ListBoxCoC.Text = "General Comments" Then
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
            End If
            If ListBoxCoC.Text = "Specific Comments" Then
                AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
            End If
        Case Is = 1    'Second item in list
            AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "Table"
        Case Else    'Nothing selected
    End Select
    What I mean is that the Building Blocks do not get inserted when I choose the Case=0 document (i.e. Compilation of Comments) and that both options are selected. This should (my intention) have the GeneralComments building block inserted at bookmark EVTBookMark01 and the SpecifComments building block at bokmark EVTBookMark02. But it doesn't.

  3. #23
    Unfortunately what you want is is not what the code posted actually commands.

    If the first item (Case 0) is selected then the code selects your choice from ListBoxCoCListBoxCoC. i.e. General Comments at bookmark1 OR Specific Comments at Bookmark 2. The ListBOXCOC.Text is whatever choice you select from that list box.

    If you want to insert BOTH items, you don't need the listbox to select from between them.

    You might want the list box to set values in the other lists, but as you have not shared your document nor indicated what they are for, it is not possible to guess how they should be used. You might want to look at Greg's web page on cascading list boxes, where the selection from one determines what is shown in another.

    Sub InsertExistingBuildingBlock()
        FillBM "EVTBookMark01", ""
        FillBM "EVTBookMark02", ""
        Select Case ComboBoxDocType.ListIndex
            Case Is = 0    'First item in list
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
                AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
            Case Is = 1    'Second item in list
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "Table"
                'Bookmark "EVTBookMark02" has no content with this option, unless you indicate here what it should be.
            Case Else    'Nothing selected
        End Select
    End Sub
    You will notice that the above code also calls FillBM to clear the bookmarks' contents before addressing them with the new data. The FillBM code below, is required should you wish to change you mind and change the inserted data. I assume that the Table autotext inserts a table. Tables inserted into ranges don't behave in quite the same was as texts and so it is necessary to delete the table(s) from the range before re-applying data to it - hence the modified version of my FillBM code from that you will find on my web site.

    Public Sub FillBM(strbmName As String, strValue As String)
    'Graham Mayor - http://www.gmayor.com
    Dim orng As Range
    Dim i As Long
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set orng = .Bookmarks(strbmName).Range
            If orng.Tables.Count > 0 Then
                For i = 1 To orng.Tables.Count
                    orng.Tables(i).Delete
                Next i
            End If
            orng.Text = ""
            orng.Text = strValue
            orng.Bookmarks.Add strbmName
        End With
    lbl_Exit:
        Set orng = Nothing
        Exit Sub
    End Sub
    If you want to insert different texts based on what is selected in the second list box then you need to indicate what happens for each selection e.g.

    Case Is = 0    'First item in list
            If ListBoxCoC.Text = "General Comments" Then
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
                AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
            Else 
                'Specific Comments has been selected so do something else
            End If
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #24
    Quote Originally Posted by gmayor View Post
    Unfortunately what you want is is not what the code posted actually commands.

    If the first item (Case 0) is selected then the code selects your choice from ListBoxCoCListBoxCoC. i.e. General Comments at bookmark1 OR Specific Comments at Bookmark 2. The ListBOXCOC.Text is whatever choice you select from that list box.

    If you want to insert BOTH items, you don't need the listbox to select from between them.

    You might want the list box to set values in the other lists, but as you have not shared your document nor indicated what they are for, it is not possible to guess how they should be used. You might want to look at Greg's web page on cascading list boxes, where the selection from one determines what is shown in another.

    Sub InsertExistingBuildingBlock()
        FillBM "EVTBookMark01", ""
        FillBM "EVTBookMark02", ""
        Select Case ComboBoxDocType.ListIndex
            Case Is = 0    'First item in list
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
                AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
            Case Is = 1    'Second item in list
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "Table"
                'Bookmark "EVTBookMark02" has no content with this option, unless you indicate here what it should be.
            Case Else    'Nothing selected
        End Select
    End Sub
    You will notice that the above code also calls FillBM to clear the bookmarks' contents before addressing them with the new data. The FillBM code below, is required should you wish to change you mind and change the inserted data. I assume that the Table autotext inserts a table. Tables inserted into ranges don't behave in quite the same was as texts and so it is necessary to delete the table(s) from the range before re-applying data to it - hence the modified version of my FillBM code from that you will find on my web site.

    Public Sub FillBM(strbmName As String, strValue As String)
    'Graham Mayor - http://www.gmayor.com
    Dim orng As Range
    Dim i As Long
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set orng = .Bookmarks(strbmName).Range
            If orng.Tables.Count > 0 Then
                For i = 1 To orng.Tables.Count
                    orng.Tables(i).Delete
                Next i
            End If
            orng.Text = ""
            orng.Text = strValue
            orng.Bookmarks.Add strbmName
        End With
    lbl_Exit:
        Set orng = Nothing
        Exit Sub
    End Sub
    If you want to insert different texts based on what is selected in the second list box then you need to indicate what happens for each selection e.g.

    Case Is = 0    'First item in list
            If ListBoxCoC.Text = "General Comments" Then
                AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
                AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
            Else 
                'Specific Comments has been selected so do something else
            End If
    What if either one of the items in ListBoxCoC does not get selected? How would the programming then affect what comes in the final document?

  5. #25
    It depends on what you want to achieve. If you are talking about the last example you can code it like
    Case Is = 0    'First item in list
                If ListBoxCoC.Text = "General Comments" Then
                    AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
                    AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
                ElseIf ListBoxCoC.Text = "Specific Comments" Then
                    'Do something associated with the Specific Comments option
                Else
                    'Neither option selected so do something else
                End If
    or you can use a second set of case statements e.g

    Sub InsertExistingBuildingBlock()
        FillBM "EVTBookMark01", ""
        FillBM "EVTBookMark02", ""
        Select Case ComboBoxDocType.ListIndex
            Case Is = 0    'First item in ComboBoxDocType list
                Select Case ListBoxCoC.Text
                    Case Is = "General Comments"
                        'do stuff associated with General Comments
                    Case Is = "Specific Comments"
                        'do stuff associated with Specific Comemnts
                    Case Else
                        'neither selected so do something else or leave this empty and do nothing at all
                End Select
            Case Is = 1    'Second item in ComboBoxDocType list
                Select Case ListBoxCoC.Text
                    Case Is = "General Comments"
                        'do stuff associated with General Comments
                    Case Is = "Specific Comments"
                        'do stuff associated with Specific Comemnts
                    Case Else
                        'neither selected so do something else or leave this empty and do nothing at all
                End Select
            Case Else    'Nothing selected
        End Select
    End Sub
    Unless you tell us what you expect to happen, I can't tell you how to program it, but the above should point the way.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #26
    Quote Originally Posted by gmayor View Post
    It depends on what you want to achieve. If you are talking about the last example you can code it like
    Case Is = 0    'First item in list
                If ListBoxCoC.Text = "General Comments" Then
                    AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
                    AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
                ElseIf ListBoxCoC.Text = "Specific Comments" Then
                    'Do something associated with the Specific Comments option
                Else
                    'Neither option selected so do something else
                End If
    or you can use a second set of case statements e.g

    Sub InsertExistingBuildingBlock()
        FillBM "EVTBookMark01", ""
        FillBM "EVTBookMark02", ""
        Select Case ComboBoxDocType.ListIndex
            Case Is = 0    'First item in ComboBoxDocType list
                Select Case ListBoxCoC.Text
                    Case Is = "General Comments"
                        'do stuff associated with General Comments
                    Case Is = "Specific Comments"
                        'do stuff associated with Specific Comemnts
                    Case Else
                        'neither selected so do something else or leave this empty and do nothing at all
                End Select
            Case Is = 1    'Second item in ComboBoxDocType list
                Select Case ListBoxCoC.Text
                    Case Is = "General Comments"
                        'do stuff associated with General Comments
                    Case Is = "Specific Comments"
                        'do stuff associated with Specific Comemnts
                    Case Else
                        'neither selected so do something else or leave this empty and do nothing at all
                End Select
            Case Else    'Nothing selected
        End Select
    End Sub
    Unless you tell us what you expect to happen, I can't tell you how to program it, but the above should point the way.
    Just imagine you have to create a new document from a template. You double-click on the EVT.dotm document and a new document opens, covered by this interface. It first gives you the choice (ComboBoxDocType) between creating a Compilation of Comments, a Lessons Obervation, a Military Advice or a Initiating Military Directive. After having xhosen your document type, a further choice appears in that interface where you need to choose the chapters that will appear in your document.
    Case 1: Imagine you want to create aCompilation of Comments and you click on it in the initial scroll-down menu (ComboBoxDocType) Then comes up a further field with choices (ListBoxCoC). For the Compilation of Comments, you have the choice between General Comments and Specific Comments. You can potentially choose either the General Comment (in which case only the GeneralComments building block ought to be inserted in EVTBookMark01), or the Specific Comments (in which case only the SpecificComments building block is inserted), or both GeneraComments and SpecificComments building blocks are to come in the document to be creates at bookmarks EVTBookMark01 and EVTBookMark02.
    Case 2: I want to create a Military Advice. I click on it in the ComboBoxDocType and a further fiels poops up (ListBoxMA). This listbox offers me 4 options: References, SITUATION AND AIM, CONSIDERATIONS, RECOMMENDATION(S). Here again, the author can pick and choose any one, two, three or all four of the choices. The chosen headings, corresponding to building blocks References, SITUATION, CONSIDERATIONS and RECOMMENDATION, respectively, will have to be inserted in the document at the relevant bookmarks, i.e. EVTBookMark01, EVTBookMark02, EVTBookMark03 and EVTBookLMark04 (they all exist in the template EVT.dotm already).
    Does this make things any clearer. I'm afraid I cannot attach the template here so that you can have a better idea.

  7. #27
    Most of the principles involved have already been covered. Basically you start at the top of the tree and work down

    Select Case ComboBoxDocType.ListIndex
    which provides four options (the code posted previously only covered two of them, but you can add the rest easily enough, once you understand the principles involved).
    Select Case ComboBoxDocType.ListIndex
        Case Is = 0    'First item in ComboBoxDocType list
        Case Is = 1    'Second item in ComboBoxDocType list
        Case Is = 2    'Third item in ComboBoxDocType list
        Case Is = 3    'Fourth item in ComboBoxDocType list
        'etc as required
        Case Else    'Nothing selected
    End Select
    In each of those case statements you perform the tasks associated with the document type. However the code assumed that you were only making a single selection from the list box. If you are making multiple selections you need to loop through the list and see which selections are made and perform tasks accordingly.

    Frankly I don't see why you need several list boxes to perform the same task. You can use one and populate it according to the document selection.

    I have attached an example. Obviously I don't know what all your selections are supposed to do, but I have filled in what you have told us. I have also removed the superfluous list boxes from the userform and have moved the code out of the userform except for the list box changes.

    There should be enough here to get you going. If not, you can contact me via my web site and we'll discuss taking it forward.
    Attached Files Attached Files
    Last edited by gmayor; 12-20-2017 at 02:11 AM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •