VBA 标签名称循环

VBA loop for label names

我有一个包含 旧式下拉列表 的 Word table。有 11 行,比方说 4 列。每个单元格都包含一个 Legacy DropDown List,这使得每列计数为 11(总共 44 个)。

我必须从这些 Legacy DropDown List 中获取数据并将其放入另一个 table 中的 Labels同一文档的页面。

到目前为止没问题,虽然我不得不把它全部写下来而不是使用循环,但我让它工作了,因为我找不到在我的标签名称中放置变量的方法。

我将我的代码缩短到第一周的第一行(因此有 4 个 标签 ),否则它会太长而且没有必要那样。

当前代码:

Sub Week1()
'Week 1
If ActiveDocument.FormFields("Dom1").DropDown.ListEntries.Count <> 0 And ActiveDocument.FormFields("Dom1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Dom1").DropDown.Value).Name <> "Choose a DOM." Then
    lblDom1W1.Caption = ActiveDocument.FormFields("Dom1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Dom1").DropDown.Value).Name
End If

If ActiveDocument.FormFields("Sit1").DropDown.ListEntries.Count <> 0 Then
    If ActiveDocument.FormFields("Sit1").DropDown.Value <> 0 Then
        If ActiveDocument.FormFields("Sit1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Sit1").DropDown.Value).Name <> "Choose a SIT" Then
            lblSit1W1.Caption = ActiveDocument.FormFields("Sit1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Sit1").DropDown.Value).Name
        End If
    End If
End If

If ActiveDocument.FormFields("Int1").DropDown.ListEntries.Count <> 0 Then
    If ActiveDocument.FormFields("Int1").DropDown.Value <> 0 Then
        lblInt1W1.Caption = ActiveDocument.FormFields("Int1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Int1").DropDown.Value).Name
    End If
End If

 If ActiveDocument.FormFields("Gram1").DropDown.ListEntries.Count <> 0 And ActiveDocument.FormFields("Gram1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Gram1").DropDown.Value).Name <> "Choose a GRAM." Then
    lblGram1W1.Caption = ActiveDocument.FormFields("Gram1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Gram1").DropDown.Value).Name
End If

所以这行得通。话虽这么说,我想循环它,这样我就可以只使用一次这么多代码,而不是让它每周重复 11 次,持续 11 周。

我已经将标签命名为易于使用循环。因此,他们的名字在第一周都是 lblDom1W1lblDom11W1 ,其他标签依此类推(只有最后一位数字更改( e.i. lblDom1W2lblDom11W2)).

此外,我认为并尝试了这些我认为行不通但没有奏效的方法:

我浏览了这个 post 但我不太理解其中的所有内容,我不确定这是否是我需要的,因为它是 VB.NET 而不是 VBA . Post I checked


编辑:

根据用户 R3uK 的评论,我尝试了这个,但它也不起作用。我为我的 leLabelDom.Caption...:

提供了 Invalid qualifier
Sub Week1()
'Week1

Dim labelDom As String
labelDom = "lblDom"

Dim week1 As String
week1 = "W1"

Dim leLabelDom As String

For k = 1 To 11
       leLabelDom = labelDom & k & week1
If ActiveDocument.FormFields("ListeDomaine" & k).DropDown.ListEntries.Count <> 0 And ActiveDocument.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeDomaine" & k).DropDown.Value).Name <> "Choisissez un domaine." Then
           leLabelDom.Caption = ActiveDocument.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeDomaine" & k).DropDown.Value).Name
End If

编辑 2 - 工作:

从用户 R3uK 的回答开始,以这段有效的代码结束。非常感谢你 R3uK !

Sub Remplir()

Dim leLabelDom As String, _
    wDocD As Word.Document, _
    IsHd As InlineShape, _
    leLabelSit As String, _
    leLabelInt As String, _
    leLabelGram As String, _
    semaine As String

Set wDoc = ActiveDocument

If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 1" Then
    semaine = "S1"
    lblMaterielS1.Caption = TextBoxMateriel.Text
    lblEvaluationS1.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 2" Then
    semaine = "S2"
    lblMaterielS2.Caption = TextBoxMateriel.Text
    lblEvaluationS2.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 3" Then
    semaine = "S3"
    lblMaterielS3.Caption = TextBoxMateriel.Text
    lblEvaluationS3.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 4" Then
    semaine = "S4"
    lblMaterielS4.Caption = TextBoxMateriel.Text
    lblEvaluationS4.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 5" Then
    semaine = "S5"
    lblMaterielS5.Caption = TextBoxMateriel.Text
    lblEvaluationS5.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 6" Then
    semaine = "S6"
    lblMaterielS6.Caption = TextBoxMateriel.Text
    lblEvaluationS6.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 7" Then
    semaine = "S7"
    lblMaterielS7.Caption = TextBoxMateriel.Text
    lblEvaluationS7.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 8" Then
    semaine = "S8"
    lblMaterielS8.Caption = TextBoxMateriel.Text
    lblEvaluationS8.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 9" Then
    semaine = "S9"
    lblMaterielS9.Caption = TextBoxMateriel.Text
    lblEvaluationS9.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 10" Then
    semaine = "S10"
    lblMaterielS10.Caption = TextBoxMateriel.Text
    lblEvaluationS10.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 11" Then
    semaine = "S11"
    lblMaterielS11.Caption = TextBoxMateriel.Text
    lblEvaluationS11.Caption = TextBoxEvaluation.Text
