Attribute VB_Name = "modInit"
Option Explicit

Const LIST_BOX_PROG_ID      As String = "Forms.ListBox.1"
Const COMMAND_BTN_PROG_ID   As String = "Forms.CommandButton.1"

Const LIST_AVAILABLE        As String = "lstAvailable"
Const LIST_SELECTED         As String = "lstSelected"

Const CMD_OK                As String = "cmdOK"
Const CMD_CANCEL            As String = "cmdCancel"
Const CMD_ADD               As String = "cmdAdd"
Const CMD_REMOVE            As String = "cmdRemove"

Const CMD_OK_CAPTION        As String = "OK"
Const CMD_CANCEL_CAPTION    As String = "Cancel"
Const CMD_ADD_CAPTION       As String = "Add"
Const CMD_REMOVE_CAPTION    As String = "Remove"

Const CMD_OK_ACCEL          As String = "O"
Const CMD_CANCEL_ACCEL      As String = "C"
Const CMD_ADD_ACCEL         As String = "A"
Const CMD_REMOVE_ACCEL      As String = "R"

Const CODE_TEXT             As String = "WizardCode.txt"

Sub Init()
    AddFormToProject
End Sub

Private Sub AddFormToProject()
    ' Creates new form, adds controls and code.

    Dim vbpProject      As VBIDE.VBProject
    Dim vbcComp         As VBIDE.VBComponent
    Dim frmForm         As MSForms.UserForm
    Dim lstAvailable    As MSForms.ListBox
    Dim lstSelected     As MSForms.ListBox
    Dim cmdBtn          As MSForms.CommandButton
    Dim strHeight       As String
    Dim strWidth        As String
    Dim lngMidX1        As Long
    Dim lngMidX2        As Long
    Dim lngMidY         As Long
    
    On Error GoTo AddFormToProject_Err
    
    ' Return reference to active project.
    Set vbpProject = gobjVBE.ActiveVBProject
    ' Add new UserForm.
    Set vbcComp = vbpProject.VBComponents.Add(vbext_ct_MSForm)
    
    ' Return reference to component's designer, which is
    ' a UserForm object.
    Set frmForm = vbcComp.Designer
    frmForm.Caption = ""
    
    ' Units are in points (1 point = 1/72 inch).
    ' Specify height as 4 inches and width as 6 inches.
    ' Other dimensions will be fractions of these.
    strHeight = 4 * 72
    strWidth = 6 * 72
    
    ' Add multiselect list boxes.
    Set lstAvailable = AddListBox(frmForm, _
        LIST_AVAILABLE, strHeight * 0.4, strWidth * 0.2, _
        strHeight * 0.1, strWidth * 0.1, fmMultiSelectExtended)
        
    Set lstSelected = AddListBox(frmForm, _
        LIST_SELECTED, strHeight * 0.4, strWidth * 0.2, _
        strHeight * 0.1, strWidth * 0.6, fmMultiSelectExtended)

    ' Find X coordinates between two list boxes. Divide space
    ' into thirds.
    lngMidX1 = (lstAvailable.Top + lstAvailable.Height) * 0.33
    lngMidX2 = (lstAvailable.Top + lstAvailable.Height) * 0.67

    ' Find center Y coordinate between two list boxes.
    lngMidY = (lstAvailable.Left + lstSelected.Left + lstSelected.Width) / 2

    ' Add command buttons.
    Set cmdBtn = AddCommandButton(frmForm, _
        CMD_ADD, CMD_ADD_CAPTION, CMD_ADD_ACCEL, 24, 72, _
        lngMidX1, lngMidY - 36)
    
    Set cmdBtn = AddCommandButton(frmForm, _
        CMD_REMOVE, CMD_REMOVE_CAPTION, CMD_REMOVE_ACCEL, 24, 72, _
        lngMidX2, lngMidY - 36)
    
    ' Find horizontal center of each list box.
    lngMidX1 = lstAvailable.Left + (lstAvailable.Width / 2)
    lngMidX2 = lstSelected.Left + (lstSelected.Width / 2)
    
    ' Find vertical coordinate 24 points below list boxes.
    lngMidY = lstAvailable.Top + lstAvailable.Height + 24
    
    Set cmdBtn = AddCommandButton(frmForm, _
        CMD_OK, CMD_OK_CAPTION, CMD_OK_ACCEL, 24, 72, _
        lngMidY, lngMidX1 - 36)
        cmdBtn.Default = True
    
    Set cmdBtn = AddCommandButton(frmForm, _
        CMD_CANCEL, CMD_CANCEL_CAPTION, CMD_CANCEL_ACCEL, , , _
        lngMidY, lngMidX2 - 36)
        cmdBtn.Cancel = True

    ' Prompt user to name form.
    vbcComp.Name = InputBox("Enter name for new form:")

    ' Add code to form's module.
    vbcComp.CodeModule.AddFromFile App.Path & "\" & CODE_TEXT
    
    ' Display form designer.
    With vbcComp.DesignerWindow
        .Height = strHeight
        .Width = strWidth
        .WindowState = vbext_ws_Normal
        .Visible = True
    End With

AddFormToProject_End:
    Exit Sub

AddFormToProject_Err:
    Select Case Err.Number
        Case 50135
            ' User entered invalid name for form.
            If MsgBox("The name you have entered is invalid or is " _
                & "already in use. Do you want to enter a different " _
                & "name?", vbYesNo) = vbYes Then
                Resume
            Else
                ' Ignore and allow system to provide name.
                Resume Next
            End If
        Case Else
            MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf _
                & Err.Description
            Resume AddFormToProject_End
    End Select
End Sub

Function AddListBox(frmForm As MSForms.UserForm, _
    strName As String, _
    Optional lngHeight As Long, _
    Optional lngWidth As Long, _
    Optional lngTop As Long, _
    Optional lngLeft As Long, _
    Optional lngMultiSelect As fmMultiSelect = _
        fmMultiSelectSingle) As MSForms.ListBox

    Dim lstList As MSForms.ListBox
    
    ' Create new list box on form.
    Set lstList = frmForm.Controls.Add(LIST_BOX_PROG_ID, _
        strName, True)
    
    ' Set list box properties.
    With lstList
        If lngHeight > 0 Then .Height = lngHeight
        If lngWidth > 0 Then .Width = lngWidth
        If lngLeft > 0 Then .Left = lngLeft
        If lngTop > 0 Then .Top = lngTop
        .MultiSelect = lngMultiSelect
    End With

    Set AddListBox = lstList
End Function

Function AddCommandButton(frmForm As MSForms.UserForm, _
    strName As String, _
    strCaption As String, _
    Optional strAccelerator As String, _
    Optional lngHeight As Long, _
    Optional lngWidth As Long, _
    Optional lngTop As Long, _
    Optional lngLeft As Long) As MSForms.CommandButton

    Dim cmdBtn As MSForms.CommandButton
    
    ' Create new command button on form.
    Set cmdBtn = frmForm.Controls.Add(COMMAND_BTN_PROG_ID, _
        strName, True)
    
    ' Set properties.
    With cmdBtn
        .Caption = strCaption
        .Accelerator = strAccelerator
        If lngHeight > 0 Then .Height = lngHeight
        If lngWidth > 0 Then .Width = lngWidth
        If lngLeft > 0 Then .Left = lngLeft
        If lngTop > 0 Then .Top = lngTop
    End With

    Set AddCommandButton = cmdBtn
End Function




