How to include predesigned shapes into an Add-in?

L

Lars-Åke Aspelin

Powerpoint 2003.

I have an Add-in with an Auto_open() sub that creates a custom toolbar
with some buttons that fire different macros that is included in the
Add-in.

One macro moves around existing shapes (images) in the ppt
presentation and that works fine as long as the shapes are there. But
if the user has deleted a shape from the presentation, the macro will
fail.

What I would like to do is to incorporate some shapes (images) in the
Add-in (.ppa) and make them available to the Add-in macro so it can
copy the shapes into the presentation when needed.

Appreciate any advice on how to accomplish this.

Lars-Åke
 
L

Lars-Åke Aspelin

I'll fill in the general outlines; if you need details on how to make it
work, let us know.

I would tag the shapes that you want to work with so that they're easy
for your macro to find. I'd also write a little function that hands
back a reference to the shape with the tag you're after. I already did,
in fact. It's here:

Working with Tags (and a bit about Functions)
http://www.pptfaq.com/FAQ00815.htm

There's also an example of how to use the function which includes a test
to see if the function returned nothing ... ie the requested shape
wasn't there.

If it's not there, your code could add the needed image to the slide.
You could store the images along with the add-in itself and include a
bit more code to get the correct path to the images, basically just:

Addins("your_addin_name").Path

Storing the images within the add-in itself would be more complicated.

You might do something like this:

Write a routine that reads the data from an image file as binary then
writes out the value of each byte as a string to a file.

Open the file in notepad and paste it into something like this:

Dim sFileData as String

sFileData="4298374058372.....2748503"
where the numbers represent the ascii values of the bytes in your
images.

To use this, you'd convert the data back to binary and write it to a
temp file, import the temp file into PPT, then delete it.

I think the editor may impose limits on how much data you can tack onto
a string in this way, so you might need to break it into chunks and
handle it as an array of strings or the like.



==============================
PPT Frequently Asked Questions
http://www.pptfaq.com/

PPTools add-ins for PowerPoint
http://www.pptools.com/

Thanks for you suggestions.

The refering to shapes in the ppt is not a problem. Tags seem
convenient, but as I don't have a need for more than one "tag" per
shape I guess the use of "index by name" for refering a shape works
well.

I have tried the proposal with image file(s) along, i.e. in the same
directory as, the Addin, but it would be very nice to have everything
in the .ppa file in order to eliminate the risk of these extra files
being lost.

As I can have images in user forms that are "embedded" in the Add-in
(.ppa-file) itself, I was kind of hoping that I could reference these
images by
<something>.Userform1.Image1.Picture without having to use the low
level binary alternative that you suggested.

Any ideas on what <something> could be?

Lars-Åke
 
M

Matti Vuori

Lars-Åke Aspelin said:
What I would like to do is to incorporate some shapes (images) in the
Add-in (.ppa) and make them available to the Add-in macro so it can
copy the shapes into the presentation when needed.

Nothing stops you from distributing the PPT version of your add-in also. It
would be easiest for maintenance of the shape collection and testing.

In you add-in, you just figure out where the add-in is, open the
corresponding .ppt file (invisibly) and reference the shapes in any way you
like.
 
L

Lars-Åke Aspelin

Nothing stops you from distributing the PPT version of your add-in also. It
would be easiest for maintenance of the shape collection and testing.

In you add-in, you just figure out where the add-in is, open the
corresponding .ppt file (invisibly) and reference the shapes in any way you
like.

Thanks for your suggestion. I may use that as a "plan B".
But it would be extremely convenient not to have to rely on that the
..ppa and .ppt files are kept together in the same directory.

As the images already are in the .ppa, as (bitmap) images in a
userform, it was hoping that they could also be accessed by a macro in
the addin itself.
Other attributes of the userform image pictures can be accessed, e.g.
like this
Userform1.Image1.Picture.Height (or Width or Type)

I guess the method Render is about getting the bits of the image INTO
the Picture and not out of it, but there is also an attribute called
Handle. Could that be of any use here?

Lars-Åke
 
A

Andy Pope

Hi,

Will this approach work for you.

Add a Image control to your userform and load the required picture.
The following code inserts a new Image control on the active slide and
copies over the images picture.

Sub CopyImage()
Dim frmX As UserForm1
Dim objImage As Object

Set frmX = New UserForm1
Load frmX

With frmX.Image1
Set objImage =
ActivePresentation.Slides(ActiveWindow.View.Slide.Name).Shapes.AddOLEObject(
_
Left:=10, Top:=10, Width:=.Width,
Height:=.Height, ClassName:="Forms.Image.1", Link:=msoFalse)
objImage.OLEFormat.Object.Picture = .Picture
End With
Unload frmX
Set frmX = Nothing

End Sub

Cheers
Andy
 
L

Lars-Åke Aspelin

Yes this definitely looks like what I was looking for!
I will test it and report back.

Lars-Åke
 
L

Lars-Åke Aspelin

I have now tested the proposed approach that indeed meets the
requirement to be fully "embedded" in the .ppa file.
The result on the ppt slide is a "Control" with an image rather than a
"Picture", but I can live with that.

However, there are a couple of problems with this approach that I have
not been able to solve:

1) Unwanted resizing and blurring of picture
When the picture is loaded to the userform image, it seem it is scaled
by some 75-76%. It does not matter if the picture is an JPEG or a BMP
picture in the source file.
If the picture is 200x150 pixels, the size in the user form image will
only be some 152x114.
When the picture is copied to the ppt slide by the CopyImage macro as
suggested this reduced format is kept. The size is thus only 75%
compared to if the picture is copied by Insert Picture from file.
That can be compenated for, but the "sharpness" of the image is lost
when the size is reduced and that makes the result unusable.

Is there something that can be added to the macro to prevent the
picture from being resized and blurred?

2) Application sometimes crashing/restarting
Occasionally Powerpoint crashes/restarts when the CopyImage macro is
called from a custom toolbar. I have not been able to see any pattern
when this occurs, it does not happen very often, but it makes it
unusable.
The worst kind of problem that can not be repeated in a predictable
way.
The environment is PowerPoint 2003 (with SP2) on Windows Vista
Enterprise (with SP1).

Appreciate any suggestions on how to overcome these two problems.

Lars-Åke
 
A

Andy Pope

Hi,

I can not reproduce the problem of image shrinkage. For me the image
appears the same as if I had used Insert> Picture.

Cheers
Andy
 
L

Lars-Åke Aspelin

Well, I guess that the resizing problem is not a problem with the
macro as the macro just copies the picture from the user form image
into a slide control.
As the 200x150 pixel picture does become just 152x114 when it is used
in the user form happens also in Excel so it is a general problem not
limited to PowerPoint. For me it happens in Office 2007 as well as in
Office 2003.

Is there any other way to populate the user form image from a file
than just click in the Properties and then select the file in the file
explorer?

Lars-Åke
 
A

Andy Pope

You can copy the picture and then select the picture property of the
control and paste. Although I can not see how this would make a difference.

Cheers
Andy
 

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

Top