macro to delete slides

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to create a macro to delete slides on a daily basis to put "new"
slides in. Any help appreciated.
 
I think you'll have to be more specific than that. The following line
of code will delete slide 3 (change the 3 to anything else to delete a
different slide).

ActivePresentation.Slides(3).Delete

Be more specific, and perhaps, someone here will be able to better help
you.

--David

--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
Thank you for your reply. To be more specific I need to go in and delete 78
slides a day to input 78 new slides. Is there a way to do this without coding
each slide to be deleted? Thanks!!!
 
How will you know which 78 slides need to be deleted? Is it all of them?
Is it all from 2 through 79? Is it even-numbered slides? Is there a
reason that you can't just go to slide sorter view an select the slides
you want and hit the Delete key? As long as you can tell the code which
slides to delete, it can do the job.

Also, do you really want to delete the slides or just change the content?

--David

--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
David, Actually I only want to delete the "content" in the slides. I am sorry
I didnt make my self clear. I am fairly new at this. How do I do this in a
command button & where can I put it for the user to just click it everyday to
delete out the content of the 78 slides? Thanks so much for your time.
 
This might sound like a silly question, but what do you mean by content?
Is it just text? Is it text and pictures? Is it text, pictures, and
sounds? Does it include any background colors? What about buttons? Do
you have buttons? Will they have text that you don't want deleted?

A procedure like the following could be used to delete all the text on
all the slides except the first (leaving the boxes that the text was in):

Sub DeleteText()
Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
If sld.SlideIndex <> 1 Then
For Each shp In sld.Shapes
If shp.HasTextFrame Then
shp.TextFrame.TextRange.Text = ""
End If
Next shp
End If
Next sld
End Sub

Minor adjustments could be made to do other things (like skip buttons
with hyperlinks, to do the first slide as well, to skip any shape that
has a blue background, to do the first 78 slides only and not anything
beyond that).

You can run this in a number of ways. You could just paste this code
into a new module and choose Tools->Macro>Macros and pick the Delete Text
macro. You could add a button to your first slide that is has the Action
Setting to run the Delete Slide macro and press that button while in
slide show view.

This seems to me to be what you have asked for so far. If it's not,
we'll need more details.

--David

--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/

David, Actually I only want to delete the "content" in the slides. I
am sorry I didnt make my self clear. I am fairly new at this. How do I
do this in a command button & where can I put it for the user to just
click it everyday to delete out the content of the 78 slides? Thanks
so much for your time.
 
David, Thanks again for posting. No, it isnt a silly question! Here goes..ALL
I need is the pictures to be deleted, not the text. This is for an animal
shelter, and each day we post new pictures of new animals we get daily. The
text will be the same as it is the name and number of the "pen" the animal
resides in.
So all I want to do is delete the pictures from 2 to 78. Our first slide
will remain the same.
I tried the macro instructions you sent but did not get anywhere with that.
I will need to know where to put the code and how a user can click on a
"button" I suppose to delete the pictures --Make sense? thanks for all your
help so far!!
 
OK. Now, we are getting somewhere. Try this:

Sub DeletePix()
Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
If sld.SlideIndex <> 1 Then
For Each shp In sld.Shapes
If shp.Type = msoPicture Then
shp.Delete
End If
Next shp
End If
Next sld
End Sub

It will delete all pictures on all slides other than the first slide.
However, it will only delete pictures that are by themselves on the slide
(not inside text boxes or in placeholders). These would be pictures that
you added with something like Insert->Picture->From File.

To use this, you should start your PowerPoint presentation, go to the
Tools menu and choose Macro and Visual Basic Editor. Choose Module from
the Insert Menu. Paste the text procedure above (starting with Sub and
ending with End Sub) into the module. Close the Visual Basic Editor.
Now you should be in your current PowerPoint presentation. You can
either go to the Tools menu, choose Macro, and Run Macro and pick the
DeletePix macro to run; or you can create a button on your first slide
and set the Action Settings of the button (it can be a regular button or
any shape that you draw with the drawing tools; right click and choose
Action Settings to get to the Action Settings menu) to Run Macro and pick
the DeletePix macro.

Two important things to remember:

