Erreur VBA légende shape
Bonjour à tous.
J'ai bien avancé sur mon problème de légende de photo. J'ai rebidouillé un programme permettant d'ajouter une photo, puis d'ajouter une forme avec un texte, puis de regrouper les deux pour ne faire qu'une forme. Le code ci-dessous fonctionne à merveille sur mon fichier test qui est un fichier .doc
Dim Image As Word.Shape
Dim Legende As Word.Shape
Dim PhotoLegende As Word.Shape
Set Image = ActiveDocument.Shapes.AddPicture(FileName:="chemin vers mon image")
With Image
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeCenter
.LockAspectRatio = msoTrue
.Width = 300
.Name = "c1"
End With
Set Legende = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=Selection.Information(wdHorizontalPositionRelativeToPage), Top:=Selection.Information(wdVerticalPositionRelativeToPage), Width:=300, Height:=30)
With Legende
.Line.Transparency = 1
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeCenter
.Name = "c2"
End With
Legende.Select
Selection.Font.Bold = wdToggle
Selection.Range.Text = "Légende de ma photo"
Selection.Font.Size = 12
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Legende.Select
Set Legende = Nothing
Set PhotoLegende = ActiveDocument.Shapes.Range(Array("c1", "c2")).Group
With PhotoLegende
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeCenter
.Name = "photo avec légende"
End WithMais voilà que quand j'intègre ce code à mon document de travail (document excel), il me met une erreur sur cette ligne
Set Legende = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=Selection.Information(wdHorizontalPositionRelativeToPage), Top:=Selection.Information(wdVerticalPositionRelativeToPage), Width:=300, Height:=30)Erreur 438,
propriété ou méthode non géré par cet objet.
Comment savoir de quel objet il s'agit ? Est-ce que c'est la selection ? La Shape ?
Je vous remercie d'avance pour votre aide.
Je pense que tu reboucles sur le même problème que celui du "Shape" non préfixé par "Word." et qui de ce fait était assimilé à un objet Excel.
ActiveDocument => génère une erreur car non connu d'Excel, par contre Word.ActiveDocument ça le fait !
Tu as tout à fait raison !
Je n'arrive cependant pas à corriger. J'ai remplacé ActiveDocument par DocWord, en sachant que j'ai mis cela auparavant
Dim DocWord As Word.Document
Dim appWord As Word.Application
Set appWord = New Word.Application
appWord.Visible = True
Set DocWord = appWord.Documents.AddEffectivement, quand j'essaye de faire un MsgBox vartype(Selection), il me met nothing.
Il y a quelque chose qui m'échappe décidément
Voici le bout de code en question, une fois intégré dans excel
Photo = Dir(CheminPhoto & NomPhoto)
If Photo <> "" Then
'DocWord.Paragraphs.Last.Range.Select
'DocWord.Paragraphs.Last.Range.InsertBefore (vbCrLf & vbCrLf & vbCrLf)
Set Image = DocWord.Shapes.AddPicture(Filename:=CheminPhoto & NomPhoto, linktofile:=False, savewithdocument:=True)
With Image
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeCenter
.LockAspectRatio = msoTrue
.Width = 300
.Name = "c" & Count & j
End With
Image.Select
'
' MsgBox (VarType(Selection))
DocWord.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=Selection.Information(wdHorizontalPositionRelativeToPage), Top:=Selection.Information(wdVerticalPositionRelativeToPage), Width:=300, Height:=30)
With Legende
.Line.Transparency = 1
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeCenter
.Name = "d" & Count & j
End WithMais la ligne Image.Select ne fonctionne visiblement pas.
J'ai essayé avec un DocWord.Paragraphs.Last.Range.Select mais ça ne marche pas non plus.
Je précise que cette partie est importante pour moi puisque je veux intégrer la légende sous la photo, donc là où est le curseur à ce moment là du code
L'image est créée ?
Remplaces MsgBox (VarType(Selection)) par MsgBox Image.type tu devrais obtenir un nombre 13 à priori. Tu obtiens quoi ?
Pourquoi veux-tu faire un select sur l'image ?
Dans With Legende Legende correspond à quoi ?
Je commence par te remercier pour ta précieuse aide. A chaque fois que tu écris, cela me permet de réfléchir et de commencer à réparer les erreurs.
J'ai bien un problème avec selection (qui était vide). Suite à tes conseils, je l'ai remplacé par
Set Legende = DocWord.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=DocWord.Paragraphs.Last.Range.Information(wdHorizontalPositionRelativeToPage), Top:=DocWord.Paragraphs.Last.Range.Information(wdVerticalPositionRelativeToPage), Width:=300, Height:=30)
With Legende
.Line.Transparency = 1
.WrapFormat.Type = wdWrapTopBottom
.Left = wdShapeCenter
.Name = "d" & Count & j
End WithEt ça ça marche. Je vois bien la zone de texte se créer. Par contre, je n'arrive pas à écrire dedans ! Il y a une erreur avec la suite
Legende.Select
Selection.Font.Bold = wdToggle
Selection.Range.Text = "Légende de ma photo"
Selection.Font.Size = 12
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Legende.SelectPour répondre à ta question, Legende est la zone de texte (forme rectangle) qui contient la légende de la photo. Elle se met sous la photo théoriquement
Je fais les tests de vartype. C'est encore ma sélection qui foire, puis je reviens
En fait, je ne sait pas me servir de l'outil selection.
Quand je mets Legende.Select, mon but est de prendre la zone de texte pour la modifier. Or, le msgbox vartype(selection) me dit 0.
Image.Type me dit bien 13 et Legende.Type me dit 17. Une variable d'octet ????
J'ai trouvé ! hourra !
h = DocWord.Shapes.Count
DocWord.Shapes(h).TextFrame.TextRange = "Légende de ma photo"Pourquoi ça marchait sur word et pas excel... certainement en lien avec ce que tu disais. Le range.Text n'existait pas sur excel
Merci pour ton aide en tout cas. ça m'a débloqué !
Impec !