Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-14-2018, 11:36 AM
thuriel thuriel is offline fill in a drop-down list with filenames Windows 10 fill in a drop-down list with filenames Office 2016
Novice
fill in a drop-down list with filenames
 
Join Date: Oct 2018
Location: Buenos Aires, Argentina
Posts: 7
thuriel is on a distinguished road
Default fill in a drop-down list with filenames

Good afternoon, I would like to know if it is possible or not, how can you fill in a drop-down list with the name of the files in a folder?




Sorry for my English

Thnx
Reply With Quote
  #2  
Old 11-14-2018, 02:36 PM
macropod's Avatar
macropod macropod is offline fill in a drop-down list with filenames Windows 7 64bit fill in a drop-down list with filenames 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

Is there a reason for not using a file open dialogue?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-14-2018, 03:33 PM
thuriel thuriel is offline fill in a drop-down list with filenames Windows 10 fill in a drop-down list with filenames Office 2019
Novice
fill in a drop-down list with filenames
 
Join Date: Oct 2018
Location: Buenos Aires, Argentina
Posts: 7
thuriel is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Is there a reason for not using a file open dialogue?

I wanted to make a dynamic list to be able to add more files in the folder and to be added to the list, I enclose my test file .


I apologize for not knowing word vba, just beginning and they are my first macros
also for my english, I only speak spanish
Attached Files
File Type: zip test.zip (58.2 KB, 9 views)
Reply With Quote
  #4  
Old 11-14-2018, 03:54 PM
macropod's Avatar
macropod macropod is offline fill in a drop-down list with filenames Windows 7 64bit fill in a drop-down list with filenames 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

A dropdown list in Word is less dynamic than a file open dialogue, since you would need to repopulate the list every time you access it to ensure it remains current. Also, adding more files to the list wouldn't add them to the folder. To populate and keep refreshed your content control with such a list you might use code like the following in your document's 'ThisDocument' code module:
Code:
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String
strDocNm = ActiveDocument.FullName
strFolder = ActiveDocument.Path
strFile = Dir(strFolder & "\*.doc", vbNormal)
With CCtrl
  .DropdownListEntries.Clear
  .Type = wdContentControlText
  .Range.Text = ""
  .Type = wdContentControlDropdownList
  While strFile <> ""
    If strFolder & "\" & strFile <> strDocNm Then
      .DropdownListEntries.Add strFile
    End If
    strFile = Dir()
  Wend
End With
Application.ScreenUpdating = True
End Sub
The above code populates the content control with a list of documents in found in the same folder as the document containing your content control.

To retain the previous selection, omit:
Code:
  .Type = wdContentControlText
  .Range.Text = ""
  .Type = wdContentControlDropdownList
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-14-2018, 04:11 PM
thuriel thuriel is offline fill in a drop-down list with filenames Windows 10 fill in a drop-down list with filenames Office 2019
Novice
fill in a drop-down list with filenames
 
Join Date: Oct 2018
Location: Buenos Aires, Argentina
Posts: 7
thuriel is on a distinguished road
Default

thank you!!! It is functional, my idea was to add several lists for example list1, list2, list3 to the document that's why they were several folders, their method works now I just have to see the logic to be able to apply it to the different folders and files.
Reply With Quote
  #6  
Old 11-14-2018, 04:14 PM
macropod's Avatar
macropod macropod is offline fill in a drop-down list with filenames Windows 7 64bit fill in a drop-down list with filenames 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 implementation would depend on whether you want a separate content control for each list, or all lists in the same content control.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 11-14-2018, 04:16 PM
thuriel thuriel is offline fill in a drop-down list with filenames Windows 10 fill in a drop-down list with filenames Office 2019
Novice
fill in a drop-down list with filenames
 
Join Date: Oct 2018
Location: Buenos Aires, Argentina
Posts: 7
thuriel is on a distinguished road
Default

change the method if it is separately?
Reply With Quote
  #8  
Old 11-14-2018, 04:23 PM
macropod's Avatar
macropod macropod is offline fill in a drop-down list with filenames Windows 7 64bit fill in a drop-down list with filenames 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

In that case, you would need to test which content control was being used. That is easiest to do if you give each content control a title.

PS: He visitado Argentina cuatro veces (Bariloche, Calafate, El Chaltén), para caminar por los parques nacionales.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 11-14-2018, 04:29 PM
thuriel thuriel is offline fill in a drop-down list with filenames Windows 10 fill in a drop-down list with filenames Office 2019
Novice
fill in a drop-down list with filenames
 
Join Date: Oct 2018
Location: Buenos Aires, Argentina
Posts: 7
thuriel is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
In that case, you would need to test which content control was being used. That is easiest to do if you give each content control a title.

PS: He visitado Argentina cuatro veces (Bariloche, Calafate, El Chaltén), para caminar por los parques nacionales.
Muchísimas gracias por toda tu ayuda! si vienes de nuevo a argentina no te pierdas de las cataratas del iguazú

Intentare tu metodo ya que realmente era justo lo que necesitaba.


Thank you very much for all your help! If you come back to Argentina do not miss the Iguazu Falls

I will try your method since it really was just what I needed.

Un gran abrazo!
Reply With Quote
  #10  
Old 11-14-2018, 04:50 PM
macropod's Avatar
macropod macropod is offline fill in a drop-down list with filenames Windows 7 64bit fill in a drop-down list with filenames 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 thuriel View Post
Thank you very much for all your help! If you come back to Argentina do not miss the Iguazu Falls
I have been there, too, on both sides
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 11-15-2018, 08:27 AM
thuriel thuriel is offline fill in a drop-down list with filenames Windows 10 fill in a drop-down list with filenames Office 2019
Novice
fill in a drop-down list with filenames
 
