How to access slides from a VBA addin

  • Thread starter Thread starter peterfarge
  • Start date Start date
P

peterfarge

Hallo Usenet,

I have created an AddIn with a commandbar (toolbar) and want to add my
own icons. The problem is that I dont know how to access a slide while
my project is saved as an addin. On the slide lies my icon...

This is the code I'm using to add the toolbar:
Public Sub auto_load()
Dim oComBar As CommandBar
Dim oComBarButton As CommandBarButton

On Error Resume Next
CommandBars("222").Delete
Set oComBar = Application.CommandBars.Add("222")

Set oComBarButton = oComBar.Controls.Add(msoControlButton)
oComBarButton.Style = msoButtonIcon
' Here is the problem:
Presentations("My_Presentation").Slides(1).Shapes("My_Icon").Copy
oComBarButton.PasteFace
oComBar.Visible = True
End Sub

If my project is loaded as an addin, than there is no Presentation
named "My_Presentation". And Application.AddIns(1) has no slides
property. Can anyone help me?


Peter
 
If my project is loaded as an addin, than there is no Presentation
named "My_Presentation". And Application.AddIns(1) has no slides
property. Can anyone help me?

To add to what Chirag's mentioned, you'll need some other means of getting the
button icon on the clipboard. One approach is to ship it with your add-in as a
graphic file; when creating your toolbar, open a new presentation, import the
graphic, copy it, paste into your toolbar, then close the presentation.

If you open the presentation w/o a window, this will all be invisible to the
user.

Consider *not* deleting the existing toolbar at each startup, also.
PPT will start more quickly if it doesn't have to wait for you to rebuild your
toolbar at each startup.

Also, if your users don't like where your toolbar appears, they'll move it. If
you delete it every time you start, you also throw away their customizations to
it. They won't be amused. ;-)
 
A workaround is the following: Place for every icon a imagebox on a
form and place them into the commanbar with the picture property of the
commandbarbutton.
 
A workaround is the following: Place for every icon a imagebox on a
form and place them into the commanbar with the picture property of the
commandbarbutton.

Oh, nice! Could you post some example code for that? And may I include it on
the PPT FAQ (url below)?

Thanks!
 
Ok, here is the example code:

1. Create a form "UserForm1" with an image control named "Image1"
2. Load a picture in the image control.
3. Create a modul with this 3 functions:

Public Sub CreateCommandBar()
Dim oComBar As CommandBar
Const COMMANDBAR_NAME As String = "Test toolbar"

' Delete old toolbar:
For Each oComBar In CommandBars
If oComBar.Name = COMMANDBAR_NAME Then
oComBar.Delete
End If
Next oComBar

' Create new toolbar:
Set oComBar = Application.CommandBars.Add(COMMANDBAR_NAME)
Call AddButton(oComBar, "Button Caption",
"CMDBAR_FunctionDoSomething", UserForm1.Image1.Picture, True)
oComBar.Visible = True
End Sub

Private Sub AddButton(oComBar As CommandBar, sButtonName As String,
sMakroName As String, oIcon As IPictureDisp, bBeginGroup As Boolean)
Dim oComBarButton As CommandBarButton

Set oComBarButton = oComBar.Controls.Add(msoControlButton)
With oComBarButton
.Caption = sButtonName
.OnAction = sMakroName
.BeginGroup = bBeginGroup
.Style = msoButtonIcon
.Picture = oIcon
End With
End Sub

Public Sub CMDBAR_FunctionDoSomething()
MsgBox "Hello toolbar"
End Sub
 
Back
Top