PDA

View Full Version : Placing Building Blocks at Bookmarks



Paskie_be
12-15-2017, 07:55 AM
Hello,

I have just created this UserForm called EVT. The purpose of it is to ask users to choose a type of document and then select the document parts that they want to insert in the document.
So I have created a number of Building Blocks (BB) that would fit in at a number of bookmarks. In a nutshell and schematically, BB1/Doc1 through to BB1/Doc6 goes to Bookmark1, BB2/Doc1 to BB2/Doc6 goes to Bookmark2, bearing in mind that each time there's only one BB for each level and for each give Doc. I am not sure I am making myself clear.

also, one type of document requires a landscape paper orientation.

The result I have achieved is the conditional appearance of the selected frame (one frame per document type). I cannot seem to get the condition paper orientation nor the BB insertion.

Thanks for your help.

Paskie.

This is the code I have come up with so far:


Private Sub CommandButtonCreate_Click()
With ActiveDocument.PageSetup
If ComboBoxDocType.Value = "Compilation of Comments" Then
.Orientation = wdOrientLandscape
.PaperSize = wdPaperA4
.TopMargin = CentimetersToPoints(2.54)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.BottomMargin = CentimetersToPoints(2.54)
Else
.Orientation = wdOrientPortrait
.PaperSize = wdPaperA4
.TopMargin = CentimetersToPoints(2.54)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.BottomMargin = CentimetersToPoints(2.54)
End If
End With

Me.Repaint
EVT.Hide
End Sub


Private Sub userform_initialize()
ComboBoxDocType.List = Array("Compilation of Comments", "Lessons Observation", "EUMC Meeting Documents", "Military Advice", "Initiating Military Directive")
ListBoxCoC.List = Array("General Comments", "Specific Comments")
ListBoxEUMC.List = Array("Adoption of the Agenda (Provisional Agenda)", "Adoption of the Agenda (Outcome of Proceedings)", "Agenda Point", "Agenda Point (Possible)", "Information (Provisional Agenda)", "Information (Outcome of Proceedings)", "Next Meeting", "A.O.B. at 28", "Operation ALTHEA", "A.O.B. at 27")
ListBoxMA.List = Array("References", "INTRODUCTION AND AIM", "CONSIDERATIONS", "RECOMMENDATION(S)")
ListBoxIMD.List = Array("References", "SITUATION", "MISSION", "DIRECTION", "ANNEXES")
End Sub


Private Sub ComboBoxDocType_Change()
If ComboBoxDocType.Value = "Compilation of Comments" Then
FrameLO.Visible = False
FrameCoC.Visible = True
FrameEUMC.Visible = False
FrameMA.Visible = False
FrameIMD.Visible = False
End If
If ComboBoxDocType.Value = "Lessons Observation" Then
FrameLO.Visible = True
FrameCoC.Visible = False
FrameEUMC.Visible = False
FrameMA.Visible = False
FrameIMD.Visible = False
End If
If ComboBoxDocType.Value = "EUMC Meeting Documents" Then
FrameLO.Visible = False
FrameCoC.Visible = False
FrameEUMC.Visible = True
FrameMA.Visible = False
FrameIMD.Visible = False
End If
If ComboBoxDocType.Value = "Military Advice" Then
FrameLO.Visible = False
FrameCoC.Visible = False
FrameEUMC.Visible = False
FrameMA.Visible = True
FrameIMD.Visible = False
End If
If ComboBoxDocType.Value = "Initiating Military Directive" Then
FrameLO.Visible = False
FrameCoC.Visible = False
FrameEUMC.Visible = False
FrameMA.Visible = False
FrameIMD.Visible = True
End If
End Sub


