Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 10-25-2017, 11:58 PM
kinimv kinimv is offline How to import list from Excel into drop-down list into word Windows 10 How to import list from Excel into drop-down list into word Office 2016
Novice
 
Join Date: Oct 2017
Location: Seattle, WA
Posts: 9
kinimv is on a distinguished road
Default


Thanks for that macropod.
As I read your post I realized what I was trying to do was nonsensical.
The need I have is to have multiple lines dependent on each dropdown option. The Value field has a character limit, and my text exceeds that limit for every dropdown option.
Reply With Quote
  #17  
Old 10-26-2017, 12:57 AM
macropod's Avatar
macropod macropod is offline How to import list from Excel into drop-down list into word Windows 7 64bit How to import list from Excel into drop-down list into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For a workaround that stores the conditional output in document variables, see:
https://www.msofficeforums.com/word-...tml#post119230
The source workbooks are earlier in the thread. Although (in the case of the link) secondary dropdowns are used, that's not to say you'd have to take the same approach.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #18  
Old 10-26-2017, 01:16 AM
kinimv kinimv is offline How to import list from Excel into drop-down list into word Windows 10 How to import list from Excel into drop-down list into word Office 2016
Novice
 
Join Date: Oct 2017
Location: Seattle, WA
Posts: 9
kinimv is on a distinguished road
Default

Hmm macropod, that looks like a very detailed document. I will take some time to study it and adapt it to my needs. Thanks a ton for your help!
Reply With Quote
  #19  
Old 10-26-2017, 01:25 AM
macropod's Avatar
macropod macropod is offline How to import list from Excel into drop-down list into word Windows 7 64bit How to import list from Excel into drop-down list into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The important thing about what's in the link is that it demonstrates how you can use document variables. The method of getting the data into and out of it/them needn't necessarily be as complex as it was for that thread, where data from multiple workbooks and worksheets were being collated.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #20  
Old 11-22-2017, 08:27 PM
kinimv kinimv is offline How to import list from Excel into drop-down list into word Windows 10 How to import list from Excel into drop-down list into word Office 2016
Novice
 
Join Date: Oct 2017
Location: Seattle, WA
Posts: 9
kinimv is on a distinguished road
Default

Hey macropod, I successfully found a way around the character limit by storing the excel columns as arrays.

However, in other parts of my document, I am still using value to fill in multiple fields from the dropdown. Which brings me to my question: In your first post in this thread, your code included the following:

Code:
 With xlWkBk
      With .Worksheets(StrWkShtNm)
        ' Find the last-used row in column A.
        LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        ' Populate the content control titled 'ID', with Column A for the 'ID' as the
        ' content control Text and the values from columns B-E as the content control
        ' value, using a "|" separator
        ActiveDocument.SelectContentControlsByTitle("ID")(1).DropdownListEntries.Clear
        For i = 1 To LRow
          ActiveDocument.SelectContentControlsByTitle("ID")(1).DropdownListEntries.Add _
            Text:=Trim(.Range("A" & i))
          'or, for example, to add the contents of column B to the content control's 'value':
          'ActiveDocument.SelectContentControlsByTitle("ID")(1).DropdownListEntries.Add _
            Text:=Trim(.Range("A" & i)), Value:=Trim(.Range("B" & i))
        Next
      End With
    .Close False
  End With
I can get the contents of column B to be my value, but I cannot see how the value can be from B-E columns using a "|" separator. Is that part missing from your code, or am I missing something?
Thanks
Reply With Quote
  #21  
Old 11-22-2017, 08:29 PM
macropod's Avatar
macropod macropod is offline How to import list from Excel into drop-down list into word Windows 7 64bit How to import list from Excel into drop-down list into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Simply use:
Value:=Trim(.Range("B" & i)) & "|" & Trim(.Range("C" & i)) & "|" & Trim(.Range("D" & i)) & "|" & Trim(.Range("E" & i))
Do be aware, though, that the Value property also has limited capacity.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #22  
Old 11-22-2017, 08:35 PM
kinimv kinimv is offline How to import list from Excel into drop-down list into word Windows 10 How to import list from Excel into drop-down list into word Office 2016
Novice
 
Join Date: Oct 2017
Location: Seattle, WA
Posts: 9
kinimv is on a distinguished road
Default

Ah, that explains it. Thanks!
Reply With Quote
  #23  
Old 11-23-2017, 07:11 AM
gmaxey gmaxey is offline How to import list from Excel into drop-down list into word Windows 7 32bit How to import list from Excel into drop-down list into word Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
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

Paul,

It has been awhile since I have been in this thread and I notice that it gets a lot of visits. I hope you won't feel that I am trying to upstage you in any way, but I thought that I would provide and alternate method that doesn't require the reference to the Excel object library or physically opening the Excel file (with the Excel app). In this method the data is gathered using ADODB in an array then the array is used in various ways to file the CC dropdown list:

Code:
Option Explicit
Sub Document_Open()
Dim strWorkbook As String, strColumns As String
Dim lngRowIndex As Long, lngColIndex As Long
Dim arrData As Variant
Dim oCC As ContentControl
  Application.ScreenUpdating = False
  strWorkbook = "D:\Data Stores\Populate Array from Data.xlsx"
  If Dir(strWorkbook) = "" Then
    MsgBox "Cannot find the designated workbook: " & strWorkbook, vbExclamation
    Exit Sub
  End If
  Set oCC = ActiveDocument.SelectContentControlsByTitle("ID").Item(1)
  arrData = fcnExcelDataToArray(strWorkbook, , , False)
  oCC.DropdownListEntries.Clear
  For lngRowIndex = 0 To UBound(arrData, 2)
    'Examples:
    '1. Populate the dropdown list text and value property using data from column 1
    oCC.DropdownListEntries.Add arrData(0, lngRowIndex), arrData(0, lngRowIndex)
    '2. Populate the dropdown list text property using data from column 1 _
        and the value property using data from column 2
    'oCC.DropdownListEntries.Add arrData(0, lngRowIndex), arrData(1, lngRowIndex)
    '3. Populate the dropdown list text property using data from column 1 _
    '   and the value property using concanated data from all remaining columns.
    'strColumns = vbNullString
    'For lngColIndex = 1 To UBound(arrData, 1)
    '  strColumns = strColumns & "|" & arrData(lngColIndex, lngRowIndex)
    'Next lngColIndex
    'strColumns = Right(strColumns, Len(strColumns) - 1)
    'oCC.DropdownListEntries.Add arrData(0, lngRowIndex), strColumns
  Next