(1) If you want to run the macro with a button, you must be in Slide Show
view, not Normal/Edit view.
(2) You must have your macro security set to medium or low. Before
adding the macro, go to the Tools menu, choose options, click on the
Security tab, and click on the macro security button. Choose medium or
low.

That should do it. Try it out and let us know if it works for you.

--David

--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
David, I really thought that would get it. I copied and pasted the code you
sent, did exactly what you told me to do in your instructions. But it is not
working.
The pictures are on a slide input from a file. There is a text box, but it
is above the picture. The picture itself is not in a text box or place holder.
I am not running it from a button, I just clicked on the macro and RUN.
It just isnt working. I dont know what I am doing wrong. I am at a
standstill now. If you can suggest anything please advise.
 
Sheryl,

Try the following code:

Sub SayHello()
MsgBox "Hello"
End Sub

When you run this macro, you should just get a little box that says "Hello"
and lets you hit OK to close the box. If you can't get that to work, then
you need to check your macro security settings (be sure that you quit out of
PowerPoint and start up the presentation again after you change the
security). If it still doesn't work, we'll have to figure out what else you
are doing wrong.

If the SayHello procedure does work, then you are probably doing everything
right, but my code isn't designed to handle something in your presentation.
If that is the case, you can send me your presentation, and I'll see if I can
get it to work (tomorrow when I get back to my office). You can send it to
me at (e-mail address removed) (just remove NOSPAM). You can send it as a
regular PowerPoint file, or you can send it as a ZIP file. However, if you
send it as a ZIP file, change the extension from .zip to .zp so my SPAM
filter doesn't block it.

Also, if you send me email, I'll send a little sample that works.

--David

David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
David, Thanks again for posting. No, it isnt a silly question! Here goes..ALL
I need is the pictures to be deleted, not the text. This is for an animal
shelter, and each day we post new pictures of new animals we get daily. The
text will be the same as it is the name and number of the "pen" the animal
resides in.
So all I want to do is delete the pictures from 2 to 78. Our first slide
will remain the same.

Ah, NOW I get it.

Have a look at http://merge.pptools.com

If you were to put the names, numbers and the name of the file the picture's in
into rows of a spreadsheet, Merge could be set up to generate you a new PPT file
in a matter of a minute or so.
 
OK. I received the file through email. The difficulty is that the
pictures are inserted into placeholders. I sent back email to ask for
more details. The PPT Merge tool might be the right solution for this or
just a bit of code might work fine. I've asked them for details about
how the pictures are saved and how code will know what pictures to
insert.

Steve, I know how to identify if a shape is a picture (shp.Type =
msoPicture) or if a shape is a placeholder (shp.Type = msoPlaceholder),
but I don't know how to identify if a shape is a placeholder that
contains a picture. Any suggestions?

--David

--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/

=?Utf-8?B?RGF2aWQgTS4gTWFyY292aXR6?=
 
Steve, I know how to identify if a shape is a picture (shp.Type =
msoPicture) or if a shape is a placeholder (shp.Type = msoPlaceholder),
but I don't know how to identify if a shape is a placeholder that
contains a picture. Any suggestions?

This logic seems to work:

If it's a placeholder
Copy it
Is copiedshape.type = msoPicture?
If so, the placeholder contained a picture
 
Thanks, Steve. I'll try to remember that. We got past the placeholder issue
because not only did she want to delete 78 pictures, she also wanted to add
78 new pictures. Once we started using VBA to add the pictures, the
placeholders were unnecessary because the placeholders were only being used
to make sure the pictures showed up in the same place. VBA does that just
fine.

Further, the new pictures were consecutively-numbered pictures from a
digital camera, so it was really easy to add them all with a simple For Next
loop. I'm working out the details with her now. My simple procedures worked
for one set of pictures because they were numbered starting at 1,and then she
told me that the pictures don't always start at 1. I'm waiting to hear back
from her as to how she wants to handle this based on how much the picture
numbering is going to vary. Since this is a quick and dirty solution, I
figure the easiest thing is to use an InputBox to ask for the starting number
of the pictures and the ending number of the pictures.

I'll try to remember to post the code here when we're done, but it's nothing
fancy. I don't do any error-checking or anything interesting, but apparently
they were spending 3 hours a day on this task, and this little
quick-and-dirty code will save them most of that time.

