Something like the following might work.
This code assumes a UserForm called UserForm1 with five ComboBox controls
called ComboBox1, ComboBox2, ComboBox3, ComboBox4 and ComboBox5, as well as
an 'OK' and a 'Cancel' button. It also assumes five bookmarks in the template
for displaying the results of the ComboBoxes called ExhibitA, ExhibitB,
ExhibitC, ExhibitD and ExhibitE, and five additional bookmarks for
showing/hiding the various "Exhibit" paragraphs called NoExhibitA,
NoExhibitB, NoExhibitC, NoExhibitD and NoExhibitE.
The code in the main module for initiating the process is:
Option Explicit
Public bNewDoc As Boolean
Public myDoc As Document
Public myForm As UserForm1
Dim Combo1Val As String
Dim Combo1Idx As Integer
Dim Combo2Val As String
Dim Combo2Idx As Integer
Dim Combo3Val As String
Dim Combo3Idx As Integer
Dim Combo4Val As String
Dim Combo4Idx As Integer
Dim Combo5Val As String
Dim Combo5Idx As Integer
Sub AutoNew()
Set myDoc = ActiveDocument
Set myForm = New UserForm1
Load myForm
myForm.Show
If bNewDoc = True Then CollectUserFormValues
Unload myForm
Set myForm = Nothing
If bNewDoc Then BuildDoc Else myDoc.Close wdDoNotSaveChanges
End Sub
This code displays an instance of UserForm1, at which point control is
handed over to the UserForm. The code in the UserForm1 module is:
Option Explicit
Dim Combo1Array() As Variant
Dim Combo2Array() As Variant
Dim Combo3Array() As Variant
Dim Combo4Array() As Variant
Dim Combo5Array() As Variant
Sub UserForm_Initialize()
BuildCombo1List
InitializeCombos
End Sub
Sub BuildCombo1List()
Dim i As Integer
Dim ExhibitName As String
ReDim Combo1Array(5) As Variant
Combo1Array(0) = "[SELECT]"
For i = 1 To 5
ExhibitName = "Exhibit " & i
Combo1Array(i) = ExhibitName
Next i
With ComboBox1
.List = Combo1Array
.ListIndex = 0
End With
End Sub
Sub InitializeCombos()
If ComboBox1.ListIndex < 1 Then DisableCombo2 Else EnableCombo2
If ComboBox2.ListIndex < 1 Then DisableCombo3 Else EnableCombo3
If ComboBox3.ListIndex < 1 Then DisableCombo4 Else EnableCombo4
If ComboBox4.ListIndex < 1 Then DisableCombo5 Else EnableCombo5
End Sub
Sub ComboBox1_Change()
If ComboBox1.ListIndex < 1 Then DisableCombo2 Else EnableCombo2
End Sub
Sub DisableCombo2()
With ComboBox2
.Enabled = False
.Locked = True
.TabStop = False
.BackColor = &H8000000F
.Clear
End With
End Sub
Sub EnableCombo2()
BuildCombo2List
With ComboBox2
.Enabled = True
.Locked = False
.TabStop = True
.BackColor = &H80000005
.List = Combo2Array
.ListIndex = 0
End With
End Sub
Sub BuildCombo2List()
Dim i As Integer
Dim DelIndex As Integer
ReDim Combo2Array(5) As Variant
DelIndex = ComboBox1.ListIndex
Combo2Array = Combo1Array
If DelIndex <= 4 Then
For i = DelIndex To 4
Combo2Array(i) = Combo2Array(i + 1)
Next i
End If
ReDim Preserve Combo2Array(4) As Variant
End Sub
Sub ComboBox2_Change()
If ComboBox2.ListIndex < 1 Then DisableCombo3 Else EnableCombo3
End Sub
Sub DisableCombo3()
With ComboBox3
.Enabled = False
.Locked = True
.TabStop = False
.BackColor = &H8000000F
.Clear
End With
End Sub
Sub EnableCombo3()
BuildCombo3List
With ComboBox3
.Enabled = True
.Locked = False
.TabStop = True
.BackColor = &H80000005
.List = Combo3Array
.ListIndex = 0
End With
End Sub
Sub BuildCombo3List()
Dim i As Integer
Dim DelIndex As Integer
ReDim Combo3Array(4) As Variant
DelIndex = ComboBox2.ListIndex
Combo3Array = Combo2Array
If DelIndex <= 3 Then
For i = DelIndex To 3
Combo3Array(i) = Combo3Array(i + 1)
Next i
End If
ReDim Preserve Combo3Array(3) As Variant
End Sub
Sub ComboBox3_Change()
If ComboBox3.ListIndex < 1 Then DisableCombo4 Else EnableCombo4
End Sub
Sub DisableCombo4()
With ComboBox4
.Enabled = False
.Locked = True
.TabStop = False
.BackColor = &H8000000F
.Clear
End With
End Sub
Sub EnableCombo4()
BuildCombo4List
With ComboBox4
.Enabled = True
.Locked = False
.TabStop = True
.BackColor = &H80000005
.List = Combo4Array
.ListIndex = 0
End With
End Sub
Sub BuildCombo4List()
Dim i As Integer
Dim DelIndex As Integer
ReDim Combo4Array(3) As Variant
DelIndex = ComboBox3.ListIndex
Combo4Array = Combo3Array
If DelIndex <= 2 Then
For i = DelIndex To 2
Combo4Array(i) = Combo4Array(i + 1)
Next i
End If
ReDim Preserve Combo4Array(2) As Variant
End Sub
Sub ComboBox4_Change()
If ComboBox4.ListIndex < 1 Then DisableCombo5 Else EnableCombo5
End Sub
Sub DisableCombo5()
With ComboBox5
.Enabled = False
.Locked = True
.TabStop = False
.BackColor = &H8000000F
.Clear
End With
End Sub
Sub EnableCombo5()
BuildCombo5List
With ComboBox5
.Enabled = True
.Locked = False
.TabStop = True
.BackColor = &H80000005
.List = Combo5Array
.ListIndex = 0
End With
End Sub
Sub BuildCombo5List()
Dim i As Integer
Dim j As Integer
Dim DelIndex As Integer
ReDim Combo5Array(2) As Variant
DelIndex = ComboBox4.ListIndex
Combo5Array = Combo4Array
If DelIndex <= 1 Then
For i = DelIndex To 1
Combo5Array(i) = Combo5Array(i + 1)
Next i
End If
ReDim Preserve Combo5Array(1) As Variant
End Sub
Sub btnOK_Click()
bNewDoc = True
Me.Hide
End Sub
Sub btnCancel_Click()
Dim myResult As Integer
myResult = MsgBox("Confirm?", vbYesNo, "Cancel")
If myResult = 6 Then
bNewDoc = False
Me.Hide
End If
End Sub
Control is then handed back to the main module and, assuming that the 'OK'
button is clicked, continues as follows:
Sub CollectUserFormValues()
With myForm
With .ComboBox1
Combo1Val = .Value
Combo1Idx = .ListIndex
End With
With .ComboBox2
Combo2Val = .Value
Combo2Idx = .ListIndex
End With
With .ComboBox3
Combo3Val = .Value
Combo3Idx = .ListIndex
End With
With .ComboBox4
Combo4Val = .Value
Combo4Idx = .ListIndex
End With
With .ComboBox5
Combo5Val = .Value
Combo5Idx = .ListIndex
End With
End With
End Sub
Sub BuildDoc()
InsertBookmarkValue "ExhibitA", Combo1Val
If Combo2Idx > 0 Then
ShowBookmarkRange "NoExhibitB"
InsertBookmarkValue "ExhibitB", Combo2Val
Else: HideBookmarkRange "NoExhibitB"
End If
If Combo3Idx > 0 Then
ShowBookmarkRange "NoExhibitC"
InsertBookmarkValue "ExhibitC", Combo3Val
Else: HideBookmarkRange "NoExhibitC"
End If
If Combo4Idx > 0 Then
ShowBookmarkRange "NoExhibitD"
InsertBookmarkValue "ExhibitD", Combo4Val
Else: HideBookmarkRange "NoExhibitD"
End If
If Combo5Idx > 0 Then
ShowBookmarkRange "NoExhibitE"
InsertBookmarkValue "ExhibitE", Combo5Val
Else: HideBookmarkRange "NoExhibitE"
End If
With ActiveWindow.View
.ShowBookmarks = False
.ShowHiddenText = False
.ShowAll = False
End With
End Sub
Sub InsertBookmarkValue(BkmkName As String, Value As String)
With myDoc
If .Bookmarks.Exists(BkmkName) Then
Dim myRange As Range
Set myRange = .Bookmarks(BkmkName).Range
myRange.Text = Value
.Bookmarks.Add BkmkName, myRange
End If
End With
End Sub
Sub ShowBookmarkRange(BkmkName As String)
With myDoc
If .Bookmarks.Exists(BkmkName) Then
..Bookmarks(BkmkName).Range.Font.Hidden = False
End With
End Sub
Sub HideBookmarkRange(BkmkName As String)
With myDoc
If .Bookmarks.Exists(BkmkName) Then
..Bookmarks(BkmkName).Range.Font.Hidden = True
End With
End Sub
See how this works in action, create a template containing a Module and the
UserForm described above, and simply copy and paste this sample code into it
(just be careful of the line breaks that the web interface adds).
Alternatively, you can email me and I'll send you the template.
Note that this code does not quite achieve all of your objectives - it
doesn't have functionality for prompting the user for the last Exhibit and
limiting their choices accordingly, and the names of the Exhibits aren't
quite right - but it was the best I could do in just a couple of hours.
However, I have some time today and will continue working on it. I have an
idea of how to meet all of your goals, but it will take me a bit of time to
get there.
BTW, this is something that I've always wanted to do, so thanks for the
opportunity and motivation. I'm sure it will come in handy for me someday.
--
Cheers!
Gordon
Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.