[E-02] VBA : Récupérer du texte dans Word à partir d'Excel
:roll:Bonjour,
je souhaite récupérer via macro Excel (Excel2002 sous XP) des données contenues dans un fichier .doc (Word2002).
Exemple : ici, dans ma macro VBA Excel, je voudrais récupérer le texte (une référence sur 5 caractères) qui se trouve après le mot "Facture :" dans le fichier Word qui est actuellement ouvert.
J'ai procédé en deux temps :
- écriture de la macro sous Word --> ça marche !
- transposition de la macro sous Excel --> ça ne marche pas
Voici ma macro sous Word (qui fonctionne donc) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Dim Reffact As Range
Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="Facture :", Forward:=True
If myRange.Find.Found = True Then
If Selection.StoryType = wdMainTextStory Then
wUnits = Selection.Move(Unit:=wdWord, Count:=1)
End If
If wUnits < 1 Then
MsgBox "Numéro de facture inexistant"
Else
Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
Set Reffact = ActiveDocument.Range(Selection.Start, Selection.End)
MsgBox "Facture = " & Reffact.Text
End If |
Voici ma macro transposée sous Excel qui ne fonctionne pas :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="Facture :", Forward:=True
If myRange.Find.Found = True Then
'on a bien trouvé une référence de facture
If FichierWord.Selection.StoryType = .wdMainTextStory Then
wUnits = Selection.Move(Unit:=wdWord, Count:=1)
End If
If wUnits < 1 Then
MsgBox "Numéro de facture inexistant"
Else
FichierWord.Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend
Set Reffact = ActiveDocument.Range(Selection.Start, Selection.End)
MsgBox "Facture = " & Reffact.Text
End If
End If |
Il doit manquer des blocs "With - End With" mais je ne connais pas la syntaxe.
Quelqu'un aurait une idée ou des pistes ?
Par avance merci.