--David

David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
Thanks for jumping in to help with this one. Sounds like it's well under
control.

Don't you just LOVE being able to shave a 3-hour job down to 30 seconds?
 
OK. I think I have solved their problem. Deleting all the pictures
(that aren't in placeholders) was easy:

Sub DeletePix()
Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
If sld.SlideIndex <> 1 Then
For Each shp In sld.Shapes
If shp.Type = msoPicture Then
shp.Delete
End If
Next shp
End If
Next sld
End Sub

The tough part was figuring out what they wanted to do with adding the
pictures. They have 78 pens for dogs, 37 pens for cats, and 4 pens for
rabbits. They take pictures of each pen with a digital camera every day
(whether or not an animal is actually in the pen). The pictures are
generally numbered consecutively (from 1 to 78 for the dogs) with the
date prefixed to the number in 3-digit format. However, when a picture
doesn't come out, a picture might be missing in the middle; for example,
they might be numbered from 1 to 79 with 32 missing. Furthermore, the
dog pictures might start at number 1, while the cat pictures might start
at number 89, and the rabbit pictures might start at number 143, so they
need the code to ask where to start. The only thing the code doesn't do
(which wouldn't be that hard, but they didn't ask) is to ask which animal
or how many pictures there are, so they have to change the number in the
code for each animal. I assume they'll make their own procedures for
each number (AddDogs, AddCats, AddRabbits), but that is up to them. Here
is the code that does all this (watch out for line breaks caused by
wrapping):

Sub AddPixAsk()
Dim sld As Slide
Dim slideNumber As Long
Dim picNumber As Long
Dim picNumberTxt As String
Dim pixFile As String
Dim theDate As String

'Change 78 to the number of pictures (78 for dogs, 37 for cats, 4 for
rabbits)
Const numberOfPix As Long = 78

theDate = InputBox("What is today's date (use the form MMDDYY)?")
picNumberTxt = InputBox("What is the number of the first picture?")

On Error GoTo BadNumber
picNumber = CLng(picNumberTxt)
On Error GoTo 0

If picNumber < 0 Or picNumber > 999 Then
picNumber = 0
MsgBox "That wasn't a valid number. We'll just start from picture
#1"
Else
picNumber = picNumber - 1 'We add one to it at the beginning of
the loop
End If

For slideNumber = 2 To numberOfPix + 1
SetUpFileName:
picNumber = picNumber + 1
If picNumber < 10 Then
pixFile = theDate & " 00" & picNumber & ".jpg"
ElseIf picNumber < 100 Then
pixFile = theDate & " 0" & picNumber & ".jpg"
Else
pixFile = theDate & " " & picNumber & ".jpg"
End If
On Error GoTo MissingPicture
ActivePresentation.Slides(slideNumber).Shapes.AddPicture _
FileName:=pixFile, LinkToFile:=msoFalse, SaveWithDocument:
=msoTrue, _
Left:=220, Top:=220
On Error GoTo 0
Next slideNumber
Exit Sub
MissingPicture:
If picNumber < 999 Then
Resume SetUpFileName
Else
MsgBox "You seem to have run out of pictures."
End If
Exit Sub
BadNumber:
picNumber = -1
Resume Next
End Sub


--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
David M. said:
OK. I think I have solved their problem. Deleting all the pictures
(that aren't in placeholders) was easy:

Sub DeletePix()
Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
If sld.SlideIndex <> 1 Then
For Each shp In sld.Shapes
If shp.Type = msoPicture Then
shp.Delete
End If
Next shp
End If
Next sld
End Sub

That may not always work; try it with, say, five pictures on a slide, all added
one after another.

Instead, try

For X = sld.shapes.count to 1 step -1
If sld.shapes(x).Type = etc etc etc
 
That may not always work; try it with, say, five pictures on a slide,
all added one after another.

Instead, try

For X = sld.shapes.count to 1 step -1
If sld.shapes(x).Type = etc etc etc

Ah! That makes sense. Fortunately, they only have one picture per slide
so it's not a problem, but it makes sense to count the shapes backward.

--David


--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
Back
Top