Sub InsertExistingBuildingBlock()
Dim EVT_Stage01 As Template
Dim GeneralComments As BuildingBlock
Dim SpecificComments As BuildingBlock
Dim Table As BuildingBlock
Dim OoPAdoptionofAgenda As BuildingBlock
Dim AgendaPoint As BuildingBlock
Dim AgendaPointPossible As BuildingBlock
Dim OoPInformation As BuildingBlock
Dim OoPNextMeeting As BuildingBlock
Dim AOBat28 As BuildingBlock
Dim OpALTHEA As BuildingBlock
Dim AOBat27 As BuildingBlock
Dim ProvAgendaAdoption As BuildingBlock
Dim InformationAgenda As BuildingBlock
Dim ProvAgendaNotes As BuildingBlock
Dim References As BuildingBlock
Dim SITUATION As BuildingBlock
Dim MISSION As BuildingBlock
Dim DIRECTION As BuildingBlock
Dim Annexes As BuildingBlock
Dim INTRODUCTION As BuildingBlock
Dim CONSIDERATIONS As BuildingBlock
Dim RECOMMENDATIONS As BuildingBlock
Set EVT_Stage01 = ActiveDocument.AttachedTemplate
If ComboBoxDocType.Value = "Compilation of Comments" Then
If ListBoxCoC.Value = "General Comments" Then
Set GeneralComments = objTemplate.BuildingBlockTypes(wdTypeCustomQuickParts).Categories("Compilation of Comments").BuildingBlocks("GeneralComments")
GeneralComments.Insert Where = ActiveDocument.Bookmarks("EVTBookMark01").Range, RichText = True
ElseIf ComboBoxDocType.Value = "Lessons Observed" Then
Set Table = objTemplate.BuildingBlockTypes(wdTypeCustomQuickParts).Categories("Lesson Observation").BuildingBlocks("Table")
Table.Insert Where = ActiveDocument.Bookmarks("EVTBookMark01").Range, RichText = True
End If
End If
If ListBoxCoC.Value = "Specific Comments" Then
Set SpecificComments = objTemplate.BuildingBlockTypes(wdTypeCustomQuickParts).Categories("Compilation of Comments").BuildingBlocks("SpecificComments")
SpecificComments.Insert Where = ActiveDocument.Bookmarks("EVTBookMark02").Range, RichText = True
End If
End Sub

SamT
12-16-2017, 10:54 AM
None of the Declared Building Blocks in the last sub have any Values. It won't work with Null Building Blocks.

I refactored one sub for brevity and clarity. The first sub can be refactored similarly.

Private Sub ComboBoxDocType_Change()

FrameLO.Visible = False
FrameCoC.Visible = False
FrameEUMC.Visible = False
FrameMA.Visible = False
FrameIMD.Visible = False

With Me 'Added this Line and subsequent Dots (".") dunno if "ME' Is appropriate
Select Case .ComboBoxDocType.Value
Case "Compilation of Comments": .FrameCoC.Visible = True
Case "Lessons Observation": .FrameLO.Visible = True
Case "EUMC Meeting Documents": .FrameEUMC.Visible = True
Case "Military Advice": .FrameMA.Visible = True
Case "Initiating Military Directive": .FrameIMD.Visible = True
End Select
End With
End Sub


I also moved this thread to the Word Help Forum.

gmayor
12-17-2017, 12:16 AM
The orientation part of the macro does work but is affected by the order of processing


Private Sub CommandButtonCreate_Click()
With ActiveDocument.PageSetup
.PaperSize = wdPaperA4
.TopMargin = CentimetersToPoints(2.54)
.LeftMargin = CentimetersToPoints(2.54)
.RightMargin = CentimetersToPoints(2.54)
.BottomMargin = CentimetersToPoints(2.54)
End With
InsertExistingBuildingBlock
If ComboBoxDocType.ListIndex = 0 Then
If Selection.PageSetup.Orientation = 0 Then
Selection.PageSetup.Orientation = 1 'landscape
End If
Else
Selection.PageSetup.Orientation = 0 'portrait
End If
Me.Hide
End Sub


Some of your conditional logic cannot work e.g. ComboBoxDocType.Value = "Lessons Observed" does not exist - the value is "Lessons Observation". In any case the line containing it can never work as that condition never exists. There are only three states for ComboBoxDocType.Value so you could use case statements, and I would suggest using the listindex and then the spelling doesn't matter e.g.


