1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
Option Explicit
Sub CreerDesChampsListeDeroulante()
Dim DocEnCours As Document
Dim MonRange As Range
Dim MaListe As Variant
On Error GoTo Fin
Set DocEnCours = ActiveDocument
With DocEnCours
MaListe = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
Set MonRange = .Tables(1).Cell(21, 3).Range
CreerUnFormFieldDropDown MonRange, "AAA", MaListe, "La longueur des champs va de 1 à 17 charactères."
Set MonRange = Nothing
MaListe = Array("A", "B", "C", "D")
Set MonRange = .Tables(2).Cell(2, 1).Range
CreerUnFormFieldDropDown MonRange, "BBB", MaListe, "XXXXXXXXXXXX"
Set MonRange = Nothing
MaListe = Array("Alpha", "Bravo", "Charlie", "Delta", "Echo")
Set MonRange = .Paragraphs(.Paragraphs.Count).Range
CreerUnFormFieldDropDown MonRange, "CCC", MaListe, "Aphabet NATO"
Set MonRange = Nothing
End With
GoTo Fin
Fin:
Set DocEnCours = Nothing
End Sub
Sub CreerUnFormFieldDropDown(ByVal MonRange2 As Range, ByVal MonNom2 As String, ByVal MaListe2 As Variant, ByVal MonTexteDAide2 As String)
Dim I As Integer
Dim MonFormField As FormField
With MonRange2
' Suppression du formfield existant le cas échéant
If .FormFields.Count > 0 Then
For I = .FormFields.Count To 1 Step -1
.FormFields(I).Delete
Next I
End If
' Création du formfield
.FormFields.Add Range:=MonRange2, Type:=wdFieldFormDropDown
Set MonFormField = .FormFields(.FormFields.Count)
With MonFormField
.Enabled = True
.OwnHelp = True
.HelpText = MonTexteDAide2
For I = LBound(MaListe2, 1) To UBound(MaListe2, 1)
.DropDown.ListEntries.Add Name:=MaListe2(I)
Next I
End With
MonFormField.Name = MonNom2
'Debug.Print MonFormField.Name
Set MonFormField = Nothing
End With
End Sub |
Partager