Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2017, 06:55 AM
BlackGirlMagic's Avatar
BlackGirlMagic BlackGirlMagic is offline Create a ComboBox in a Form that creates a new document and autofills a textBox Windows 7 64bit Create a ComboBox in a Form that creates a new document and autofills a textBox Office 2010 64bit
Novice
Create a ComboBox in a Form that creates a new document and autofills a textBox
 
Join Date: Feb 2017
Location: Washington, DC
Posts: 3
BlackGirlMagic is on a distinguished road
Question Create a ComboBox in a Form that creates a new document and autofills a textBox


Is it possible to create a ComboBox in a form that autofills a textBox? So I have multiple names in a combo box that has numbers associated to each. I want to be able to select more than one name and have the form create separate documents with one person per document and their associated name. Is that possible? How could I go about doing that?
Reply With Quote
  #2  
Old 02-10-2017, 03:08 AM
gmayor's Avatar
gmayor gmayor is offline Create a ComboBox in a Form that creates a new document and autofills a textBox Windows 10 Create a ComboBox in a Form that creates a new document and autofills a textBox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you want multiple selections, you need a list box rather than a combo-box. It is not clear what the relevance of the text box is, or how it relates to multiple selections. However, the basic premise of making multiple selections from a list of items and creating a new document for each is certainly viable.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-10-2017, 12:24 PM
BlackGirlMagic's Avatar
BlackGirlMagic BlackGirlMagic is offline Create a ComboBox in a Form that creates a new document and autofills a textBox Windows 7 64bit Create a ComboBox in a Form that creates a new document and autofills a textBox Office 2010 64bit
Novice
Create a ComboBox in a Form that creates a new document and autofills a textBox
 
Join Date: Feb 2017
Location: Washington, DC
Posts: 3
BlackGirlMagic is on a distinguished road
Red face Follow up question now using ListBox

This is what I've got so far. How can I save all of the variables (i.e. EmpName, EmpNum, ManagerName, etc) and when the new document opens up store those values into assigned textboxes?

Code:
Option Explicit

Private Sub UserForm_Initialize()
    EmpNameList.MultiSelect = 2       
    With EmpNameList
        .AddItem "Ally"
        .AddItem "Linda"
        .AddItem "Cameron"
        .AddItem "Ron"
        .AddItem "Tony"
    End With
    
End Sub

Private Sub EmpNameList_Change()
Select Case EmpName
 Case "Ally"
    With EmpNum
    EmpNum.Text = "1234"
    End With
    
    With ManagerName
    ManagerName.Text = "Jerry"
    End With
    
 Case "Linda"
    With EmpNum
    EmpNum.Text = "4567"
    End With

    With ManagerName
    ManagerName.Text = "Mike"
    End With

 Case "Cameron"
    With EmpNum
    EmpNum.Text = "6789"
    End With

    With ManagerName
    ManagerName.Text = "Tom"
    End With

 Case "Ron"
    With EmpNum
    EmpNum.Text = "0132"
    End With

    With ManagerName
    ManagerName.Text = "Ralph"
    End With

 Case "Tony"
    With EmpNum
    EmpNum.Text = "6783"
    End With

    With ManagerName
    ManagerName.Text = "Tom"
    End With
 End Select
End Sub

Private Sub cmdOK_Click()
    Dim fileName As String
    Dim Cost_Center
    Dim Auth_Date1
    Dim Auth_Date2
    Dim Emp_Name
    Dim Emp_Num
    Dim Man_Name
    EmpName.List = Array("Ally, Linda, Cameron, Ron, Tony")
    