Sub InsertExistingBuildingBlock()
Select Case ComboBoxDocType.ListIndex
Case Is = 0 'First item in list
If ListBoxCoC.Text = "General Comments" Then
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "GeneralComments"
End If
Case Is = 1 'Second item in list
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "Table"
If ListBoxCoC.Text = "Specific Comments" Then
AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SpecificComments"
End If
Case Else 'Nothing selected
End Select
End Sub

The macro calls the folllowing function to fill the bookmarks


Public Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'Graham Mayor - http://www.gmayor.com - Last updated - 17 Dec 2017
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim orng As Range
On Error GoTo lbl_Exit
With ActiveDocument
Set orng = .Bookmarks(strbmName).Range
Set orng = oTemplate.AutoTextEntries(strAutotext).Insert _
(Where:=orng, RichText:=True)
.Bookmarks.Add Name:=strbmName, Range:=orng
End With
lbl_Exit:
Exit Sub
End Sub


I have no idea what your other list boxes do - but the principles will be similar.

gmaxey
12-17-2017, 08:43 AM
Jumping in late here to offer my own two cents (pence) :-). I use buildingblocks (AutoText) quite a bit and employ several of the Galleries and custom categories. With that practice, it is possible to have two or more building blocks defined such that Word may or may not know the correct BB to insert using the old AutoTextEntries method. Therefore, I try to explicitly define the BuidlingBlock to define by template, gallery, catefory and name.

I've also included my code for insert at a content control vice bookmark:


Sub InsertDemos()
' 'For building blocks in the default template "Normal.dotm", default Gallery "AutoText"
' 'and default Category "General" simmply pass the bookmark and building block names as arguments e.g.,:
' BBtoBM "BMTarget_Header", "GKM"
' 'For building blocks in the document attached template (other than Normal.dotm),
' 'pass the bookmark name, building block name and ThisDocument.AttachedTemplate.Name as arguments.
' 'If the gallery or category is not the default, pass them as well e.g.,
' BBtoBM "BMTarget_1", "Car", ThisDocument.AttachedTemplate.Name, 29, "Classic"
' 'For building blocks in a named tempalte (not Normal.dotm or not the attached template), pass the template
' 'name (without extenstion) e.g., a Table of Contents from the built in building blocks.
' BBtoBM "BMTarget_1", "Manual Table", "Built-in Building Blocks", 14, "Built-in"
'Similar process using a titled content control.
BBtoCC "CCTarget_1", "GKM"
BBtoCC "CCTarget_Header", "Car", ThisDocument.AttachedTemplate.Name, 29, "Classic"
BBtoCC "CCTarget_1", "Manual Table", "Built-in Building Blocks", 14, "Built-in"
BBtoCC "CCTarget_1", "Car", ThisDocument.AttachedTemplate.Name, 29, "Classic"
lbl_Exit:
Exit Sub
End Sub
Public Sub BBtoBM(BMName As String, BBName As String, _
Optional Template As String = "Normal", _
Optional GalleryIndex As Long = 9, _
Optional Category As String = "General")
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/18/2016
'To use BuildingBlocks from a Gallery other than "AutoText" pass the appropriate
'number as the Gallery argument.
'1 Quick Parts|2 Cover Pages|3 Equations|4 Footers|5 Headers|6 Page Numbers|7 Tables
'8 Watermarks|9 AutoText|10 Text Boxes|11 Page Numbers (Top of Page)|12 Page Numbers (Bottom of Page)
'13 Page Numbers (Margins)|14 Table of Contents|15 Custom Quick Parts|16 Custom Cover Pages
'17 Custom Equations|18 Custom Footers|19 Custom Headers|20 Custom Page Numbers|21 Custom Tables
'22 Custom Watermarks|23 Custom AutoText|24 Custom Text Boxes|25 Custom Page Numbers (Top of Page)
'26 Custom Page Numbers (Bottom of Page)|27 Custom Page Numbers (Margins)|28 Custom Tables
'29 Custom 1|30 Custom 2|31 Custom 3|32 Custom 4|33 Custom 5|34 Bibliographies|'35 Custom Bibliographies
Dim oTmpLoaded As Template, oTmp As Template
Select Case Template
Case "Normal"
Set oTmp = Templates("Normal.dotm")
Case ActiveDocument.AttachedTemplate.Name
Set oTmp = ActiveDocument.AttachedTemplate 'Templates(Template)
Case Else
If Template = "Built-in Building Blocks" Then Templates.LoadBuildingBlocks
For Each oTmpLoaded In Templates
If UCase(CreateObject("scripting.filesystemobject").GetBaseName(oTmpLoaded.Name)) = UCase(Template) Then
Set oTmp = oTmpLoaded
Exit For
End If
Next
End Select
If Not oTmp Is Nothing Then
If ActiveDocument.Bookmarks.Exists(BMName) Then
On Error Resume Next
ActiveDocument.Bookmarks.Add Name:=BMName, _
Range:=oTmp.BuildingBlockTypes(GalleryIndex).Categories(Category).BuildingB locks(BBName).Insert _
(Where:=ActiveDocument.Bookmarks(BMName).Range, RichText:=True)
If Not Err.number = 0 Then
MsgBox "The defined buildingblock is not available.", vbInformation + vbOKOnly, "BUILDINGBLOCK NOT FOUND"
End If
Else
MsgBox "The defined bookmark is not found in the document.", vbInformation + vbOKOnly, "BOOKMARK NOT FOUND"
End If
Else
MsgBox "The building block template is not available.", vbInformation + vbOKOnly, "TEMPLATE NOT LOADED"
End If

