我有一个包含旧版DropDown列表的Word表格。有11行,让我们说4列。每个单元格都包含一个 Legacy DropDown List ,它将每个列的计数设置为11(总共44个)。
我必须从这些旧版DropDown列表中获取数据,然后将其放入同一文档另一页的另一个表格中的标签。
到目前为止没有问题,虽然我不得不把它全部写下来而不是使用循环,但我没有问题,因为我找不到在我的标签名称中放置变量的方法。
我将代码缩短到第一周的第一行(因此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周。
我已经通过循环为它们命名了标签。因此,第一周的名称全部为lblDom1W1
至lblDom11W1
,其他标签也是如此(仅最后一位数字更改(ei lblDom1W2
降至lblDom11W2
) )。
此外,我想并尝试了一些我认为不起作用的方法:
我浏览了这篇文章,但我并不完全理解其中的所有内容,我不确定这是否是我需要的,因为它是VB.NET而且没有完全是VBA。 Post I checked
在用户R3uK评论之后,我尝试了这个,但它也不起作用。我为leLabelDom.Caption...
提供了无效限定符:
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
从用户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
答案 0 :(得分:1)
在编辑中,您尝试将属性用于String,但属性仅用于对象变量。
因此,您需要找到存储控件的位置,在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