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
|
Option Explicit
Function RecupererLeNumeroDeFacture(ByVal MonWord As Word.Application, ByVal DocEnCours As Word.Document, ByVal NomDuSignet As Word.Bookmark) As String
Dim MonRange As Word.Range
Dim MaSelection As Word.Selection
Dim DebutFacture As Integer, FinFacture As Integer
Dim MonSignet As Word.Bookmark
Dim Continuer As Boolean
Continuer = False
For Each MonSignet In DocEnCours.Bookmarks
If MonSignet.Name = NomDuSignet.Name Then Continuer = True
Next MonSignet
If Continuer = True Then
NomDuSignet.Range.Select
Set MaSelection = MonWord.Selection
With MaSelection
.HomeKey unit:=wdStory, Extend:=wdExtend
DebutFacture = .Characters.Count
FinFacture = DebutFacture + 6
Set MonRange = DocEnCours.Range(Start:=DebutFacture, End:=FinFacture)
RecupererLeNumeroDeFacture = MonRange.Text
Set MonRange = Nothing
End With
Set MaSelection = Nothing
NomDuSignet.Range.Select
End If
End Function
Sub RecupererLesNumerosDeFacture()
Dim MesFactures As Variant
Dim I As Long, LigneDeTitre As Long, DerniereLigne As Long
Dim AireFactures As Range, CelluleFacture As Range
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Set ShFactures = Sheets("Liste des factures")
With ShFactures
RepertoireFactures = .Range("DossierFactures")
LigneDeTitre = 10
DerniereLigne = .Cells(.Rows.Count, 1).End(xlUp).Row
Set AireFactures = .Range(.Cells(LigneDeTitre + 1, 1), .Cells(DerniereLigne, 1))
End With
Set WordApp = CreateObject("word.application") 'ouvre session word et le fichier voulu
WordApp.Visible = True 'word masqué pendant l'operation
For Each CelluleFacture In AireFactures
Set WordDoc = WordApp.Documents.Open(RepertoireFactures & "\" & CelluleFacture) 'ouvre document Word
CelluleFacture.Offset(0, 2) = RecupererLeNumeroDeFacture(WordApp, WordDoc, WordDoc.Bookmarks("numero"))
WordDoc.Close savechanges:=False
Set WordDoc = Nothing
Next CelluleFacture
WordApp.Quit
Set WordApp = Nothing
Set AireFactures = Nothing
Set ShFactures = Nothing
End Sub |
Partager