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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
Public MatriceInfo() As Variant
Sub getWordFormData()
Dim WdApp As New Word.Application
Dim MyDoc As Word.Document
Dim FmFld As Word.FormField
Dim MyFolder As String, StrFile As String
Dim MyWkSht As Worksheet, I As Long, j As Long
Dim IndexMatrice As Integer
Dim Titre As Variant
WdApp.Visible = True
myFolder = "D:\Route et PM en vigueur\Changement huile test"
Application.ScreenUpdating = False
If MyFolder = "" Then Exit Sub
Set MyWkSht = ActiveSheet
With MyWkSht
Titre = Array("No tâche", "No équipement", "No moteur", "Titre", "Emplacement", "Secteur", "fréquence", "PM", "Pd", "MD", "PCE", "Route", "Arret", "Marche", "Mecanique", "Menuisier", "Électro", "Opérateur", "Estimation")
With .Range("B1:T1")
.Value = Titre
.Font.Bold = True
End With
I = 2
End With
StrFile = Dir(MyFolder & "\*.doc*", vbNormal)
While StrFile <> ""
I = I + 1
Set MyDoc = WdApp.Documents.Open(Filename:=MyFolder & "\" & StrFile, AddToRecentFiles:=False, Visible:=False)
With MyDoc
MsgBox .Tables(1).Rows.Count
j = 4
ChargementMatrice MyDoc
For Each FmFld In .FormFields
'mets la valeur en colonne en débutant a celle spécifié par j= " " ici c'est 4 pour la 4eme colonne
j = j + 1
MyWkSht.Cells(I, j) = FmFld.Result
Next
Select Case .Tables(1).Rows.Count
Case 5
For IndexMatrice = LBound(MatriceInfo) To UBound(MatriceInfo)
'Debug.Print IndexMatrice & " : " & MatriceInfo(IndexMatrice)
Select Case IndexMatrice
Case 0 ' Tâche
MyWkSht.Cells(I, 2) = Trim(Split(MatriceInfo(IndexMatrice), "No. Tâche: ")(1))
Case 2 ' Titre
MyWkSht.Cells(I, 5) = MatriceInfo(IndexMatrice)
Case 12 ' Estimation
MyWkSht.Cells(I, 20) = Trim(Split(MatriceInfo(IndexMatrice), " Estimation (hom.- heures):")(1))
Case 14 ' Equipement
MyWkSht.Cells(I, 3) = MatriceInfo(IndexMatrice)
Case 15 ' Moteur
MyWkSht.Cells(I, 4) = Trim(Split(MatriceInfo(IndexMatrice), "No. Moteur*:")(1))
End Select
Next IndexMatrice
Case 6
For IndexMatrice = LBound(MatriceInfo) To UBound(MatriceInfo)
'Debug.Print IndexMatrice & " : " & MatriceInfo(IndexMatrice)
Select Case IndexMatrice
Case 0 ' Tâche
MyWkSht.Cells(I, 5) = Trim(Split(MatriceInfo(IndexMatrice), "No. Tâche: ")(1))
Case 2 ' Titre
MyWkSht.Cells(I, 8) = MatriceInfo(IndexMatrice)
Case 12 ' Estimation
MyWkSht.Cells(I, 23) = Trim(Split(MatriceInfo(IndexMatrice), " Estimation (hom.- heures):")(1))
Case 14 ' Equipement
MyWkSht.Cells(I, 6) = MatriceInfo(IndexMatrice)
Case 15 ' Moteur
MyWkSht.Cells(I, 7) = Trim(Split(MatriceInfo(IndexMatrice), "No. Moteur*:")(1))
End Select
Next IndexMatrice
End Select
'pour que les colonnes s'ajuste au texte
MyWkSht.Columns.AutoFit
End With
'pour fermer le fichier et sans faire de sauvegarde
MyDoc.Close SaveChanges:=False
StrFile = Dir()
Wend
'pour fermer l'application word et purger les mémoires (vider presse papier)
WdApp.Quit
Set MyDoc = Nothing: Set WdApp = Nothing: Set MyWkSht = Nothing
Application.ScreenUpdating = True
MsgBox "Fin de l'import !", vbInformation
End Sub |
Partager