End If

For k = 1 To 11
    leLabelDom = "lblDomaine" & k & semaine
    leLabelSit = "lblSituation" & k & semaine
    leLabelInt = "lblIntention" & k & semaine
    leLabelGram = "lblGrammaire" & k & semaine

If wDoc.FormFields("ListeDomaine" & k).DropDown.ListEntries.Count <> 0 And _
    wDoc.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeDomaine" & k).DropDown.Value).Name <> "Choisissez un domaine." _
    Then
        If wDoc.InlineShapes.Count <> 0 Then
            For Each IsH In wDoc.InlineShapes
                If IsH.Type = wdInlineShapeOLEControlObject Then
                    If TypeName(IsH.OLEFormat.Object) = "Label" Then
                        If IsH.OLEFormat.Object.Name = leLabelDom Then
                            IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeDomaine" & k).DropDown.Value).Name
                        End If
                    End If
                End If
            Next
        End If
End If

If wDoc.FormFields("ListeSituation" & k).DropDown.ListEntries.Count <> 0 _
Then
 If wDoc.FormFields("ListeSituation" & k).DropDown.Value <> 0 _
 Then
    If wDoc.FormFields("ListeSituation" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeSituation" & k).DropDown.Value).Name <> "Choisissez une situation" _
    Then
        If wDoc.InlineShapes.Count <> 0 Then
                    For Each IsH In wDoc.InlineShapes
                        If IsH.Type = wdInlineShapeOLEControlObject Then
                            If TypeName(IsH.OLEFormat.Object) = "Label" Then
                                If IsH.OLEFormat.Object.Name = leLabelSit Then
                                    IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeSituation" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeSituation" & k).DropDown.Value).Name
                                End If
                            End If
                        End If
                    Next
                End If
        End If
    End If
End If
        
  If wDoc.FormFields("ListeIntention" & k).DropDown.ListEntries.Count <> 0 And _
    wDoc.FormFields("ListeIntention" & k).DropDown.Value <> 0 _
    Then
        If wDoc.InlineShapes.Count <> 0 Then
                    For Each IsH In wDoc.InlineShapes
                        If IsH.Type = wdInlineShapeOLEControlObject Then
                            If TypeName(IsH.OLEFormat.Object) = "Label" Then
                                If IsH.OLEFormat.Object.Name = leLabelInt Then
                                    IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeIntention" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeIntention" & k).DropDown.Value).Name
                                End If
                            End If
                        End If
                    Next
                End If
        End If
        
  If wDoc.FormFields("ListeGrammaire" & k).DropDown.ListEntries.Count <> 0 And _
    wDoc.FormFields("ListeGrammaire" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeGrammaire" & k).DropDown.Value).Name <> "Choisissez un niveau." _
    Then
        If wDoc.InlineShapes.Count <> 0 Then
                    For Each IsH In wDoc.InlineShapes
                        If IsH.Type = wdInlineShapeOLEControlObject Then
                            If TypeName(IsH.OLEFormat.Object) = "Label" Then
                                If IsH.OLEFormat.Object.Name = leLabelGram Then
                                    IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeGrammaire" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeGrammaire" & k).DropDown.Value).Name
                                End If
                            End If
                        End If
                    Next
                End If
        End If
                   
Next k

Set wDoc = Nothing

End Sub

在您的编辑中,您尝试对字符串使用 属性,但 属性仅适用于对象变量

因此,您需要在 InlineShapes 中找到 控件的存储位置,然后循环并过滤它以将其缩小到您的特定控件并更改其值。

这里有一些应该可以工作或者至少可能接近(无法测试)的东西:

Sub OuO()
Dim leLabelDom As String, _
    wDoc As Word.Document, _
    wListE As DropDown, _
    IsH As InlineShape

Set wDoc = wDoc

For k = 1 To 11
    leLabelDom = "lblDom" & k & "W1"
    Set wListE = wDoc.FormFields("ListeDomaine" & k).DropDown

    If wListE.ListEntries.Count <> 0 And _
        wListE.ListEntries.Item(wListE.Value).Name <> "Choisissez un domaine." _
        Then
            If wDoc.InlineShapes.Count <> 0 Then
                For Each IsH In wDoc.InlineShapes
                    If IsH.Type <> wdInlineShapeOLEControlObject Then
                    Else
                        'filter on name
                        With IsH.OLEFormat.Object
                            If .Name <> leLabelDom Then
                            Else
                                .Caption = wListE.ListEntries.Item(wListE.Value).Name
                            End If
                        End With
                    End If
                Next IsH
            Else
            End If
        Else
    End If
Next k

Set wDoc = Nothing
Set wListE = Nothing

End Sub