Link to home
Start Free TrialLog in
Avatar of telyni19
telyni19Flag for United States of America

asked on

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.
Avatar of Harry Lee
Harry Lee
Flag of Canada image

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

SOLUTION
Avatar of John Wilson
John Wilson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of telyni19

ASKER

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
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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!