Can a Macro Uncomment itself? - vba

Suppose I have the below, in which for every selected shape another one will be created:
For Each Shp In ActiveWindow.Selection.ShapeRange'.GroupItems
as you can see, at the end of the line objects within groups are commented out. Would it be possible to tell VBA to uncomment it, if for example a ListBox gives the option to apply the macro for grouped items? This would not be out of laziness, rather to save lines and possibly reduce the file size. And just to understand more how everything works. Or VBA is intrinsically blind to comments?

Here's a simple example of handling shapes whether within groups or not:
Sub Main()
Dim oSh As Shape
For Each oSh In ActivePresentation.Slides(1).Shapes
Call HandleShape(oSh)
Next
End Sub
Sub HandleShape(oSh As Shape)
Dim x As Long
If oSh.Type = msoGroup Then
For x = 1 To oSh.GroupItems.Count
Call HandleShape(oSh.GroupItems(x))
Next
Else
oSh.Fill.ForeColor.RGB = RGB(255, 0, 0)
End If
End Sub

Related

For each shape Macro not deleting all shapes at once in PowerPoint VBA

So I have a Slide on PowerPoint where I have a Macro that should delete all shapes with the name that starts with "element". It does delete the shapes that I want, but not all. I have to run the macro again and then it deletes another bunch of them. So it seems not to be deleting all at once.
Can someone help me? Here's the Macro:
Sub course_reset()
Dim shp As Shape
For Each shp In Slide36.Shapes
If Left(shp.Name, 7) = "element" Then shp.Delete
Next
End Sub
The root cause of this is because of how iteration works. To illustrate:
Say you're at position 42. The object at this position meets the criteria for deletion.
You delete the object.
Object 43 is now object 42, 44 is 43, etc., all the way down the line, to close the gap.
You now move to position 43, and have skipped checking the old object 43.
The simplest way to fix this is to use a For loop starting at the end of the collection.
For i = Slide36.Shapes.Count To 1 Step -1
Then when you delete, you bypass the gap closure entirely.
SOLUTION
I just figured out by experimenting that if I create a variable as a ShapeRange, set it to Slide36.Slides.Range and then do the for each for the ShapeRange, it works! Thank you anyways for the quick reply.
So the code would be like this:
Sub course_reset()
Dim shp As Shape
Dim shprng As ShapeRange
Set shprng = Slide36.Shapes.Range
For Each shp In shprng
If Left(shp.Name, 7) = "element" Then shp.Delete
Next
End Sub

Run macro and hyperlink in one mouse click

I need a button in a PowerPoint slide that when I click it, it will, 1) run a macro and 2) hyperlink to another slide within the same presentation.
I can only see a way to do one or the other, not both at the same time.
My macro code is:
Sub question1_real()
Dim oSh As Shape
Dim oSl As Slide
Dim lScore As Long
' By doing it this way it's easy to change to a different slide if you
' need to later for some reason:
Set oSl = ActivePresentation.Slides(18)
' Change this if your shape is named something else:
Set oSh = oSl.Shapes("TextBox 2")
With oSh
' Make sure it's not blank to start with:
If Len(.TextFrame.TextRange.Text) = 0 Then
.TextFrame.TextRange.Text = "1"
End If
lScore = CLng(.TextFrame.TextRange.Text)
lScore = lScore + 1
.TextFrame.TextRange.Text = CStr(lScore)
End With
End Sub
My VBA skills are zero. The above code is borrowed from someone. I used the Insert Action option in PowerPoint to get it to work.
Assuming that you've given a shape a Run Macro action setting and chosen the subroutine you've posted above, you can add this function to the VBA project:
Sub JumpTo(lSlideIndex As Long)
SlideShowWindows(1).View.GoToSlide (lSlideIndex)
End Sub
Then wherever you want to jump to another slide, call it like so:
Call JumpTo(42) ' or whatever slide you want to jump to
or just
JumpTo 42
It's more convenient to have this in a Function if you need to use it more than once in the presentation. If it's strictly a one-shot, you can just paste this into your existing code:
SlideShowWindows(1).View.GoToSlide (42)

