Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-03-2019, 09:10 AM
Wire.Lless Wire.Lless is offline Scan all existing listBoxes in a document and retrieve the selected value. Windows 8 Scan all existing listBoxes in a document and retrieve the selected value. Office 2010
Novice
Scan all existing listBoxes in a document and retrieve the selected value.
 
Join Date: Apr 2019
Posts: 2
Wire.Lless is on a distinguished road
Default Scan all existing listBoxes in a document and retrieve the selected value.

Hello.


I have a number of MS Word documents that all have the same layout.
I wish:
1- open each document
2- browse through all the listboxes in each document
3-For each listbox:
I want to retrieve the value that is selected
For the moment I have managed to program the first part: Loop on several Word documents:
Code:
Public wordApp As New Word.Application
Public Sub Ouvrir_Tout_Les_Fichiers()
Dim Fichier_Objet As String
 
titre = "Ouvrir le(s) fichier(s)"
Filt = "Fichier(s) a integrer (*.docm;*.doc;*.docx),*.docm;*.doc;*.docx"
fileToOpen = Application.GetOpenFilename(FileFilter:=Filt, Title:=titre, MultiSelect:=True)
 
For I = LBound(fileToOpen) To UBound(fileToOpen)
 
    FilePath = Left(fileToOpen(I), InStrRev(fileToOpen(I), "\"))
    Fichier_Objet = Mid(fileToOpen(I), InStrRev(fileToOpen(I), "\") + 1)
    Set wordApp = CreateObject("Word.Application")
    wordApp.Documents.Open fileToOpen(I)
    wordApp.Visible = True
 
    File_Name = UBound(fileToOpen)
 
    Call _2browse_through_all_the_listboxes_in_each_document(I, Fichier_Objet)
 
    wordApp.Documents.Close
    wordApp.Quit
 
Next I
 
End Sub
In each document I try to browse all the listboxes!

Apparently the listboxes are "ContentControlListEntry" objects...
https://docs.microsoft.com/fr-fr/off...ntrollistentry
... which are contained in "ContentControl" objects
https://docs.microsoft.com/fr-fr/off...ntent-controls

Code:
Public Sub _2browse_through_all_the_listboxes_in_each_document(I, Fichier_Objet)
Dim objCc As ContentControl
Dim objLe As ContentControlListEntry
 
Debug.Print Fichier_Objet
 
For Each objCc In wordApp.ActiveDocument.ContentControls
   For Each objLe In objCc.DropdownListEntries
        Debug.Print objLe.Text
        Debug.Print objLe.Index
        Debug.Print objLe.Value
        'Debug.Print objLe.Creator
   Next
Next
 
End Sub
I try to browse all the listboxes (objCc.DropdownListEntries) and extract the value that has been selected. But it's impossible to find the right property!
I managed to extract objLe.Text; objLe.Index; objLe.Value ... but I can't find anything that looks like the value that was chosen in the list box. (maybe the visible property = true ... or something like that??)

...
I managed to successfully extract the values of the "checkboxes" and the values of the "user fields"... but I really block on these "listboxes":

Code:
Public Sub Extract_user_fields_and_checkbox(I, Fichier_Objet)
Dim ch As Field
Dim Ctrl As ContentControl
 
Debug.Print Fichier_Objet
 
For Each ch In wordApp.ActiveDocument.Fields
   If ch.Type = "70" Then
      Debug.Print "Index ", ch.Index
      Debug.Print "Result ", ch.Result
      Debug.Print "Type ", ch.Type
   End If
Next ch
 
For Each Ctrl In wordApp.ActiveDocument.ContentControls
   If Ctrl.Type = "8" Then
      If Ctrl.Checked = True Then
         Debug.Print Ctrl.Tag
         Debug.Print Ctrl.ID
         Debug.Print "True"
      Else
         Debug.Print Ctrl.Tag
         Debug.Print Ctrl.ID
         Debug.Print "False"
 
      End If
   End If
Next
End Sub
Does anyone have any idea how to proceed?
Thank you very much
Reply With Quote
  #2  
Old 04-03-2019, 10:27 AM
Wire.Lless Wire.Lless is offline Scan all existing listBoxes in a document and retrieve the selected value. Windows 8 Scan all existing listBoxes in a document and retrieve the selected value. Office 2010
Novice
Scan all existing listBoxes in a document and retrieve the selected value.
 
Join Date: Apr 2019
Posts: 2
Wire.Lless is on a distinguished road
Default

Well, I don't think I'm that far from the solution because the following code can display the contents of my listbox!
(Then, I'm not sure I'm using the right vocabulary... but apparently they're ContentControlListEntry since I can display each item in my "list")

Code:
Public Sub _2_browse_through_all_the_listboxes_in_each_document(I, Fichier_Objet)
Dim objCc As ContentControl
Dim objLe As ContentControlListEntry
 
Debug.Print Fichier_Objet
 
For Each objCc In wordApp.ActiveDocument.ContentControls
   For Each objLe In objCc.DropdownListEntries
        Debug.Print objLe.Text
        Debug.Print objLe.Index
        Debug.Print objLe.Value
        'Debug.Print objLe.Creator
   Next
Next
 
End Sub
Reply With Quote
  #3  
Old 04-03-2019, 02:06 PM
macropod's Avatar
macropod macropod is offline Scan all existing listBoxes in a document and retrieve the selected value. Windows 7 64bit Scan all existing listBoxes in a document and retrieve the selected value. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
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

Perhaps:
Code:
Sub GetFormData()
'Note: this code requires a reference to the Word object model.
'See under the VBE's Tools|References.
Application.ScreenUpdating = False
Dim wdApp As New Word.Application, wdDoc As Word.Document, CCtrl As Word.ContentControl
Dim strFolder As String, strFile As String, WkSht As Worksheet, r As Long, c As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet: r = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
'Disable any auto macros in the documents being processed
wdApp.WordBasic.DisableAutoMacros
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  r = r + 1
  Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    c = 1: WkSht.Cells(r, c) = Split(strFile, ".doc")(0)
    For Each CCtrl In .ContentControls
      With CCtrl
        Select Case .Type
          Case wdContentControlDropdownList, wdContentControlComboBox
            c = c + 1
            If IsNumeric(.Range.Text) Then
              If Len(.Range.Text) > 15 Then
                WkSht.Cells(r, c) = "'" & .Range.Text
              Else
                WkSht.Cells(r, c) = .Range.Text
              End If
            Else
              WkSht.Cells(r, c) = .Range.Text
            End If
          Case Else
        End Select
      End With
    Next
    .Close SaveChanges:=False
  End With
  strFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scan all existing listBoxes in a document and retrieve the selected value. Retrieve a word from another document vestergaard Word 3 02-08-2018 10:30 AM
Scan all existing listBoxes in a document and retrieve the selected value. Retrieve the last activated document name. eduzs Word VBA 1 08-22-2017 03:11 PM
Can i make excel retrieve selected text? peterpiper Excel 1 07-11-2017 03:49 AM
Scan all existing listBoxes in a document and retrieve the selected value. Scan Document into excel mtwa Excel 1 04-21-2016 09:01 PM
Data from hidden internal tables feeding listboxes in same Word Document marksm33 Word VBA 2 02-21-2014 07:10 PM

Other Forums: Access Forums

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