Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 07-07-2017, 10:16 AM
gmaxey gmaxey is offline Word Template with a drop down list to populate other fields Windows 7 32bit Word Template with a drop down list to populate other fields 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 are going have to roll up your sleeves and write an awful lot of code or tie your content control dropdown list to a data base.

I really don't see how "Clients Name" is associated with a selected provider but if you have a Excel files "Providers.xlsx" in the same folder as your template then something like this:

Code:
Option Explicit
Dim arrData As Variant
Private Sub Document_ContentControlOnEnter(ByVal oCC As ContentControl)
Dim lngIndex As Long
Dim strSQL As String
  Select Case oCC.Title
    Case "Provider"
      oCC.DropdownListEntries.Clear
      strSQL = "SELECT * FROM [Sheet1$];"
      xlFillList arrData, ThisDocument.Path & "\Providers.xlsx", "True", strSQL
      For lngIndex = 0 To UBound(arrData, 2)
        oCC.DropdownListEntries.Add arrData(0, lngIndex), arrData(0, lngIndex)
      Next lngIndex
  End Select
End Sub
Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean)
Dim lngIndex As Long
  Select Case oCC.Title
    Case "Provider"
      If Not oCC.ShowingPlaceholderText Then
        ActiveDocument.SelectContentControlsByTitle("Provider Address").Item(1).Range.Text = arrData(1, fcnDropDownIndex(oCC))
        ActiveDocument.SelectContentControlsByTitle("Client Name").Item(1).Range.Text = arrData(2, fcnDropDownIndex(oCC))
      Else
        ActiveDocument.SelectContentControlsByTitle("Provider Address").Item(1).Range.Text = vbNullString
        ActiveDocument.SelectContentControlsByTitle("Client Name").Item(1).Range.Text = vbNullString
      End If
  End Select
End Sub
Function fcnDropDownIndex(CC As ContentControl) As Long
Dim lngIndex As Long
  For lngIndex = 1 To CC.DropdownListEntries.Count
    If CC.DropdownListEntries.Item(lngIndex).Text = CC.Range.Text Then
      fcnDropDownIndex = lngIndex - 1
      Exit For
    End If
  Next
lbl_Exit:
  Exit Function
End Function
Public Function xlFillList(arrPassed As Variant, strWorkbook As String, _
                           bSuppressHeader As Boolean, strSQL As String)
Dim oConn As Object
Dim oRS As Object
Dim lngNumRecs As Long
Dim strConnection As String
  'Create connection:
  Set oConn = CreateObject("ADODB.Connection")
  If bSuppressHeader Then
   strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "Data Source=" & strWorkbook & ";" & _
                    "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
  Else
   strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "Data Source=" & strWorkbook & ";" & _
                    "Extended Properties=""Excel 12.0 Xml;HDR=NO"";"
  End If
  oConn.Open ConnectionString:=strConnection
  Set oRS = CreateObject("ADODB.Recordset")
  'Read the data from the worksheet.
  oRS.Open strSQL, oConn, 3, 1 '3: adOpenStatic, 1: adLockReadOnly
  With oRS
    'Find the last record.
    .MoveLast
    'Get count.
    lngNumRecs = .RecordCount
    'Return to the start.
    .MoveFirst
  End With
  arrPassed = oRS.GetRows(lngNumRecs)
  'Cleanup
  If oRS.State = 1 Then oRS.Close
  Set oRS = Nothing
  If oConn.State = 1 Then oConn.Close
  Set oConn = Nothing
lbl_Exit:
  Exit Function
End Function
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA coding for multiple drop-down lists to populate a text box for each list yeatropulo Word VBA 14 11-11-2015 01:08 PM
Error 5941 when running my macro to auto populate fields throughout the word doc VBAnovice1 Word VBA 2 05-26-2015 01:35 AM
Populate Word Drop-down list with Excel column then auto fill form fields with Excel data Faldinio Word VBA 7 10-19-2014 06:03 AM
Word Template with a drop down list to populate other fields how do I auto-populate a word template Greenhorn Word 4 09-05-2012 04:16 PM
Word Template with a drop down list to populate other fields MSWord forms use list to populate successive text fields 2surgeons Word 4 03-05-2012 07:19 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:47 PM.


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