从 Word 表格单元格中的下拉列表访问值

问题描述 投票:0回答:1

我有一个 Word 文档,用于提供用户的调查回复。然后他们将word文档发回给我。该文档包含许多 2 列表。在每一行中,第 1 列包含文本(问题),第 2 列包含一个下拉列表。我的代码可以轻松访问所选响应的文本。但我试图获取与创建表时已编码到下拉列表项中的所选响应相关的值。

我的问题似乎是,当我浏览表格的每一行时,我无法引用单元格中的下拉列表。

救命!

    Private Sub CommandButton1_Click()

Dim Tb1, Tb2 As Word.Table
Dim irow, icol, i, j, Tabnum As Integer
Dim Strng As String
Dim RowCount, Responsevalue As Long
Dim File1 As Document
Dim rngDoc As Range
Dim ccMyContentControl As ContentControl



 
'
'
'
'First Open Survey Data File


'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in fullpath variable
        With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
        .AllowMultiSelect = False
        'Filter to just the following types of files to narrow down selection options
        .Filters.Add "Word Files", "*.docx; *.doc; *.docm", 1
        'Show the dialog box
        .Show
        
        'Store in SurveyData variable
        SurveyData = .SelectedItems.Item(1)
        
        Documents.Open SurveyData
        Documents(SurveyData).Activate
        Tabnum = ActiveDocument.Tables.Count
        
        For i = 1 To Tabnum
                   
' Skip Header Row

            For irow = 2 To ActiveDocument.Tables(i).Rows.Count
                         
                Strng = ActiveDocument.Tables(i).Cell(irow, 2).Range.Text
             '    Responsevalue = ActiveDocument.Tables(i).Cell(irow, 2).ContentControl.Range.Result
                Let ccMyContentControl = ActiveDocument.Tables(i).Cell(irow, 2).wdContentControlDropdownList
                With ccMyContentConrol
                For j = 1 To .DropdownListEntries.Count
                   If .DropdownListEntries(j).Text = Strng Then _
                       Responsevalue = .DropdownListEntries(j).Value
                   Else
                       Responsevalue = 99
                    MsgBox Responsevalue
                'Next j
                'End With

               
                 
            Next irow
            
        Next i
       
                        
        
        End With
    
'
' Close Survey Response file
'

        
     




    
    
    
End Sub
ms-word contentcontrol
1个回答
0
投票

正如我在评论中简要提到的,您的代码有很多问题。尝试看看下面的代码是否适合您。

Private Sub CommandButton1_Click()
    
    Dim SurveyData
    
    
    Dim Tb1, Tb2 As word.Table
    Dim irow, icol, i, j, Tabnum As Integer
    Dim Strng As String
    Dim RowCount, Responsevalue As Long
    Dim File1 As Document
    Dim rngDoc As Range
    Dim ccMyContentControl As ContentControl
    '
    '
    'First Open Survey Data File
    
    
    'Display a Dialog Box that allows to select a single file.
    'The path for the file picked will be stored in fullpath variable
    With Application.FileDialog(msoFileDialogFilePicker)
        'Makes sure the user can select only one file
        .AllowMultiSelect = False
        'Filter to just the following types of files to narrow down selection options
        .Filters.Add "Word Files", "*.docx; *.doc; *.docm", 1
        'Show the dialog box
        .Show
        
        'Store in SurveyData variable
        SurveyData = .SelectedItems.Item(1)
        
        Rem These 3 lines should be revised as follows will be better
        Documents.Open SurveyData
        Documents(SurveyData).Activate
        Tabnum = ActiveDocument.Tables.Count
'        Dim d As Document
'        Set d = Documents.Open(SurveyData)
'        d.Activate
'        Tabnum = d.Tables.Count
        
        
        For i = 1 To Tabnum
                   
            ' Skip Header Row

            For irow = 2 To ActiveDocument.Tables(i).Rows.Count
                         
                Strng = ActiveDocument.Tables(i).Cell(irow, 2).Range.Text
                
                Rem Did you stuck here? You have to use `Set` to refer to an object!
                Rem "Use the Set statement to assign object references to variables." ' https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/let-statement#:~:text=user%2Ddefined%20types.-,Use%20the%20Set%20statement%20to%20assign%20object%20references%20to%20variables.,-Example
             '    Responsevalue = ActiveDocument.Tables(i).Cell(irow, 2).ContentControl.Range.Result
                'Let ccMyContentControl = ActiveDocument.Tables(i).Cell(irow, 2).wdContentControlDropdownList
                
                Set ccMyContentControl = ActiveDocument.Tables(i).Cell(irow, 2).Range.ContentControls(1) ' You have to use Index or Name to invovk the item in a collection like `ContentControls`. Use 1 if there is only one ContentControl in the cell
                
                Rem Wrong spelling.
                'With ccMyContentConrol
                With ccMyContentControl 'Must be! Watch your spelling. You can use `Option Explicit` at the top of this module to prevent any wrong spelling.
                
                For j = 1 To .DropdownListEntries.Count
                
                    Rem You've used `Else`, then can not have the "_" !
                   'If .DropdownListEntries(j).Text = Strng Then _

                   Rem it Should be as the follow
                   'If .DropdownListEntries(j).Text = Strng Then
                   If .DropdownListEntries(j).Text & Chr(7) = Strng Then

                   
                       Responsevalue = .DropdownListEntries(j).Value
                   Else
                       Responsevalue = 99
                       MsgBox Responsevalue
                    
                    Rem You lost this line
                   End If
                Next j
                End With
                 
            Next irow
            
        Next i
       
    End With
    
'
' Close Survey Response file
'
    
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.