Programming to the vbe

  • Thread starter Thread starter dave.cuthill
  • Start date Start date
Is it possible to program to the vbe in powerpoint much like described
in the excel related site indicated below? Is so can someone please
point me to how it can be done.

http://www.cpearson.com/excel/vbe.htm

From the very little I've played with it in PPT, it'd be pretty much the same
(same VBE, after all, only the host application changes).

Have you tried the examples on Chip's site in PPT?
 
I played with it a little after I posted this and was able to create a
module. But I really need to be able to put something behind a slide to
be able to add some code to an activex scroll bar that it add with
code. How do you place code in a Powerpoint slide object?
 
Dave,
You perfrom the same operations as Excel. One point to note is that a slide
object does not appear in the project explorer unless there is an activex
control on that slide. Once the control is on the slide, you can enumerate
it and insert code within that object's module.


' Assumes that a slide object exists with an activex control on it and the
1st object in the project
' explorer is that object.
Call ActivePresentation.VBProject.VBComponents(1).CodeModule.InsertLines(1,
"Option Explicit")
 
I played with it a little after I posted this and was able to create a
module. But I really need to be able to put something behind a slide to
be able to add some code to an activex scroll bar that it add with
code. How do you place code in a Powerpoint slide object?

Let's back up and look at what you need to accomplish. Maybe there's a more
direct way of getting there.
 
I am using code to insert a picture, then a scroll bar - I then want to
be able to place code in the slide for the scroll bar change and scroll
events. The scroll bar value then controls the top position of the
added picture.

I sort of figured out crude way to do this but it am open to any
suggestions.

Here is what I have so far. This is all "hard coded" - ultimately I
want it to prompt for a file name and then place the picture and
scrollbar on the active slide and then place the code in the active
slide to control the scrollbar.

Sub InsertGraphicOnSlide()

Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppShape As PowerPoint.Shape
Dim ppCurrentSlide As PowerPoint.Slide

Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = True

'Set ppPres = ppApp.Presentations.Add(msoTrue)
'Set ppCurrentSlide = ppPres.Slides.Add(Index:=1, _
Layout:=ppLayoutBlank)
'Set ppCurrentSlide = ActiveWindow.Selection.SlideRange
With ActiveWindow.Selection.SlideRange.Shapes
Set opicture = .AddPicture("c:\Picture1.png", _
msoFalse, msoTrue, 0, 0, 1, 1)
opicture.ScaleHeight 1, msoTrue
opicture.ScaleWidth 1, msoTrue
opicture.Name = "Graphic_1"
End With

ActiveWindow.Selection.SlideRange.Shapes.AddOLEObject _
(Left:=618#, Top:=156#, Width:=12#, Height:=294#,
ClassName:="Forms.ScrollBar.1", Link:=msoFalse).Select


With ActivePresentation.VBProject.VBComponents(Slide1).CodeModule
.InsertLines 1, _
"Private Sub ScrollBar1_change()" & Chr(13) & _
"ScrollBar1.SmallChange = 1" & Chr(13) & _
"ScrollBar1.Max = 100" & Chr(13) & _
"ScrollBar1.Min = 0" & Chr(13) & _
"Ht = Shapes(""Graphic_1"").Height" & Chr(13) & _
"Increment = 77.04 + ((496.8 - (Ht + 77.04)) * ScrollBar1.Value /
ScrollBar1.Max)" & Chr(13) & _
"'increment = 48 + (-1.2762 * ScrollBar1.Value)" & Chr(13) & _
"ActivePresentation.SlideShowWindow.View.Slide.Shapes(""Graphic_1"").Top
= Increment" & Chr(13) & _
"End Sub"
End With

End Sub
 
I am using code to insert a picture, then a scroll bar - I then want to
be able to place code in the slide for the scroll bar change and scroll
events. The scroll bar value then controls the top position of the
added picture.

I sort of figured out crude way to do this but it am open to any
suggestions.

Thanks for filling in the background ... and I see you've already seen Shyam's
post.
Here is what I have so far. This is all "hard coded" - ultimately I
want it to prompt for a file name and then place the picture and
scrollbar on the active slide and then place the code in the active
slide to control the scrollbar.

Getting the filename from the user shouldn't be a problem for you, is that
correct?

A couple of suggestions, though:
Sub InsertGraphicOnSlide()

Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppShape As PowerPoint.Shape
Dim ppCurrentSlide As PowerPoint.Slide

It's cleaner if you don't .Select anything so create a var to hold an object
reference to your scrollbar

Dim oScroll as PowerPoint.Scrollbar
Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = True

'Set ppPres = ppApp.Presentations.Add(msoTrue)
'Set ppCurrentSlide = ppPres.Slides.Add(Index:=1, _
Layout:=ppLayoutBlank)
'Set ppCurrentSlide = ActiveWindow.Selection.SlideRange
With ActiveWindow.Selection.SlideRange.Shapes
Set opicture = .AddPicture("c:\Picture1.png", _
msoFalse, msoTrue, 0, 0, 1, 1)
opicture.ScaleHeight 1, msoTrue
opicture.ScaleWidth 1, msoTrue
opicture.Name = "Graphic_1"
End With