Join Date: Oct 2018
Location: Buenos Aires, Argentina
Posts: 7
thuriel is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I have been there, too, on both sides

Thank u for helping me, thanks to your example I was able to solve the problem through tags

Code:
Sub list1()
Application.ScreenUpdating = False
Dim strFolder$, strFile$, strDocNm$
strDocNm = ActiveDocument.FullName
strFile = Dir(ActiveDocument.Path & "\list1\*.doc", vbNormal)
Dim oThisdoc As Word.Document
Dim oCC As ContentControl
Dim oCCs As ContentControls

Set oThisdoc = ActiveDocument
Set oCCs = oThisdoc.SelectContentControlsByTag("List1")

    For Each oCC In oCCs
    If oCCs.Count > 0 Then
With oCC
  .DropdownListEntries.Clear
  .Type = wdContentControlText
  .Range.Text = ""
  .Type = wdContentControlDropdownList
  While strFile <> ""
    If strFolder & "\" & strFile <> strDocNm Then
      .DropdownListEntries.Add strFile
    End If
    strFile = Dir()
  Wend
End With
Application.ScreenUpdating = True
End If
Next
End Sub

Sub list2()
Application.ScreenUpdating = False
Dim strFolder$, strFile$, strDocNm$
strDocNm = ActiveDocument.FullName
strFile = Dir(ActiveDocument.Path & "\list2\*.doc", vbNormal)
Dim oThisdoc As Word.Document
Dim oCC As ContentControl
Dim oCCs As ContentControls

Set oThisdoc = ActiveDocument
Set oCCs = oThisdoc.SelectContentControlsByTag("List2")

    For Each oCC In oCCs
    If oCCs.Count > 0 Then
With oCC
  .DropdownListEntries.Clear
  .Type = wdContentControlText
  .Range.Text = ""
  .Type = wdContentControlDropdownList
  While strFile <> ""
    If strFolder & "\" & strFile <> strDocNm Then
      .DropdownListEntries.Add strFile
    End If
    strFile = Dir()
  Wend
End With
Application.ScreenUpdating = True
End If
Next
End Sub
It has been managed to make the most dynamic and still complete the text by means of what is selected in the drop-down list through

Code:
Sub list_to_text()
Dim oThisdoc As Word.Document
Dim oCC As ContentControl
Dim oCCs As ContentControls
Dim sText As String

Set oThisdoc = ActiveDocument
Set oCCs = oThisdoc.SelectContentControlsByTag("List1")



Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
    For Each oCC In oCCs
    If oCCs.Count > 0 Then
    oCC.Range.Select
    oCC.Delete False
    sText = Application.Selection.Text
    End If
Next

                ChangeFileOpenDirectory ActiveDocument.Path & "\List1\"
            Selection.InsertFile FileName:=sText, Range:="", ConfirmConversions:= _
        False, Link:=False, Attachment:=False
            Selection.TypeBackspace
            Selection.MoveRight Unit:=wdCharacter, Count:=1
  
  
End Sub

Last edited by thuriel; 11-15-2018 at 01:53 PM.
Reply With Quote
  #12  
Old 11-15-2018, 02:18 PM
macropod's Avatar
macropod macropod is offline fill in a drop-down list with filenames Windows 7 64bit fill in a drop-down list with filenames 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

Try:
Code:
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
With CCtrl
  .DropdownListEntries.Clear
  .Type = wdContentControlText
  .Range.Text = ""
  .Type = wdContentControlDropdownList
  Select Case .Tag
    Case "List1": strFolder = ActiveDocument.Path & "\list1\"
    Case "List2": strFolder = ActiveDocument.Path & "\list2\"
    Case "List3": strFolder = ActiveDocument.Path & "\list3\"
  End Select
  strFile = Dir(strFolder & "*.doc", vbNormal)
  While strFile <> ""
    .DropdownListEntries.Add strFile
    strFile = Dir()
  Wend
End With
Application.ScreenUpdating = True
End Sub
Your can add/delete dropdowns and their list folders, as desired.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 11-15-2018, 02:35 PM
thuriel thuriel is offline fill in a drop-down list with filenames Windows 10 fill in a drop-down list with filenames Office 2019
Novice
fill in a drop-down list with filenames
 
Join Date: Oct 2018
Location: Buenos Aires, Argentina
Posts: 7
thuriel is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Try:
Code:
Private Sub Document_ContentControlOnEnter(ByVal CCtrl As ContentControl)
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
With CCtrl
  .DropdownListEntries.Clear
  .Type = wdContentControlText
  .Range.Text = ""
  .Type = wdContentControlDropdownList
   Case .Tag
    Case "List1": strFolder = ActiveDocument.Path & "\list1\"
    Case "List2": strFolder = ActiveDocument.Path & "\list2\"
    Case "List3": strFolder = ActiveDocument.Path & "\list3\"
  End 
  strFile = Dir(strFolder & "*.doc", vbNormal)
  While strFile <> ""
    .DropdownListEntries.Add strFile
    strFile = Dir()
  Wend
End With
Application.ScreenUpdating = True
End Sub
Your can add/delete dropdowns and their list folders, as desired.

you're the best!! This is very simplifying.
Reply With Quote
Reply

Tags
drop-down list



Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop down box list based on response to another drop down box Phideaux Excel 16 04-13-2018 03:07 AM
Document with drop down field for attorney. How to fill in the address etc in other fields. ElfegoBaca Word 3 09-27-2017 09:00 AM
fill in a drop-down list with filenames How to get a Drop Down List Content Control box to fill in other areas snips1982 Word 2 03-22-2017 03:37 AM
Fill - White, Drop Shadow formatting in PP 2013 dave3point0 PowerPoint 2 12-28-2015 04:23 PM
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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:27 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