lbl_Exit:
  Application.ScreenUpdating = True
  Exit Sub
End Sub
Private Function fcnExcelDataToArray(strWorkbook As String, _
                                     Optional strRange As String = "Sheet1", _
                                     Optional bIsSheet As Boolean = True, _
                                     Optional bHeaderRow As Boolean = True) As Variant
Dim oRS As Object, oConn As Object
Dim lngRows As Long
Dim strHeaderYES_NO As String
  strHeaderYES_NO = "YES"
  If Not bHeaderRow Then strHeaderYES_NO = "NO"
  If bIsSheet Then strRange = strRange & "$]" Else strRange = strRange & "]"
  Set oConn = CreateObject("ADODB.Connection")
  oConn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & strWorkbook & ";" & _
        "Extended Properties=""Excel 12.0 Xml;HDR=" & strHeaderYES_NO & """;"
  Set oRS = CreateObject("ADODB.Recordset")
  oRS.Open "SELECT * FROM [" & strRange, oConn, 2, 1
  With oRS
    .MoveLast
    lngRows = .RecordCount
    .MoveFirst
  End With
  fcnExcelDataToArray = oRS.GetRows(lngRows)
lbl_Exit:
  If oRS.State = 1 Then oRS.Close
  Set oRS = Nothing
  If oConn.State = 1 Then oConn.Close
  Set oConn = Nothing
  Exit Function
End Function
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #24  
Old 11-23-2017, 01:03 PM
macropod's Avatar
macropod macropod is offline How to import list from Excel into drop-down list into word Windows 7 64bit How to import list from Excel into drop-down list into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
I hope you won't feel that I am trying to upstage you in any way, but I thought that I would provide and alternate method that doesn't require the reference to the Excel object library or physically opening the Excel file (with the Excel app). In this method the data is gathered using ADODB in an array then the array is used in various ways to file the CC dropdown list:
Hi Greg,

Your input is always welcome. I've never studied ADODB in any detail, though I am aware of its advantages (higher performance and no need for an Excel reference - though the latter could be avoided though late binding [=yet lower performance]). I probably should study it in more detail; it's yet another tool for users to work with.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #25  
Old 11-26-2017, 07:40 AM
gmaxey gmaxey is offline How to import list from Excel into drop-down list into word Windows 7 32bit How to import list from Excel into drop-down list into word Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
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

Paul,
I've wrapped up few examples using the ADODB method here:
https://gregmaxey.com/word_tip_pages...pdownlist.html
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #26  
Old 11-26-2017, 02:16 PM
macropod's Avatar
macropod macropod is offline How to import list from Excel into drop-down list into word Windows 7 64bit How to import list from Excel into drop-down list into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Thanks,

I'll take a look. You might find me incorporating ADODB into some of my posts...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #27  
Old 02-27-2018, 11:02 AM
garfieldsth garfieldsth is offline How to import list from Excel into drop-down list into word Windows 10 How to import list from Excel into drop-down list into word Office 2013
Novice
 
Join Date: Feb 2018
Posts: 1
garfieldsth is on a distinguished road
Default

I know this is an older thread but I thought I'd ask for some direction anyway.
I tried the code example and I received a compile error: User-defined type not defined. When I debugged, the error was with this line:

Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook
Reply With Quote
  #28  
Old 02-27-2018, 01:48 PM
macropod's Avatar
macropod macropod is offline How to import list from Excel into drop-down list into word Windows 7 64bit How to import list from Excel into drop-down list into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Did you read the comment line in the code immediately before that?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #29  
Old 07-23-2018, 12:57 AM
Guimenez Guimenez is offline How to import list from Excel into drop-down list into word Windows 10 How to import list from Excel into drop-down list into word Office 2013
Advanced Beginner
 
Join Date: Jan 2017
Posts: 33
Guimenez is on a distinguished road
Default

Hi,
i'm using this code for a while and it worked flawless.
Now when i open word and run the code i'm getting this error: "method open of object workbooks failed".
Does anyone know what can i do?
Thank you
Reply With Quote
  #30  
Old 07-23-2018, 06:03 AM
macropod's Avatar
macropod macropod is offline How to import list from Excel into drop-down list into word Windows 7 64bit How to import list from Excel into drop-down list into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That suggests you've moved and/or renamed the workbook.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
lesson plan template, teacher help



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to import list from Excel into drop-down list into word Dynamically changing drop-down list based on selection? (Word Form) laurarem Word 1 02-21-2013 10:17 PM
How to import list from Excel into drop-down list into word How to import a list of names into PowerPoint from Excel? CarpetRemnant PowerPoint 4 12-07-2012 11:08 AM
Drop down list, Can it be done??? garethreid Outlook 0 08-09-2012 06:08 AM
How to import list from Excel into drop-down list into word drop down list from external data source Excel 2007? Intruder Excel 1 08-03-2012 05:41 AM
Long List for drop down box DLo99 Word 0 02-28-2010 08:07 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:52 PM.


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