PDA

View Full Version : powerpoint 2007 add-in (newbie questions)



Welliam
02-16-2011, 05:05 PM
Hi,
I am a VB.NET developer but I am new to VBA and I have quick simple questions looking for an answer:
I have an add-in in powerpoint 2007:
1- I have a button that load a form, how I change the button icon ?
2- How I add another button in the ribbon ?
3- how I decide wich module to load when I click on the add-in button in the ribbon bar button ?
looking only for small tips


Thanks

Paul_Hossler
02-16-2011, 07:35 PM
It sound like you want to add commands to the ribbon to call your add in's subs

It's different than the previous CommandBar logic

There is a 'Office 2007 Ribbon UI' forum here that has more information about the Fluent interface

Basically you

1. Write the PP macro file (.pptm)
2. Use the CustomUI editor

http://openxmldeveloper.org/articles/customuieditor.aspx

to add XML to your .pptm

Adding the XML is not impossible (after all, I can do the simple basic stuff), but it is very finicky

3. If you get stuck, post a simple version of the add in and the forum members will be glad to assist.

4. Additional night time reading -- Ken's book has a sample chapter available

http://www.excelguru.ca/node/93

as does Stephen Bullen

http://www.oaltd.co.uk/Spreads/Excel2007VBAProgRef_ch14.pdf


Both very insightful, and worth ordering (don't seem to be on the shelf in my local book store)


MS also has Office Fluent User Interface Developer Portal

http://msdn.microsoft.com/en-us/office/aa905530.aspx

Paul

Welliam
02-16-2011, 09:05 PM
Many Thanks Paul

I found that using the CUI tool there is XML in the add-in file, in general the onAction="action_name" decide which sub to execute when user click on the button

Sub action_name(control As IRibbonControl)
code here...
End Sub
one question now please, there is code that get executed in a module like this

Option Explicit

#If VBA7 Then
Sub sub_name1
#Else
Sub sub_name2
#End If
sub_name3
End Sub
how and when this part get executed ?

Please advise

Frosty
02-17-2011, 11:10 AM
That's a confusing indent structure for what's going on.

The procedure is called sub_name1 if the project is compiled in VBA7, otherwise the procedure is called sub_name2.

In both cases, it runs a procedure called sub_name3.

It would be more clear if it looked like this:


#If VBA7 then
Sub sub_name1
#Else
Sub sub_name2
#End If
sub_name3
End Sub


As for how and when it gets executed... it depends on when it's called, like any other procedure. The # marks are simply conditional compilation statements.

Welliam
02-17-2011, 11:29 AM
the format is confusing because I used the VBA tags , it insist to display it in that way!

About the code itself , it exists in one of the modules and I want to know the order of executing codes like that in modules, by other meaning which module code fire first and whether I am able to decide or not -- bear with me plz I an new

Frosty
02-17-2011, 11:39 AM
Ahh. I'm new to this forum too, so I didn't know about the VBA tag, having only used the CODE tag.

I think you're asking a concept question. I will try to answer, although I am not 100% sure of this.

VBA modules are compiled at run-time (although it is good practice to compile your entire project to make sure you don't have some silly coding mistakes).

The entire module will be compiled at the point you call some procedure within the module.

Above that, I think, it would determine what kind of events/automacros were called in your project.

I am less familiar with powerpoint events than I am with Word, so I can't be much more specific than that.

The general concept is that the modules are compiled "as needed"... and the "as needed" order really depends on what, if anything, you are calling both a) when the project is loaded and b) when you've called a particular macro.

John Wilson
02-17-2011, 01:04 PM
What that does is run sub_name1 if you have PPT 2010 and sub_name2 if you have any other version
sub_name3 always runs.

It is often used to declare APIs for 2010 64bit that need to be PtrSafe but wouldn't compile in earlier versions

Frosty
02-17-2011, 01:13 PM
Really? Correct me if I'm wrong, John... but isn't it only declaring sub_name1 vs. subname2?

So in the 64 bit version, you would call sub_name1 to run sub_name3, but otherwise you would call sub_name2 to run sub_name3.

I'm not sure why you'd use this particular construct, honestly.

Where I've seen this usage is when you need to declare a return value or a parameter differently, but the routine is named the same. For example:



#If VBA7 then
Sub MySub(myVal as LongPtr)
#Else
Sub MySub(myVal as Long)
#End If

Or am I missing something?

John Wilson
02-17-2011, 03:59 PM
As it stands it wouldn't work at all (this is also why it didn't format)

My assumption was that the code was to run a different sub (sub_name1) if the user had PPT 2010 and another if the user had an earlier version (sub_name2). This is often useful as there are many new features in 2010 that won't compile in 2007 and also the possibility of 64 bit versions that need special API calls

Sub altsubs()
#If VBA7 Then
sub_name1
#Else
sub_name2
#End If
End Sub

