View Single Post
 
Old 02-10-2017, 01:38 PM
gmaxey gmaxey is offline Windows 7 32bit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

You certainly don't need all of that code. Assuming your list is small like your example and relatively static:

Code:
Option Explicit
Private Sub UserForm_Initialize()
  With EmpNameList
    .MultiSelect = 1
    .AddItem
    .List(.ListCount - 1, 0) = "Ally"
    .List(.ListCount - 1, 1) = "1234"
    .List(.ListCount - 1, 2) = "Ally"
    .List(.ListCount - 1, 3) = "Ally"
    .AddItem
    .List(.ListCount - 1, 0) = "Linda"
    .List(.ListCount - 1, 1) = "1234"
    .List(.ListCount - 1, 2) = "Jerry"
    .AddItem
    .List(.ListCount - 1, 0) = "Cameron"
    .List(.ListCount - 1, 1) = "6789"
    .List(.ListCount - 1, 2) = "Tom"
  End With
  cmdOK.Enabled = False
End Sub
Private Sub Cost_Center_Change()
  Validate
End Sub
Private Sub Auth_Date1_Change()
  Validate
End Sub
Private Sub Auth_Date2_Change()
  Validate
End Sub
Private Sub cmdOK_Click()
Dim fileName As String
Dim lngIndex As Long
Dim oDoc As Document
  For lngIndex = 0 To EmpNameList.ListCount - 1
    If EmpNameList.Selected(lngIndex) Then
      Set oDoc = Documents.Add("LaborAuthorizationForm.docm") 'You should use a template not a document.
      With oDoc
        'Replace these msgboxes with whatever code you want to use to write to the document targets (i.e., table cells, bookmarks, content controls, etc.)
        MsgBox EmpNameList.List(lngIndex, 0)
        MsgBox EmpNameList.List(lngIndex, 1)
        MsgBox EmpNameList.List(lngIndex, 2)
        MsgBox Cost_Center
        MsgBox Auth_Date1
        MsgBox Auth_Date2
        oDoc.SaveAs2 EmpNameList.List(lngIndex, 0) & "_LaborAuthorizationForm"
        oDoc.Close
        Set oDoc = Nothing
    End If
  Next
End Sub
Private Sub cmdCancel_Click()
  Hide
End Sub
Sub Validate()
  cmdOK.Enabled = True
  If Cost_Center = vbNullString Then cmdOK.Enabled = False
  If Not IsDate(Auth_Date1) Then cmdOK.Enabled = False
  If Not IsDate(Auth_Date2) Then cmdOK.Enabled = False
End Sub
Otherwise you should consider populating the userform listbox using a database.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote