Bonjour, voici une fonction permettant de rechercher et remplacer un mot par une image dans un document (doc, docx), par le biais des objets OLE d'Open Office (Ou Libre Office).

Le paramètre _bAsLink permet de préciser si l'on souhaite que l'image soit liée ou intégrée au document.
Par défaut, l'image est intégrée dans le document car, insérée en tant que lien, l'image n'apparaissait pas dans les zones de texte des documents .doc.
(Cela fonctionnait par contre avec les documents .docx)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
 
PROCEDURE InsererImage(_sDocumentChemin est une chaîne,LOCAL _sChaineARemplacer est une chaîne,LOCAL _sImageChemin est une chaîne,LOCAL _bAsLink est un booléen=Faux)
 
//---------------------------------------------------
//Ouverture du document
 
LOCAL
	oServiceManager est un objet OLE dynamique
	oDesktop est un objet OLE dynamique
	oDocument est un objet OLE dynamique
	sCheminFormaté est une chaîne
 
oServiceManager = allouer un objet OLE ("com.sun.star.ServiceManager")
oDesktop = oServiceManager>>createInstance("com.sun.star.frame.Desktop")
 
//Ne pas afficher le document
tArgs est un tableau de 1 objet Automation dynamique //Tableau de paramètres
tArgs[1]= oServiceManager>>Bridge_GetStruct("com.sun.star.beans.PropertyValue") 
tArgs[1]>>Name = "Hidden" 
tArgs[1]>>Value = True
 