Welliam
02-17-2011, 04:05 PM
Many Thanks for all replies:

that was like John Wilson said.

but yes I was asking about the concept not a particular codeI am still not sure which module fire first or based on what ,

I know about auto_open but I really like to know the sequence VBA code get executed modules that confuse me as I come from other languages

Frosty
02-17-2011, 04:13 PM
I think the formatting issue is one of the custom VBA code on this board rather than something about the code which wouldn't work. Technically that construct could work.

The following code (which I have in use and know works) looks "wrong" according to the VBA code on this board.


#If VBA7 Then
Private Function fCreateIPicture(ByVal hPic As LongPtr) As IPicture
#Else
Private Function dfCreateIPicture(ByVal hPic As Long) As IPicture
#End If
'do stuff
End Function

That said, Welliam, you should look up application events in powerpoint on MSDN.

There is a particular order, but it's a big discussion and not easily summed up. If you have a more specific question, it would be easier to answer.

Welliam
02-17-2011, 04:57 PM
ok simpler question can I write code in a module without using an event and get executed at start up (something like the main procedure in C++)

also without using auto_open.

Frosty
02-17-2011, 05:12 PM
Start up of what? Powerpoint? A presentation?

I believe the order of operations are:

1. Powerpoint starts
2. Global powerpoint addins are loaded (with each Auto_Open macro, if any, of those addins being run as they are loaded)
3. Powerpoint opens a blank presentation if nothing was called, otherwise it opens the presentation called and then runs Auto_Open if it exists in the opened presentation)

If you check out http://msdn.microsoft.com/en-us/library/ff746018.aspx you can write event code to access additional events, if desired.

However, it really depends on what you want to do. You want code to run at Powerpoint startup? You need an addin that has code in a procedure called Auto_Open.

Depending on the version, you'll need to develop your code in a powerpoint presentation that allows macros... and then you'll need to save it as a power point addin.

From there, you will need to load your addin (2007/2010, show the developer toolbar, click the addins button, then add your addin).

There are a whole host of issues with this, including whether your addin lives in a trusted location, etc.

But the basic structure is:

1. Write code in pptm (using Auto_Open)
2. save pptm
3. save as ppam
4. tell Powerpoint to load ppam

Paul_Hossler
02-17-2011, 05:59 PM
Welliam --

It sounds like your add in is only used when not in slide presentation mode. I have a bunch of macros (thanks to John W's help) to do various things like remove notes, etc. and i have them collected into an add in

It sounds like there's a need to do some initialization when your add in loads, OR is it at some other time (e.g. like opening a presentation) ?

I'm confused about the reasoning behind needing auto_open

For what it's worth, I've used the Fluent Ribbon Load callback to create something that works as an Auto Open, but without having to have an add in

Sample attached if you're interested, just save and remove the .zip part

If it is just a basic PP addin that you're looking to do, I'll be glad to make up a simple add in with some basic XML. Just let me know

Since you are coming from other languages, some more detail and the 'What' and especially the 'Why' would be helpful, because VBA and the Office apps are different

Paul

Welliam
02-17-2011, 09:03 PM
thanks alot for all replies:
@Paul, this is for ribbon load, can you tell me please what is the event for add-in load ?

Frosty
02-17-2011, 09:09 PM
That's clever, Paul. Although I remember reading MS is not a fan of popups during the ribbon onload event, so you might be limited in that approach.

Weillum: addin load event is Auto_Open.

But that fires when the addin is loaded. For example, you will need to create:
1. Auto_Load in MyAddin.pptm
2. Then save MyAddin.pptm AS MyAddin.ppam
3. Then load MyAddin.ppam

Paul_Hossler
02-19-2011, 12:02 PM
thanks alot for all replies:
@Paul, this is for ribbon load, can you tell me please what is the event for add-in load ?

Your addin can use the Ribbon's OnLoad sub in lieu of an auto open. When the add in (.PPMA) loads, OnLoad should run.

The XML doesn't always need to add buttons etc, but it can

My add in adds buttons etc to a new tab to to make calling my macros easier

Paul

Paul_Hossler
02-19-2011, 12:26 PM
this is a simple pptm file that adds tab and buttons to the ribbon that you can play with

save it without the .zip, and open it in pp and save as a ppam file

auto_open runs when you load the add in (but not when you open the pptm file). There is no auto_load (at least in 2010)

paul

Welliam
02-19-2011, 04:42 PM
Many thanks to every one, I am really happy from all great contribution

Paul_Hossler
02-20-2011, 10:10 AM
That's clever, Paul. Although I remember reading MS is not a fan of popups during the ribbon onload event, so you might be limited in that approach.


Not sure about the Popups during the OnLoad (don't reallyy do it), but this way there'e no need for an add in to be on another computer, only 'problem' is that the other user might have to enable macros

Paul