Need button on Powerpoint slide that uses VBA to trigger "Insert Object" command

I'm making a presentation template for my team. What I need is a button on the slide that, when clicked, triggers the "Insert Object" command from the Insert menu. Powerpoint 2010 doesn't have the record macro option the way Excel does, so I can't just record the action and modify the VBA script from there, as I would in Excel.

This is only necessary because the built-in content placeholders don't offer that capability - even the most general Content placeholder only has buttons to insert tables, charts, smartart, images, clip art, and media/videos. But the users of the final presentation template will need a simple, user-friendly way to click and embed their auxiliary files, which are generally Excel files, in the placeholders.

So I need the VBA command corresponding to the "Insert Object" menu command so I can use it as a triggered script on my slide. I did some searches but the keywords are too general and I couldn't find what I needed. Or, if there's a better way to get the same result, I'd be interested to hear it.

Thanks for any help.
LVL 12
telyni19Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harry LeeCommented:
Try the following code for your command button to see if this is what you need.

Private Sub CommandButton1_Click()
Dim sld As Slide, shp As Shape, SlideIndex As Long, In_file As Variant

Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
dlgOpen.AllowMultiSelect = False
If dlgOpen.Show = -1 Then
In_file = dlgOpen.SelectedItems.Item(1)
End If

SlideIndex = ActiveWindow.View.Slide.SlideIndex

Set sld = ActivePresentation.Slides(SlideIndex)
Set shp = sld.Shapes.AddOLEObject(FileName:=In_file, _
            Left:=0, _
            Top:=0, _
            Width:=ActivePresentation.PageSetup.SlideWidth, _
            Height:=ActivePresentation.PageSetup.SlideHeight)

End Sub

Open in new window

0
JSRWilsonCommented:
If you mean litterally open the Insert Object Dilaogue from code? You can simulate a click on that button: (I'm guessing you mean more though)

Sub Iobj()
CommandBars.ExecuteMso ("OleObjectctInsert")
End Sub

(That's NOT a spelling mistake)
0
telyni19Author Commented:
Thanks, JSRWilson's got it. That's what I needed.

It's not quite working yet, though, because nothing happens when I click on the command button on the slide; it only triggers the code when I'm in slideshow mode. I tried this with the command button both on the master slide (where it needs to be) and on the regular slide. With the command button on the master slide, it doesn't respond at all, and with it on the regular slide, it will be selected when I click on it, but the code doesn't run. Same thing with a macro action attached to a shape. It doesn't run when I click on the shape unless the presentation is in slideshow mode. Which is not where people embedding objects are generally going to be.

Is there any way to get a one-click trigger on the slide for the Insert Object command (or any menu command or macro, really)?

(Harry, it looks like your code should work in theory, but since I have to run it in slideshow mode, I get a run-time error that says "Application (unknown member): Invalid request. There is no currently active document window." A screenshot of the error is attached. Plus the one-line menu trigger is much simpler.)
RTE-Invalid.jpg
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

JSRWilsonCommented:
Buttons will ONLY fire in slide show mode.

Possible options are add the command to the ribbon in a more convenient position

Repurpose a command on the slide buttons you don't use eg Clip Art > Object. This is not simple but can be done. Not sure it would actually add an object to the placeholder though.
0
telyni19Author Commented:
I'd love to have the clip art icon work for objects instead. We almost never use clip art. Do you have any more information on how not-simple that is? I was beginning to think what I wanted just isn't possible, except for the fact that the placeholders do support one-click interaction in editing mode.
0
Harry LeeCommented:
Although this is not exactly what you need, since Command Button only works in slide mode.

Just to correct my code,

Private Sub CommandButton1_Click()
Dim sld As Slide, shp As Shape, In_file As Variant

Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
dlgOpen.AllowMultiSelect = False
If dlgOpen.Show = -1 Then
In_file = dlgOpen.SelectedItems.Item(1)
End If

Set sld = ActiveWindow.View.Slide
Set shp = sld.Shapes.AddOLEObject(FileName:=In_file, _
            Left:=0, _
            Top:=0, _
            Width:=ActivePresentation.PageSetup.SlideWidth, _
            Height:=ActivePresentation.PageSetup.SlideHeight)

End Sub

Open in new window

0
JSRWilsonCommented:
You will need to know how to access and add XML code.

There are some basic intructions on our site.
http://www.pptalchemy.co.uk/custom_UI.html

I have uploaded a repurposed presentation Test by clicking "Clip Art" on slide 2.

You should be able to look at the XML using the CustomUI Editor found on the other link and see how it calls the vba code.

I haven't really tested to see if it works as a template! I never recommend using macro enabled templates with my clients but only AddIns that control how they are used. Microsoft totally screwed up how potm files work especially if you use them to create ribbon entries!

I'm happy to help here for free but I can't talk you through how to make an addIn to do this. For your info creating Addins that do this sort of thing is what we do for a living!

And I know you are going to ask! NO you cannot change the text or image for Insert Clip Art!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
telyni19Author Commented:
Thanks for the help, both of you. The repurposed presentation is perfect, except for the button label still saying Clip Art, of course. :) It might do, though; I'll have to play with it a bit. Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft PowerPoint

From novice to tech pro — start learning today.