PDA

View Full Version : Aligning an attachment in PPT



SteveBIS
06-24-2015, 06:21 PM
Hi everyone

Using VBA, I'm failing to automate alignment of just one shape in a PPT presentation.
This seems to be because Align is for ShapeRange rather than for Shape.

In code below, VBA identifies vPic as the active shape ; that's why the SendToBack command works.
But it fails at Alignment, and I cannot generalize alignment to all shapes in the slide.

Is there a way to align just the active shape ?

Sub test()
Dim aP As PowerPoint.Presentation
Dim vPic As PowerPoint.Shape

Set vPic = aP.Slides(1).Shapes.AddPicture(Filename:="c:\temp\screenshot.png")
vPic.Align msoSendToBack
vPic.msoAlignLefts, False ' ?
vPic.msoAlignBottoms, False ' ?

End Sub

' As I can't seem to align just the new attachment (and not other shapes already in the slide) I am presently forced to hard-code the alignment by the following inaccurate method :

' Set vPic = aP.Slides(1).Shapes.AddPicture(Filename:="c:\temp\screenshot.png", LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=200, Height:=300)

NickyC
07-28-2015, 02:45 AM
Hi
try changing "false" to "true" in the alignments, does that work?

John Wilson
07-28-2015, 05:32 AM
When you use AddPicture Link, save, Left and Top must be specified they are not optional.

14019

In which case most of your code is not needed and is in any case not correct

For reference the rest of the code would look like this (it is not needed)


Sub test()
Dim aP As PowerPoint.Presentation
Dim vPic As PowerPoint.Shape
Dim sr As ShapeRange
Set aP = ActivePresentation
Set vPic = aP.Slides(1).Shapes.AddPicture(FileName:="c:\temp\screenshot.jpg", LinkToFile:=False, SavewithDocument:=msoTrue, Left:=100, Top:=100)
vPic.ZOrder (msoSendToBack)
Set sr = aP.Slides(1).Shapes.Range(1)
sr.Align (msoAlignLefts), True
sr.Align (msoAlignTops), True ' means align to slide
End Sub

NickyC
07-28-2015, 06:28 PM
Hi
this might help - it aligns the selected shape on the left of the slide



Sub LeftAlign()
ActiveWindow.Selection.ShapeRange.Align msoAlignLefts, True
End Sub