Select multiple objects with the same name

So I have a sheet with some pictures. All of these are just a copy paste of the original picture so they all have the same name "Flower".
I want to select all of them and flip them. But it will only select the original, how do I get past this? I tried incorporating a loop as well, without luck.
Sub Test()
ActiveSheet.Shapes.Range(Array("Flower")).Select
Selection.ShapeRange.Flip msoFlipHorizontal
End Sub
I know that I could simply rename them to Flower1, Flower2 etc. but the plan is to use this for a lot of pictures, so it would take to long to change manually. If it could all be done with a loop, that would be fine, but then I would still have the same problem as above.
You can loop thru the collection Shapes looking for the shapes with the given name. Try this.
Sub Test()
Dim Pic As Shape
For Each Pic In ActiveSheet.Shapes
If Pic.Name = "Flower" Then Pic.Flip msoFlipHorizontal
Next Pic
End Sub
In addition, you can code this without selecting it. Faster and more reliable.
There are two objects in VBA that could be useful - the shape collection named Shapes and the Shape itself. Thus, you can loop through the collection like this:
Sub Test()
Dim shShape As Shape
Dim shCollection As Shapes
Set shCollection = ActiveSheet.Shapes
For Each shShape In shCollection
shShape.Flip msoFlipHorizontal
Next shShape
End Sub

Moving video on every slide in powerpoint