ActiveWindow.Selection.SlideRange.Shapes.AddOLEObject _
(Left:=618#, Top:=156#, Width:=12#, Height:=294#,
ClassName:="Forms.ScrollBar.1", Link:=msoFalse).Select

' Get a reference to the shape as you create it rather than selecting it
Set oScroll =ActiveWindow.Selection.SlideRange.Shapes.AddOLEObject _
(Left:=618#, Top:=156#, Width:=12#, Height:=294#,
ClassName:="Forms.ScrollBar.1", Link:=msoFalse)

' I think you could use oScroll.Name here in place of ScrollBar1
' and simplify the code by having it output eg

With ScrollBar1
.SmallChange =
.Max =
.Etc
End With

I think that you could also use:

With Me.Shapes("Graphic_1")
' or even Me.Shapes.oPicture.Name if you want to generalize it further
.Top
.etc
End With
 
You are correct that I know how to capture the desired picture file.
I'll play around with your suggestions but I did have one other
question that may help further (but I'm not really sure). How do you
determine what name Powerpoint gives to the picture file as it is
inserted. It almost seems as though it has a number of different names
depending on how you are manipulating it. If it is in the custom
animation area it seems to have the name that it was saved as and if
you try to manipulate it with vb is goes by "Picture X".

If rely on Powerpoint to name the picture how do I place this name in
the code module insertion area since it will vary or am I better to
preselect the name as I am now doing? If the user wanted to insert 2
pictures I would need to have the code use the name assigned by
Powerpoint so as not to confuse the scrollbar code or I guess I could
check for the existance of a picture and scrollbar combination prior to
inserting the next picture/scrollbar and then increment the name of the
scrollbar and the picture.

Thanks for your help so far.

David
 
Steve:

I am getting a user defined type not defined for Dim oScroll As
PowerPoint.ScrollBar do I need to register something to get this to
work?

David
 
Steve:

I am getting a user defined type not defined for Dim oScroll As
PowerPoint.ScrollBar do I need to register something to get this to
work?

OK, duped it here. Apparently the act of adding a scroll bar to a slide sets
the necessary reference to

Microsoft Forms 2.0 Object Library

You could either set that manually (probably the best bet) or Dim oScroll as
Object

We're both learning here and both bumping into walls and furniture in the dark.
 
You are correct that I know how to capture the desired picture file.
I'll play around with your suggestions but I did have one other
question that may help further (but I'm not really sure). How do you
determine what name Powerpoint gives to the picture file as it is
inserted. It almost seems as though it has a number of different names
depending on how you are manipulating it. If it is in the custom
animation area it seems to have the name that it was saved as and if
you try to manipulate it with vb is goes by "Picture X".

This is serious weirdness, yes.
When you insert a picture or create any other shape, PPT assigns it a name,
which is not alterable (and which you see in the animation window). The shape
also has a .Name property accessible programmatically. This is set initially
to the same value as PPT assigned the shape but you can change it.
If rely on Powerpoint to name the picture how do I place this name in
the code module insertion area since it will vary or am I better to
preselect the name as I am now doing?

You're already doing the right thing ... name the shape as you create it so you
can later select it by name as needed.
If the user wanted to insert 2
pictures I would need to have the code use the name assigned by
Powerpoint so as not to confuse the scrollbar code or I guess I could
check for the existance of a picture and scrollbar combination prior to
inserting the next picture/scrollbar and then increment the name of the
scrollbar and the picture.

Exactly. And one to watch out for: you can't give two shapes on a slide the
same .Name, but if the user duplicates your shape, PPT may give both shapes the
same .Name property.
 
I had it manually set in the references but was still getting the
message. Setting it to an object seems to work fine.

At least you seem to have a better idea where the walls and furniture
are located.

David
 
I had it manually set in the references but was still getting the
message. Setting it to an object seems to work fine.

At least you seem to have a better idea where the walls and furniture
are located.

More years spent stumbling around with the lights out.
The story of my dimwit life. <g>
 
Okay so I think I now have it working the way I want it (still some
refinement needed) - but now when I try to save it as an addin I get a
message stating that it can't be saved. Initially it was giving me a
message about an activex item being on the slide which there wasn't and
now it just plain says it can't be saved as an addin with no
explanation.

Any ideas?

David
 
Okay so I think I now have it working the way I want it (still some
refinement needed) - but now when I try to save it as an addin I get a
message stating that it can't be saved. Initially it was giving me a
message about an activex item being on the slide which there wasn't and
now it just plain says it can't be saved as an addin with no
explanation.

Usually that's because it can't compile the code. Try choosing Debug, Compile
VBA Project from the main menu of the IDE. That should point you at the
line(s) it's having trouble with.
 
Thanks that did the trick - I had some code that I wasn't using that
wasn't compileable.

Thanks for all you help.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top