lbl_Exit:
Set oTmpLoaded = Nothing: Set oTmp = Nothing
Exit Sub
End Sub
Public Sub BBtoCC(CCTitle As String, BBName As String, _
Optional Template As String = "Normal", _
Optional GalleryIndex As Long = 9, _
Optional Category As String = "General")
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/18/2016

'To use BuildingBlocks from a Gallery other than "AutoText" pass the appropriate
'number as the Gallery argument.
'1 Quick Parts|2 Cover Pages|3 Equations|4 Footers|5 Headers|6 Page Numbers|7 Tables
'8 Watermarks|9 AutoText|10 Text Boxes|11 Page Numbers (Top of Page)|12 Page Numbers (Bottom of Page)
'13 Page Numbers (Margins)|14 Table of Contents|15 Custom Quick Parts|16 Custom Cover Pages
'17 Custom Equations|18 Custom Footers|19 Custom Headers|20 Custom Page Numbers|21 Custom Tables
'22 Custom Watermarks|23 Custom AutoText|24 Custom Text Boxes|25 Custom Page Numbers (Top of Page)
'26 Custom Page Numbers (Bottom of Page)|27 Custom Page Numbers (Margins)|28 Custom Tables
'29 Custom 1|30 Custom 2|31 Custom 3|32 Custom 4|33 Custom 5|34 Bibliographies|'35 Custom Bibliographies
Dim oTmpLoaded As Template, oTmp As Template
Dim oCC As ContentControl
Select Case Template
Case "Normal"
Set oTmp = Templates("Normal.dotm")
Case ActiveDocument.AttachedTemplate.Name
Set oTmp = ActiveDocument.AttachedTemplate
Case Else
If Template = "Built-in Building Blocks" Then Templates.LoadBuildingBlocks
For Each oTmpLoaded In Templates
If UCase(CreateObject("scripting.filesystemobject").GetBaseName(oTmpLoaded.Name)) = UCase(Template) Then
Set oTmp = oTmpLoaded
Exit For
End If
Next
End Select
If Not oTmp Is Nothing Then
On Error Resume Next
Set oCC = ActiveDocument.SelectContentControlsByTitle(CCTitle).Item(1)
If Not oCC.Type = wdContentControlRichText Then oCC.Type = wdContentControlRichText
If Not oCC Is Nothing Then
oTmp.BuildingBlockTypes(GalleryIndex).Categories(Category).BuildingBlocks(B BName).Insert oCC.Range, True
If Not Err.number = 0 Then
MsgBox "The defined buildingblock is not available.", vbInformation + vbOKOnly, "BUILDINGBLOCK NOT FOUND"
End If
Else
MsgBox "The defined content control is not found in the document.", vbInformation + vbOKOnly, "BOOKMARK NOT FOUND"
End If
Else
MsgBox "The building block template is not available.", vbInformation + vbOKOnly, "TEMPLATE NOT LOADED"
End If