'   Checking for completion of the form
    If CostCenter.Value = "" Then
        MsgBox "Please enter the Cost Center.", vbExclamation, "Input Data"
        CostCenter.SetFocus
        Exit Sub
    End If
    
    If AuthDate1.Value = "" Then
        MsgBox "Please enter the Period of Authorization.", vbExclamation, "Input Data"
        AuthDate1.SetFocus
        Exit Sub
    End If
    
    If AuthDate2.Value = "" Then
        MsgBox "Please enter the Period of Authorization.", vbExclamation, "Input Data"
        AuthDate2.SetFocus
        Exit Sub
    End If
    
    If EmpName.Value = "" Then
        MsgBox "Please select the Employee Name(s).", vbExclamation, "Input Data"
        EmpName.SetFocus
        Exit Sub
    End If
    
    If EmpNum.Value = "" Then
        MsgBox "Please enter the Employee Number(s).", vbExclamation, "Input Data"
        EmpNum.SetFocus
        Exit Sub
    End If
    
    If ManagerName.Value = "" Then
        MsgBox "Please enter the Authorizing Manager's Name.", vbExclamation, "Input Data"
        ManagerName.SetFocus
        Exit Sub
    End If

    Me.Hide

    Cost_Center = CostCenter.Value
    Auth_Date1 = AuthDate1.Value
    Auth_Date2 = AuthDate2.Value
    Emp_Name = EmpName.Value
    Emp_Num = EmpNum.Value
    Man_Name = ManagerName.Value

'   Creates new document to save as the Employee's Authorization Form
    fileName = EmpName.Value & "_LaborAuthorizationForm"
    Documents.Add "LaborAuthorizationForm.docm"
    With Dialogs(wdDialogFileSaveAs)
        .Name = fileName
        .Show
    End With

End Sub

Private Sub cmdCancel_Click()
    Me.Hide
End Sub
Reply With Quote
  #4  
Old 02-10-2017, 01:38 PM
gmaxey gmaxey is online now Create a ComboBox in a Form that creates a new document and autofills a textBox Windows 7 32bit Create a ComboBox in a Form that creates a new document and autofills a textBox Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
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
  #5  
Old 02-13-2017, 05:51 AM
BlackGirlMagic's Avatar
BlackGirlMagic BlackGirlMagic is offline Create a ComboBox in a Form that creates a new document and autofills a textBox Windows 7 64bit Create a ComboBox in a Form that creates a new document and autofills a textBox Office 2010 64bit
Novice
Create a ComboBox in a Form that creates a new document and autofills a textBox
 
Join Date: Feb 2017
Location: Washington, DC
Posts: 3
BlackGirlMagic is on a distinguished road
Post

So if I wanted the new document to have textboxes that are filled with the employee name, number, etc. from the previous document how would that look? Would I have to have the textboxes already in the new document (template?) or would the old document create the textbox when it creates the new document?
Reply With Quote
  #6  
Old 02-13-2017, 02:39 PM
gmaxey gmaxey is online now Create a ComboBox in a Form that creates a new document and autofills a textBox Windows 7 32bit Create a ComboBox in a Form that creates a new document and autofills a textBox Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Yes you need to put targets (bookmarks, table cells, content controls) in your target document template. Lets say you rename LaborAuthorizationForm.docm as LaborAuthorizationForm.dotm and in it you have a content control titled "Employee Name"

Replace MsgBox EmpNameList.List(lngIndex, 0)

oDoc.SelectionContentControlsByTitle("Employee Name").Item(1).Range.Text = EmpNameList.List(lngIndex, 0)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
combo box, forms vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a ComboBox in a Form that creates a new document and autofills a textBox A form for my own use which creates a document? CactusWren Word 5 08-31-2015 11:22 AM
how to save contents in textbox in VB2013 form into a Word document? saltlakebuffalo Word VBA 6 12-10-2014 06:12 PM
Create a ComboBox in a Form that creates a new document and autofills a textBox Outlook 2007 Code For Matching Textbox to a Combobox in a Different Form lms Outlook 4 07-03-2013 08:34 AM
Create a ComboBox in a Form that creates a new document and autofills a textBox how to populate textbox based on combobox selection in word IvanGeorgiev Word VBA 1 02-21-2013 07:28 PM
Textbox updating from combobox selection paxile2k Word VBA 0 10-26-2010 02:30 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft