PDA

View Full Version : .Addpicture adds the picture to a placeholder rather as a new shape



philcoul
09-30-2013, 08:04 AM
I have a vba script which creates slides based on the contents of a Word file. The steps are:

add a new slide (mySlide). It has placeholders for a title and bodytext
copy text from the Word file to the title and bodytext.
add a picture using mySlide.Shapes.AddPicture Normally this works fine and creates 3 shapes which I can then size as required.

However if the bodytext is empty the picture is added to the bodytext shape rather than as a new shape which complicates further processing considerably. How can I ensure .AddPicture always creates a third shape on the slide?

John Wilson
09-30-2013, 08:53 AM
I'm assuming you have 2007 or later?

The problem is you almost certainly DON'T have Title and Bodytext ( you can create this but it's not usually standard) You probably have Title and CONTENT.

Since Microsoft know best - There's a content placeholder and he's adding content. No brainer!

Ways to get around this is either create a now custom layout thet does have a Text Placeholder OR

Sub addPic()
Dim oshp As Shape
Dim osld As Slide
On Error Resume Next
Set osld = ActiveWindow.Selection.SlideRange(1)
If Err <> 0 Then Exit Sub
On Error GoTo 0
For Each oshp In osld.Shapes
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.Type = ppPlaceholderObject Then
If Not oshp.TextFrame.HasText Then oshp.TextFrame.TextRange = "DUMMY"
End If
End If
Next oshp
osld.Shapes.AddPicture FileName:="C:\Users\John\Desktop\Pic1.jpg", LinkToFile:=False, SaveWithDocument:=True, Left:=10, Top:=10
For Each oshp In osld.Shapes
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.Type = ppPlaceholderObject Then
If oshp.TextFrame.TextRange = "DUMMY" Then oshp.TextFrame.DeleteText
End If
End If
Next oshp
End Sub

philcoul
09-30-2013, 10:22 AM
Thanks for the response. Its not very elegant (thanks Microsoft) but it will work and save me a lot of other problems. You were right it is a default Title/Content slide - I was sloppy with my wording.