lbl_Exit:
Set oTmpLoaded = Nothing: Set oTmp = Nothing: Set oCC = Nothing
Exit Sub
End Sub

gmayor
12-17-2017, 10:26 PM
I would agree in principle, but as the autotexts are called directly from the document template, I would have thought that it was less likely that there would be duplicated names in that template.

Paskie_be
12-18-2017, 02:18 AM
This works ace, now. Thanks!

P.

Paskie_be
12-18-2017, 02:51 AM
The second part is still a bit iffy. If I choose Lessons Observation, the table comes in at the right place. The Compilation of Comments building Blocks, though, do not get inserted. The seems to an incompatibility between the case and the if clauses.

Paskie_be
12-18-2017, 03:09 AM
Works great! Thanks. BTW, is there and incompatibility of use between case and if statements? Can they be combined?

gmaxey
12-18-2017, 04:07 AM
What do you mean? Give and example of what you want to do.

Paskie_be
12-18-2017, 05:02 AM
What do you mean? Give and example of what you want to do.<br>
<br>
Well, when "Compilation of Comments" is chosen from the ComboBoxDocType, then the Compilation of Comments frame pops up, giving the opportunity for the drafter to chose either General Comments, Specific Comments, or both. The General Comments item refers to the GeneralComments building Block in the Template and the Specific Comments to the SpecificComments Building Block. Those then would have to be entered at the EVTBookMark01 and the EVTBookMark02, respectively.<br>
If the "Lessons Observation" document type is chosen from the drop-down list, then there is only one Building block associated to it, namely Table, which will then go to EVTBookMark01 in the document produced.<br>
<br>
This process is then repeated for the subsequent types of documents for which I am in the process of preparing the script along your lines. I have tested it out and only the CASE statement NOT combined with IF statements actually works:<br>
<br>

I am not sure I am making myself quite clear. I am sorry for that.<br>
<br>
Paskie_be<br>
<br>

Paskie_be
12-18-2017, 05:10 AM
The comprehensive coding is:
Sub InsertExistingBuildingBlock()
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 Is = 2
If ListBoxMA.Text = "References" Then
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "References"
End If
If ListBoxMA.Text = "INTRODUCTION AND AIM" Then
AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "INTRODUCTION"
End If
If ListBoxMA.Text = "CONSIDERATIONS" Then
AutoTextToBM "EVTBookMark03", ActiveDocument.AttachedTemplate, "CONSIDERATIONS"
End If
If ListBoxMA.Text = "RECOMMENDATION(S)" Then
AutoTextToBM "EVTBookMark04", ActiveDocument.AttachedTemplate, "RECOMMENDATIONS"
End If
Case Is = 3
If ListBoxIMD.Text = "References" Then
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "References"
End If
If ListBoxIMD.Text = "SITUATION" Then
AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SITUATION"
End If
If ListBoxIMD.Text = "MISSION" Then
AutoTextToBM "EVTBookMark03", ActiveDocument.AttachedTemplate, "MISSION"
End If
If ListBoxIMD.Text = "DIRECTION" Then
AutoTextToBM "EVTBookMark04", ActiveDocument.AttachedTemplate, "DIRECTION"
End If
If ListBoxIMD.Text = "ANNEXES" Then
AutoTextToBM "EVTBookMark05", ActiveDocument.AttachedTemplate, "Annexes"
End If
Case Else 'Nothing selected
End Select
End Sub