//Formatage du chemin du fichier
sCheminFormaté = "file:///"+Remplace(_sDocumentChemin,"\","/")
//Ouverture du fichier dans OpenOffice Writer
oDocument = oDesktop>>LoadComponentFromURL(sCheminFormaté,"_blank",0,tArgs)
 
//---------------------------------------------------
//Recherche de la valeur et remplacement par l'image
 
LOCAL
	sInfoImage est une chaîne
	iLargeur est un entier
	iHauteur est un entier
	rLargeurMM est un réel
	rHauteurMM est un réel
	sGraphicURL est une chaîne
 
//Déterminer la taille de l'image en mm (à partir d'une taille en pixels)
sInfoImage 	= InfoBitmap(_sImageChemin)
iLargeur 	= Val(ExtraitChaîne(sInfoImage,2))
iHauteur 	= Val(ExtraitChaîne(sInfoImage,3))
Pixels_Vers_Mm(iLargeur,iHauteur,rLargeurMM,rHauteurMM)
rLargeurMM = rLargeurMM*100
rHauteurMM = rHauteurMM*100
 
sGraphicURL = "file:///"+Remplace(_sImageChemin,"\","/")
 
//----------------------------------------------
//Intégration de l'image dans le document
 
SI _bAsLink = Faux ALORS
	oShape est un objet OLE dynamique
	toProp est un tableau de 1 objet Automation dynamique
	oProvider est un objet OLE dynamique
 
	oShape = oDocument>>createInstance("com.sun.star.drawing.GraphicObjectShape")
	oDocument>>getDrawPage()>>add(oShape)
 
	toProp[1] = oServiceManager>>Bridge_GetStruct("com.sun.star.beans.PropertyValue")
	toProp[1]>>Name  = "URL"
	toProp[1]>>Value  = sGraphicURL
 
	oProvider = oServiceManager>>createInstance("com.sun.star.graphic.GraphicProvider")
	oShape>>Graphic = oProvider>>queryGraphic(toProp)
FIN
 
//----------------------------------------------
LOCAL
	oSearch est un objet OLE dynamique	
	oFoundAll est un objet OLE dynamique
	oFound est un objet OLE dynamique
	oCursor est un objet OLE dynamique
 
oSearch = oDocument>>createSearchDescriptor()
oSearch>>SearchString 	= _sChaineARemplacer
oSearch>>SearchWords 	= OLETrue
 
oFoundAll = oDocument>>FindALL(oSearch)
POUR i = 0 _A_ oFoundAll>>Count -1
 
	oFound = oFoundAll>>GetbyIndex(i)	
	oFound>>setString("")
	oCursor = oFound>>getStart()
 
	oImage est un objet OLE dynamique
	oImage = oDocument>>createInstance("com.sun.star.text.GraphicObject")
 
	SI _bAsLink = Vrai ALORS
		oImage>>GraphicURL = sGraphicURL
	SINON
		oImage>>GraphicURL = oShape>>GraphicURL
	FIN
 
	//Anchor Type
	//https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/TextContentAnchorType.html#AT_PARAGRAPH
	//HoriOrient
	//http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/HoriOrientation.html
	//HoriOrientRelation
	//https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/RelOrientation.html
	//VertOrient
	//http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/VertOrientation.html
	//TextWrap
	//https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/WrapTextMode.html
 
	oImage>>AnchorType 			= 1
	oImage>>TopMargin 			= 0
	oImage>>BottomMargin 		= 0
	oImage>>LeftMargin 			= 0
	oImage>>RightMargin 		= 0
	oImage>>BorderDistance		= 0
	oImage>>HoriOrient 			= 0
	oImage>>VertOrient 			= 9
 
	oImage>>Width 			= rLargeurMM
	oImage>>Height 			= rHauteurMM
	oImage>>SizeProtected 	= OLETrue
 
	oFound>>text>>insertTextContent(oCursor,oImage,OLETrue)
FIN
 
SI _bAsLink = Faux ALORS
	oDocument>>getDrawPage()>>remove(oShape)
FIN
 
//---------------------------------------------------
//Enregistrement et fermeture du document
 
LOCAL
	mNoArgs est un tableau de 0 Variant
	sDocumentCheminSav est une chaîne
 
sDocumentCheminSav = fExtraitChemin(_sDocumentChemin,fDisque+fFichier+fRépertoire)+HeureSys()+fExtraitChemin(_sDocumentChemin,fExtension)
 
sCheminFormaté = "file:///"+Remplace(sDocumentCheminSav,"\","/")
//Enregistrement du document (Enregister sous)
oDocument>>storeToURL(sCheminFormaté, mNoArgs)
//oDocument>>store()
oDocument>>Close(Vrai)
 
RENVOYER sDocumentCheminSav
 
CAS EXCEPTION:
	Erreur(ExceptionInfo(errComplet))
	RENVOYER Faux
La fonction Pixels_Vers_Mm :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
PROCEDURE Pixels_Vers_Mm(LOCAL _iLargeurPixel est un entier,LOCAL _iHauteurPixel est un entier,__rLargeurMm est un réel,__rHauteurMm est un réel)
//Effectue la conversion entre un nombre de pixels à l'écran et la taille en millimètres
 
//http://support.microsoft.com/kb/114709/fr
 
LOCAL
	CONST_HORZSIZE est un entier = 4
	CONST_VERTSIZE est un entier = 6
	CONST_HORZRES est un entier = 8
	CONST_VERTREZ est un entier = 10
 
	iLargeurPixel est un entier
	iHauteurPixel est un entier
	iLargeurMm est un entier
	iHauteurMm est un entier
	rCoefL est un réel
	rCoefH est un réel
 
	iResHandle  est un entier = SysRécupèreDC(0)
 
iLargeurPixel = AppelDLL32("gdi32","GetDeviceCaps",iResHandle,CONST_HORZRES) 
iHauteurPixel = AppelDLL32("gdi32","GetDeviceCaps",iResHandle,CONST_VERTREZ) 
iLargeurMm    = AppelDLL32("gdi32","GetDeviceCaps",iResHandle,CONST_HORZSIZE) 
iHauteurMm    = AppelDLL32("gdi32","GetDeviceCaps",iResHandle,CONST_VERTSIZE) 
 
rCoefL = iLargeurPixel/iLargeurMm
rCoefH = iHauteurPixel/iHauteurMm
 
__rLargeurMm = _iLargeurPixel/rCoefL
__rHauteurMm = _iHauteurPixel/rCoefH
Au final, l'appel se fait de cette façon :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
LOCAL
	sDocumentChemin est une chaîne
	sDocumentCheminSav est une chaîne
	sChaineARemplacer est une chaîne
	sImageChemin est une chaîne
 
sDocumentChemin 	= "c:\MonDocument.doc"
sChaineARemplacer  	= "_MotARemplacer"
sImageChemin		= "c:\MonImage.png"
 
sDocumentCheminSav = InsererImage(sDocumentChemin,sChaineARemplacer,sImageChemin,Faux)
SI fFichierExiste(sDocumentCheminSav) ALORS
	LanceAppliAssociée(sDocumentCheminSav)
FIN
Informations complémentaires ici :
https://wiki.openoffice.org/wiki/FR/...More_Than_Text

Bonne prog