I'm just poking around in VBA with PowerPoint and know that what I want to do can be done. I just don't know the write calls/sytanx to use!
I want a macro that will run through all the slides in a presentation and move the video object to specific spot on the slide. The spot will be the same on all slides.
It would be very helpful if someone could show me how to do this! Or at least point me in the right direction. Thanks!
Here's what I found for doing something on every slide
Sub EveryTextBoxOnSlide()
' Performs some operation on every shape that contains text on every slide
' (doesn't affect charts, tables, etc)
Dim oSh As Shape
Dim oSl As Slide
On Error GoTo ErrorHandler
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
With oSh
If .HasTextFrame Then
If .TextFrame.HasText Then
' If font size is mixed, don't touch the font size
If .TextFrame.TextRange.Font.Size > 0 Then
.TextFrame.TextRange.Font.Size = .TextFrame.TextRange.Font.Size + 2
End If
End If
End If
End With
Next ' shape
Next ' slide
NormalExit:
Exit Sub
ErrorHandler:
Resume Next
End Sub
And this is something I found to move the object to desired location (here's where I don't know what to call the video object)
With ActiveWindow.Selection.ShapeRange
.Left = 640 'change the number for desired x position
.Top = 75 'change the number for desired y position
End With
End Sub
Sub ll()
End Sub
So basically, I want to run the last piece as a function of the first one instead of shapes with text. Does that make sense?
I've done some programming in the past, mainly with actionscript and Flash. I could probably write out some basic functions, just not sure how to run it in VBA without learning a whole new language. Which I don't want to do, since I'm an instructional designer and don't have the free time to learn it! :)
Here's a little function that'll return True if the shape you pass to it is a video, and a bit of example code to test it with:
Function IsVideo(oSh As Shape) As Boolean
If oSh.Type = msoMedia Then
If oSh.MediaType = ppMediaTypeMovie Then
IsVideo = True
Exit Function
End If
End If
' Things get a little trickier if the movie is in a placeholder
' Is it a placeholder? and is it a media object?
If oSh.Type = msoPlaceholder Then
If oSh.PlaceholderFormat.ContainedType = msoMedia Then
' it's a media object, but we don't know if it's
' a movie or sound or what, so we duplicate it
' then look at the duplicate (which is now a copy
' of the placeholder content but is not a placeholder itself)
With oSh.Duplicate
If .Type = msoMedia Then
If .MediaType = ppMediaTypeMovie Then
IsVideo = True
End If
End If
' and delete the duplicate
.Delete
End With
End If
End If
End Function
Sub thing()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If IsVideo(oSh) Then
oSh.Left = 0
End If
Next
Next
End Sub
Now you're getting somewhere!
I don't know what kind of shape holds your video, so this modification should help you identify it.
Sub EveryTextBoxOnSlide()
Dim oSh As Shape
Dim oSl As Slide
On Error GoTo ErrorHandler
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
With oSh
.Select
End With
Next
Next
NormalExit:
Exit Sub
ErrorHandler:
Resume Next
End Sub
While there is no need whatsoever to use the .Select in your final code, this is only here to help you identify which shape actually holds the video. Put a breakpoint (F9) on that line and run your code (F5), then debug through it with F8 to execute one line at a time and run through your loop looking at each shape until you see that your video has been selected.
Once your video has been selected look at the various properties (using the Immediate Window of the IDE) of oSh until you find the uniqueness that identifies this is your video. (There may be some property about the content type of the shape, or a link on where to find the contents that contains the text string ".avi", ".mpg", ".flv", etc - there will be some sort of identifier you can find.)
Once you've identified what makes the shape the holder of a video, replace
.Select
with
If {my video containing shape criteria is true} Then
With .Selection.ShapeRange
.Left = 640 'change the number for desired x position
.Top = 75 'change the number for desired y position
End With
End If
Changing the values (as commented) to whatever it is that you need.
Important Note: It is very good that your sample code has some error handling built in, but all this error handler is doing is sweeping any errors under the rug. This particular handler is the very long form version of On Error Resume Next which does have its uses, but in very, very limited situations.
As you get your code working, you'll actually want to put something useful in the ErrorHandler: section, but that's the topic for a whole new question.
Ok, here is how I modified the code supplied above to do this in my case:
Sub EveryTextBoxOnSlide() ' Performs some operation on every shape that contains text on every slide ' (doesn't affect charts, tables, etc) Dim oSh As Shape Dim oSl As Slide On Error GoTo ErrorHandler
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
With oSh
If .Type = msoMedia Then
If .MediaType = ppMediaTypeMovie Then
.PictureFormat.Crop.PictureHeight = 236
.PictureFormat.Crop.PictureWidth = 314.2115
.PictureFormat.Crop.PictureOffsetX = 8.737323
.PictureFormat.Crop.PictureOffsetY = 0
.PictureFormat.Crop.ShapeHeight = 236.3478
.PictureFormat.Crop.ShapeWidth = 163
.PictureFormat.Crop.ShapeLeft = 796.6956
.PictureFormat.Crop.ShapeTop = 0
End If
End If
End With
Next ' shape Next ' slide
NormalExit: Exit Sub
ErrorHandler: Resume Next
End Sub

Counting Shapes

What I'm trying to do here is give all the shapes in the worksheet a number.
The number must be counted up by one for each shape
So it should look something like this.
Keep in mind that these shapes are dynamic (it isn't always 2 columns, 3 rows)
This is the code i tried, but doesn't seem to work
Sub Nummer()
Dim lngShapes As Long
lngShapes = ActiveSheet.Shapes.Count
For teller = 1 To lngShapes
ActiveSheet.Shapes(teller).Value = teller
Next
End Sub
It seems like the question is actually "Loop through all of the shapes in a sheet". Something like the following should be used:
Sub nameTheShapes()
Dim shp As Shape
For Each shp In Sheet1.Shapes
shp.TextFrame2.TextRange.Characters.Text = shp.ID
Next
End Sub
Here we declare a shape as variable name shp. Then we loop through all of the shapes in the sheet1. Then we write the shape's id into the shape's textframe2.
If you have some charts hanging out on the same page, then you probably don't want to include them, so you can test the shp.type property to see that it's not a chart:
Sub nameTheShapes()
Dim shp As Shape
For Each shp In Sheet1.Shapes
If shp.type = 1 Then
shp.TextFrame2.TextRange.Characters.Text = shp.ID
End If
Next
End Sub
You get Error 438 when trying to set the value (text) within the shape because the shapes collection does not have a Value property to set. You need to use .TextFrame.Characters.Text
If you change the line
ActiveSheet.Shapes(teller).Value = teller
to
ActiveSheet.Shapes(teller).TextFrame.Characters.Text = teller
your code will work.