gmaxey
12-18-2017, 05:27 AM
You are not making yourself clear. Yes, Select Case and If statements can be used together:


Sub Demo()
Dim lngIndex As Long, lngTest1 As Long, lngTest2 As Long
lngTest1 = 1
For lngIndex = 1 To 3
lngTest2 = lngIndex
Select Case lngTest1
Case 1
If lngTest2 = 3 Then
MsgBox "Viola"
Else
MsgBox lngTest2 & " isn't = to 3"
End If
Case 2

Case 3
End Select
Next
End Sub

Paskie_be
12-18-2017, 05:41 AM
OK.
When a user opens the template as a new document, this screen pops up inviting him/her to choose a document type. The choice is between Compilation of Comments, Lessons Observation, Military Advice and Initiating Military Directive (IMD). According to the the document picked, a second frame opens in the same interface inviting the user to choose which parts of a document (=the building blocks) he wishes to see in his final document. For the Compilation of Comments, there are two options - General Comments and Specific Comments; for the Lessons Observation, there is only on (Table); for the Military Advice, there is References, INTRODUCTION AND AIM, CONSIDERATIONS and RECOMMENDATIONS; and for the IMD, we have References, SITUATION, MISSION, DIRECTION and ANNEXES.
Depending on the number of building blocks, each one finds its place in the document at a specific bookmarks named EVTBookMark01 to 08.
So, if the Compilation of Comments os the document to be produced and it is to contain a paragraph called General Comments and another called Specific Comments, the former goes to bookmark EVTBookMark01 in the document and the latter to bookmark EVTBookMark02.
The same goes for the other document, mutatis mutandis.

Paskie_be
12-18-2017, 05:43 AM
The trouble is I cannot attach my document to this thread. It might've made things easier to explain.

Paskie_be
12-18-2017, 06:12 AM
Could it be that the Private Sub only allow for one building block to be inserted at one bookmark and not all selected blocks to find their place at the relevant bookmarks?

gmaxey
12-18-2017, 07:36 AM
What Private Sub? The code graham and I have provided inserts 1 defined building block at 1 defined bookmark. If you wanted to insert BB A at bookmark A and BB B at bookmark B then you would have to call the sub twice.

Paskie_be
12-18-2017, 08:30 AM
What Private Sub? The code graham and I have provided inserts 1 defined building block at 1 defined bookmark. If you wanted to insert BB A at bookmark A and BB B at bookmark B then you would have to call the sub twice.

Thanks.

But, as you can see, I am only a starter. How can I do that?

gmaxey
12-18-2017, 08:59 AM
You just call it one after the other.
Select Case
Case Some Case
If Some Conditoion
AutoTextToBM "EVTBookMark02", ActiveDocument.AttachedTemplate, "SITUATION"
AutoTextToBM "EVTBookMark01", ActiveDocument.AttachedTemplate, "References"
AutoTextToBM "EVTBookMark03", ActiveDocument.AttachedTemplate, "MISSION"
End If
End Select

Paskie_be
12-18-2017, 09:11 AM
Thank you very much indeed. I'll try tomorrow because it this time of the day when we knock off work on this side of the pond. Your help is very much appreciated! I'll let you know tomorrow what I could get out of it.

Paskie_be
12-18-2017, 09:19 AM
I've got this:
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

And this is where the system blocks: under each case, I do not have multiple instructions but I have multiple Ifs inducing instructions. :dunno

gmayor
12-18-2017, 10:32 PM
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

Paskie_be
12-19-2017, 01:34 AM
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.

gmayor
12-19-2017, 04:53 AM
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

Paskie_be
12-19-2017, 05:25 AM
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?

gmayor
12-19-2017, 06:25 AM
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.

Paskie_be
12-19-2017, 07:27 AM
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.

gmayor
12-19-2017, 10:35 PM
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 SelectIn 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.