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
| Sub insert_image()
' Cette macro insere des images dans le document word actif,
' aux emplacements IMAGE <mot_cle>
'
Dim Repertoire As String
Dim Extension As String
Dim Fichier As String
Dim DesiredWidth As Double
Dim DesiredRatio As Double
'Saisie du nom du répertoire
Repertoire = "D:\Moi\RAPPORTS\EN COURS\RESULTS_DOSSIER_SIMU\DOSSIER_v3\"
'Saisie du type d'extension
Extension = "emf" 'InputBox("Type de fichier (sans le point, ex : jpg, png, bmp)", "Type de fichier", "emf")
DesiredWidth = 224 ' environ 8 cm 'Largeur d'image voulue
Selection.HomeKey Unit:=wdStory 'Pour commencer tout en haut du doc
'Initialisation : 1ere recherche
With Selection
.Find.Text = "IMAGE" 'Recherche le mot IMAGE
.Find.Execute 'Selectionne le mot IMAGE
'.EndKey Unit:=wdWord, Extend:=wdExtend
.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend 'Recupere la ligne commençant par IMAGE dans une variable
.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Ligne = .Text
MotCle = Split(Ligne, " ") 'MotCle(0) = "IMAGE"
End With
'Il faut chercher IMAGE autant de fois qu'il y est, la boucle s'arrete quand on est à la fin du doc
Do While MotCle(0) = "IMAGE"
With Selection
'Cherche dans le dossier un nom d'image contenant MotCle
If MotCle(1) = "RIEN" Then
Fichier = Dir(Repertoire & "*" & LCase(MotCle(2)) & "*" & "*.JPG", vbDirectory)
If Fichier <> "" Then 'si fichier est non vide (ou nbre de caractère >= 1)
.InlineShapes.AddPicture FileName:=Repertoire & Fichier _
, LinkToFile:=False, SaveWithDocument:=True ' Insere cette image a la place des mots clés
End If
Else
Fichier = Dir(Repertoire & MotCle(1) & "\*" & LCase(MotCle(2)) & "*" & LCase(MotCle(3)) & "*" & Extension, vbDirectory)
If Fichier <> "" Then 'si fichier est non vide (ou nbre de caractère >= 1)
.InlineShapes.AddPicture FileName:=Repertoire & MotCle(1) & "\" & Fichier ' Insere cette image a la place des mots clés
.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
DesiredRatio = DesiredWidth / .InlineShapes(1).Width
.InlineShapes(1).Width = DesiredWidth
.InlineShapes(1).Height = .InlineShapes(1).Height * DesiredRatio
End If
End If
.EndOf wdWord, wdMove
.Select
.Find.Text = "IMAGE" ' Recherche le mot IMAGE
.Find.Execute ' Selectionne le mot IMAGE
.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend ' Recupere la ligne commençant par IMAGE dans une variable
.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Ligne = .Text
MotCle = Split(Ligne, " ") 'MotCle(0) = "IMAGE"
End With
Loop
MsgBox ("Fini !")
End Sub |
Partager