Si Find ne trouve pas passer a la suite
k
bonjour le forum,
dans ma macro j'utilise Find pour trouvé un mot souhaité dans un doc Word mais forcement s'il ne trouve rien la macro plante.
J'aimerai que si find ne trouve rien il passe au find suivant. voici un bout de mon code:
' adresse (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Adresse 1 :"
WApp.Selection.MoveLeft unit:=2, Count:=12, Extend:=4
Set WSel = WApp.Selection
ws.Range("D8") = Trim(Split(WSel, "comptage")(1))
ws.Range("D8") = Replace(ws.Range("D8"), Chr(13), "")
' code postal (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "code postal :"
WApp.Selection.MoveLeft unit:=2, Count:=10, Extend:=4
Set WSel = WApp.Selection
ws.Range("D9") = Trim(Split(WSel, ":")(1))
ws.Range("D9") = Replace(ws.Range("D9"), Chr(13), "")
' commune (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Commune :"
WApp.Selection.MoveLeft unit:=2, Count:=10, Extend:=4
Set WSel = WApp.Selection
ws.Range("E9") = Trim(Split(WSel, ":")(1))
ws.Range("E9") = Replace(ws.Range("E9"), Chr(13), "")
' interlocuteur (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "N° IDC :"
WApp.Selection.MoveRight unit:=2, Count:=4, Extend:=1
Set WSel = WApp.Selection
ws.Range("I47") = Trim(Split(WSel, ":")(1))
ws.Range("I47") = Replace(ws.Range("I47"), Chr(13), "")
' telephone (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "N° Tél. de l'interlocuteur 1 :"
WApp.Selection.MoveLeft unit:=2, Count:=12, Extend:=4
Set WSel = WApp.Selection
ws.Range("C42") = Trim(Split(WSel, ":")(1))
ws.Range("C42") = Replace(ws.Range("C42"), Chr(13), "")au plaisir de vous lire
Cordialement
P
Hello
Ajoute dans ta macro:
On error Goto Err1
Exit Sub
Err1:
Msgbox "Erreur" & err.number
end Sub
k
bonjour percolator,,
merci de ta réponse mais je ne veux pas de message d'erreur , j'aimerai que si le find ne trouve pas la premiere valeur qu'il passe à la seconde valeur et ainsi de suite.
j'avais pensé à cela mais ca ne fonctionne pas non plus:
' -- Déclaration des variables
Dim wb As Workbook 'classeur Excel dans lequel on importe les données
Dim ws As Worksheet 'onglet Excel dans lequel on importe les données
Dim Fichier As Variant
Dim WApp As Object, WDoc As Object, WSel As Object
' -- Initialisation des variables
Set wb = ThisWorkbook 'on sauvegarde dans la fichier en cours
Set ws = wb.Sheets("HTA8") 'on sauvegarde dans la 1re feuille
ChDrive "C"
ChDir "C:\Users\Temp" 'chemin où se trouve le fichier extrait de mercuri
Fichier = Application.GetOpenFilename("Text Files (*.doc*), *.doc*") 'affichage boite de dialogue pour choisir un document Word
If Fichier = False Then Exit Sub
Set WApp = CreateObject("Word.Application") 'pour créer un objet Word
WApp.Visible = False 'ne pas afficher Word pendant l'exécution
Application.ScreenUpdating = False
Set WDoc = WApp.Documents.Open(Fichier) 'ouvre le document Word
On Error GoTo 3
' Nom ou raison sociale (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Nom :"
WApp.Selection.MoveLeft unit:=2, Count:=20, Extend:=4
Set WSel = WApp.Selection
ws.Range("D7") = Trim(Split(WSel, "l'installation")(1))
ws.Range("D7") = Replace(ws.Range("D7"), Chr(13), "")
3:
On Error GoTo 4
' adresse (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Adresse 1 :"
WApp.Selection.MoveLeft unit:=2, Count:=12, Extend:=4
Set WSel = WApp.Selection
ws.Range("D8") = Trim(Split(WSel, "comptage")(1))
ws.Range("D8") = Replace(ws.Range("D8"), Chr(13), "")
4:
On Error GoTo 5
' code postal (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "code postal :"
WApp.Selection.MoveLeft unit:=2, Count:=10, Extend:=4
Set WSel = WApp.Selection
ws.Range("D9") = Trim(Split(WSel, ":")(1))
ws.Range("D9") = Replace(ws.Range("D9"), Chr(13), "")
5:
On Error GoTo 6
' commune (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "Commune :"
WApp.Selection.MoveLeft unit:=2, Count:=10, Extend:=4
Set WSel = WApp.Selection
ws.Range("E9") = Trim(Split(WSel, ":")(1))
ws.Range("E9") = Replace(ws.Range("E9"), Chr(13), "")
6:
On Error GoTo 7
' interlocuteur (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "N° IDC :"
WApp.Selection.MoveRight unit:=2, Count:=3, Extend:=1
Set WSel = WApp.Selection
ws.Range("I47") = Trim(Split(WSel, ":")(1))
ws.Range("I47") = Replace(ws.Range("I47"), Chr(13), "")
7:
On Error GoTo 8
' telephone (par la fonction FIND)
WApp.Selection.HomeKey unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "N° Tél. de l'interlocuteur 1 :"
WApp.Selection.MoveLeft unit:=2, Count:=12, Extend:=4
Set WSel = WApp.Selection
ws.Range("C42") = Trim(Split(WSel, ":")(1))
ws.Range("C42") = Replace(ws.Range("C42"), Chr(13), "")
8:k
C'est bon jai trouvé ,il fallait mettre on error goto juste avant la ligne où se produit l'erreur.