I want to align images to the centre of the slide in Powerpoint. Each slide has only 1 image. I'm using a VBA macro to do this but getting an error.
The macro im currently using is generated by chatgpt
Sub CenterImages()
Dim shp As Shape
For Each shp In ActiveWindow.Selection.SlideRange.Shapes
shp.Left = (ActiveWindow.Selection.SlideRange.Width - shp.Width) / 2
shp.Top = (ActiveWindow.Selection.SlideRange.Height - shp.Height) / 2
Next
End Sub
Im getting Compile error
Related
Code that perfectly works in earlier versions of PPT stopped working in 2016.
When I try to change the left property of a shape in a chart, I get a Method left of object shape failed error.
I can perfectly read the .Left property.
I am running out of ideas? What can I do?
Sub test11()
Dim sld As Slide
Dim objChart As Object
Dim shpBubble As Object
Set sld = ActivePresentation.Slides("ScatterPlot01_Purch6")
Set objChart = sld.Shapes("Chart01").Chart
sld.Select
objChart.Select
Set shpBubble = objChart.Shapes("P01")
'shpBubble.Select
Debug.Print shpBubble.Left, shpBubble.Visible
shpBubble.Left = 10
End Sub
UPDATE
Having tested in PowerPoint 2010 and 2013, where it works, this now looks like a bug in 2016!
* END *
I managed to recreate the error in PowerPoint 2016 (PC) by manually adding a shape to a test chart (select the chart then click Format / Insert Shapes) and trying to write to several of it's properties including position and formatting such as changing fill colour. All generate an error.
Maybe one workaround is to use the .Delete method to delete the desired shape and then add a new shape at the required size and position. Something like this:
Sub test11()
Dim sld As Slide
Dim objChart As Chart 'Object
Dim shpBubble As Shape 'Object
Set sld = ActivePresentation.Slides("ScatterPlot01_Purch6")
Set objChart = sld.Shapes("Chart01").Chart
sld.Select
objChart.Select ' this won't work as you can only select the parent shape sld.Shapes("Chart01")
With objChart
.Shapes("P01").Delete
.Shapes.AddShape msoShapeOval, 10, 10, 20, 20
End With
End Sub
The challenge is that because the new shape is added as read only, the formatting can't be set!
I am trying to create a shape on a slide in PowerPoint (2010) VBA
I have created a button and this code:
Private Sub AddShape_Click()
Dim shp As Shape
Dim sld As Slide
Set sld = Application.ActiveWindow.View.Slide
Set shp = sld.Shapes.AddShape(Type:=msoShapeRectangle, _
Left:=24, Top:=65.6, Width:=672, Height:=26.6)
'No Shape Border
shp.Line.Visible = msoFalse
'Shape Fill Color
shp.Fill.ForeColor.RGB = RGB(137, 143, 75)
shp.Fill.BackColor.RGB = RGB(137, 143, 75)
End Sub
When I run the presentation as a slide show and click the Add Shape button, I get the following error:
Run-time error '-2147188160 (80048240)':
Application (unknown member): Invalid request. There is no currently active document window.
Everything I have found online indicates that this code should run OK.
All assistance appreciated!!!
Carolyn
You will get the No Current Active Document Window error if you run your code while the presentation is in Slideshow (fullscreen) mode. Try this, instead:
set sld = Application.ActivePresentation.SlideShowWindow.View.Slide
Things work differently in slide show view, but a couple very simple modifications will get this fixed up. Add this to the project and assign the AddShape_Click as an Action Setting (Run Macro):
Public Sub AddShape_Click(oBtn As Shape)
' It has to be public for the action setting to see it
Dim shp As Shape
Dim sld As Slide
'Set sld = Application.ActiveWindow.View.Slide
Set sld = oBtn.Parent
Set shp = sld.Shapes.AddShape(Type:=msoShapeRectangle, _
Left:=24, Top:=65.6, Width:=672, Height:=26.6)
'No Shape Border
shp.Line.Visible = msoFalse
'Shape Fill Color
shp.Fill.ForeColor.RGB = RGB(137, 143, 75)
shp.Fill.BackColor.RGB = RGB(137, 143, 75)
End Sub
You can also get this error if PowerPoint recently crashed and is still running in the background. Try killing any such powerpoint processes using task manager and then try again.
I am trying to remove all the shadow effects from all the textframes in the any slides of the presentation via a macro .
The below macro is running well with powerpoint 2007 but not working with 2010.
Sub NoTextShadows()
Dim oSld As Slide
Dim oShp As Shape
For Each oSld In ActivePresentation.Slides
For Each oShp In oSld.Shapes
If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Font.Shadow = msoFalse
oShp.Shadow.Visible = msoFalse
End If
End If
Next oShp
Next oSld
End Sub
Please suggest any idea to workaround this. Thanks.
I made some tests and I found out that shadows in PowerPoint 2010 can appear for Text (for specific) and for shapes in general. It's strange that two shadows can be applied to the shape independently, but only one of them could be removed by Macro. Here is what I did to remove one layer of shadow :
oShp.Shadow.Size = 0
OShp.Shadow.Blur = 0
oShp.Shadow.Visible = msoFalse
Try adding these lines inside your main For Loop and see
For more information, Working with Properties of the ShadowFormat Class in Office 2010
Can anyone help me with an issue in PPT 2007.
I have an image (a chart from excel pasted as 'Picture (Enhanced Metafile)') in a slide. I just need to crop this image to fit in the slide. I tried following code, but to no avail:
'1:
With ActivePresentation.Slides(1).Shapes(1)
.PictureFormat.CropLeft = 10
.PictureFormat.CropTop = 10
.PictureFormat.CropRight = 10
.PictureFormat.CropBottom = 10
End With
'Runtime error "ActiveX Component can't create object
'2:
ActiveWindow.Selection.ShapeRange.PictureFormat.CropRight = 10
ActiveWindow.Selection.ShapeRange.PictureFormat.CropLeft = 10
ActiveWindow.Selection.ShapeRange.PictureFormat.CropBottom = 10
ActiveWindow.Selection.ShapeRange.PictureFormat.CropTop = 10
'Object doesnt support this property or method
Any suggestion is most welcome.
This works with EMFs pasted from Excel:
Dim oSh as Shape
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Note: ShapeRange(1), not just ShapeRange
' That's the problem with your second example
With oSh
.CropLeft = 10
' etc
End With
The fact that it's yelling about an activex object in the first example makes me wonder whether the first shape is really an EMF, which would not be an ActiveX object.
I am programatically copying tables and graphs from Excel to PowerPoint.
Ideally, I'd like to center the graphs on the slide and resize them to fit the slide if necessary.
This shouldn't be too hard, and I can check & modify .Top, .Left, .Width, .Height of the shape just fine, but how do I find out the width/height of the slide itself to do the proper positioning?
The following will give you the Height and Width ...
Just divide by 2 :D
Sub a()
Dim a As Presentation
Set a = ActivePresentation
MsgBox a.PageSetup.SlideHeight, a.PageSetup.SlideWidth
End Sub
HTH