Edit a .ppam PowerPoint add-in and save changes - vba

I have a very useful PowerPoint add-in in a .ppam format. I used the following method (Edit a .ppam file for customizing an add-in) to open it in VBA and slightly modify to better suit my needs. I would like to save the changes now, so that they are in effect whenever I open PowerPoint with that add-in. However, when I click 'File -> Save' in VBA Editor and restart PowerPoint, the changes I made are no longer there.
How do I save them?

You can't. You can either copy/paste from the edited modules/forms/classes into new modules/forms/classes in a PPTM file or export each of the modules/forms/classes and then import them into a PPTM.

I am maintaining one PowerPoint Add-in myself for a long time.
My way of doing it:
keeping the master file in ".pptm", and always maintain it there
as soon as the master files opens (can be hooked in Ribbonloaded event), it'll disable the running add-in
I developed a button to release current .pptm file replacing the .ppam add-in, so as long as I want to publish again, I'll click it.

Related

Recover code modules from corrupt vba pptm file

I opened my .pptm ppt vba macro source file for editing, it showed this dialog a few times.
once I open alt+F11 to VBA editor, it has all the forms but the underlying code is not opening, also the modules are unresponsive.
Can anybody suggest a way to recover code/modules from this corrupt file. I have the latest .ppam addin output, I can share the corrupted .pptm file if there is way to attach here. Thanks for the help folks!
Tossing this in as an answer as well as the comment. Better formatting!
Make sure PowerPoint is NOT running, then open REGEDIT.
Go to HKCU\Software\Microsoft\Office\xx.0\PowerPoint\Options
Replace xx.0 with your PPT version: 16.0 for 2016/2019/365, 15.0 for 2013, 14.0 for 2010 etc.
12.0 is 2007. There's no 13.0; MS is apparently superstitious.
Rightclick Options and choose New, DWORD Value
Name it: DebugAddins
Doubleclick it and change its value to 1
Close Regedit.
Start PowerPoint and open the VBA IDE.
All of your installed VBA addins will appear as projects that you can open and edit, so long as they're not password protected (and if you know the password, you're good to go).
Note that you cannot SAVE your edited code. You'll need to copy/paste it back into a PPT/PPTM file and save that, or export the modules you've edited and import them into the original PPT/PPTM.
You can generally have both the source PPT/PPTM and the addin code loaded in the IDE; just be sure you stay aware of which is which.

Creating PowerPoint Add-Ins from VBA defined Macros

So I've created a Macro in a PowerPoint Presentation and I would like to make this Macro readily available to someone else. From what I have gathered, the best way to do this is to create an Add-in.
I have come across several articles stating to do the following, but I am still running into problems:
Open a new PowerPoint Presentation
Create a macro using VBA
Exit VBA, and save the Presentation as a .ppam
Under the Developer Tab, click on the "PowerPoint Add-ins" button
Click the "Add New..." option, and browse for your recently created
.ppam file
Click Load
Click Close
Click Add-ins
You should now see your new Add-in
I have done everything mentioned above and can even see the new Add-in when I select PowerPoint Add-Ins. I'm here because I can't find a way to run the new Add-in, and I don't want it to automatically load on startup or to interfere with the presentation by creating a button on the slides. As instead, I would like to allow the user to run this Add-in from the Ribbon.
Thoughts?
First, save as PPTM. THEN save as PPAM. If you only save as PPAM, you won't be able to go back and edit your code later to correct problems.
To launch your add-in from the ribbon, you must include RibbonX code (a type of XML) to your PPAM or PPTM (best to add it to the PPTM ... it'll become part of the PPAM automatically then).
There's an excellent book on RibbonX that'll save you hours of time:
http://www.amazon.com/RibbonX-Customizing-Office-2007-Ribbon/dp/0470191112
This is a good starting point if you don't want to buy the book:
https://blogs.msdn.microsoft.com/jensenh/tag/all-office-2007-ui-posts/
While you can manually unzip PPTM/PPTX/PPAM files and add the RibbonX code using any text editor, it's silly to do so. Instead use this free tool:
http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/08/07/7293.aspx

Excel VBA Add-in reference not listed

I saved an Excel project as type "Excel Add-in" to the default directory under AppData/Roaming/Microsoft/AddIns. The extension is .xlam. Upon reopening Excel, this Add-in was listed under File-->Options-->Add-ins and I toggled the checkbox to select it.
In the VBA editor, this Add-in is displayed along with the other Add-ins listed under File-->Options-->Add-ins but none of the code can be called. It is not listed in the available references and, when I use this window to browse for it, it is not detected in the AddIns directory -- despite clearly being there.
I notice that all of the other Add-in projects that were installed without problem are .xla file extensions. We are using Excel 2010 but the problem is identical when I save the project as type "Excel 97-2003 Add-in" with an extension of .xla.
Advice is appreciated.
In the VBA Editor click on ThisWorkbook for your Add-In. In the Properties Window, make sure that IsAddin is set to True. This also makes it easier to edit your addin. When you need to make changes to the add-in code, follow these steps:
Set the property to False
Make your edits
Change the property back to True
Click on the Save icon
Close Excel.
You will be prompted if you want to save the changes made to
"addin.xlam", click yes.
And as #nwhaught suggested, make sure your functions are set to Public and not Private.

How to check if code that is run belongs to active sheet

I have written some code for an excel spreadsheet. This sub has been added to the ribbon using the standard customizations found in File -> Options -> Customize Ribbon. What is funny though, is that the customization will run the Sub on the EXACT EXCEL FILE that the Sub is written in, i.e. the original.
My users copy the excel file and make adjustments and then run the sub (by clicking the customized button in the ribbon), which then opens the ORIGINAL template file and runs the code. This actually works fine - and I like it because it runs the TEMPLATE code on the ACTIVE sheet. But the only problem is it is also OPENING the original template file. How do I get it to close? Remember it is not the ActiveSheet anymore.
One possible answer is to check if the template is open, then close it (using a static reference), but I would prefer something a little more elegant than that.
You should be able to find the template/original using ThisWorkbook.
Also, if you deliver your workbook's code as a .xlam instead of .xlsm, it will be opened, but there won't be a window for it, so users won't see it.
Not sure if you're using this already, but you might also want to use "for this workbook only" when adding ribbon buttons for a .xlam . (Delivering as .xlam allows you to install buttons from a workbook into the ribbon, without relying on file path and without needing your users to also install buttons (they install .xlam instead).)

Standalone code for Excel

Can VBA code be written to perform actions on any Excel file?
When I create a project in Visual Studio, it asks for an Excel file to be linked to it. All the code that I write is in ThisWorkbook.vb and hence acts only on the Excel file linked to the project.
Ideally, I want a script that:
When the user double-clicks, he/she should be allowed to select an Excel file of choice for the actions to be performed on that file.
If the above is not possible, I'd at least like to invoke the VBA script from within an Excel file.
Basically, the VBA code should be independent of any Excel file.
I looked at VBScript, but the syntax for it seems to differ slightly.
You've mentioned Visual Studio, VBA, and VBScript. The solution I'm outlining works directly with VBA rather than Visual Studio or VBScript. (However, you might adapt Visual Studio (C# or VB) along the lines of what I'm outlining below.) Hope it's helpful, so here goes:
Here's what I do, and, it ultimately results in an .xlam Excel AddIn as #chris above has commented.
Overview
I start with ordinary .xslx workbook, to which I add a VBA project, making it an .xlsm. Then create some VBA Subs (macros). Then create some Excel QAT (Quick Access Toolbar) buttons for the workbook, which are bound to (i.e. they invoke) the VBA subs/macros. Then convert the workbook (with VBA in it) to an .xlam, which is an Excel AddIn. When you're all done, the buttons are accessible from any workbook (and the VBA code has access to any user workbooks as well as those originally in your .xlsm). Further, the workbook associated with the .xlam is invisible. So it just looks like you've added some buttons to the QAT that appear on all users .xlsx windows. The .xlam is pretty easy for users to install (though I provide a buttons to uninstall/reinstall/check version). You can upgrade an .xlam independently of users' workbooks; users' workbooks can thus be data only (.xlsx, no VBA).
Details
Write some Excel Subs you want to use later
You need to be aware that the buttons can only invoke macros (VBA Subs) without parameters, so the macros will have to check things like ActiveSheet and ActiveWorkbook and Selection to figure on what sheet the button was pressed, hence what user data to really operate on. (If you need to refer to your workbook with the VBA code in it, use “ThisWorkbook”). You should be aware that there can be naming conflicts, so try to name the parameterless subs with rather long names, such as MySomewhatUniqueProjectName_button1, etc…
Add Buttons to your .xlsm
Using Excel 2010 (I think this works with 2007 or later), I put workbook-specific buttons on the QAT part of the ribbon, which connect to macros (VBA subs) in the VBA code.
To do this, you from the Quick Access Toolbar customization drop down (the tiny down arrow at the very top row of the Excel window, the last icon from left to right) choose "More Commands…". When the “Customize Quick Access Toolbar” dialog box comes up, from the (2nd) "Customize Quick Access Toolbar:" heading (top to the right), choose "For XYZ.xlsm" from the dropdown instead of the "For all documents (default)". And from under "Choose Commands From:", use "Macros" (instead of “Popular Commands”) from the dropdown. Once you have those two things selected, you can move VBA subs from the left box to the right box using “Add >>”. Each so moved will become buttons visible in your QAT. As you’re doing this you can also edit the icon and text for the buttons, add a separator as needed (I always end with a separator in case other .xlam’s use the QAT). (Now is a good time to save this .xlsm.)
Convert the .xlsm into a .xlam
Then I convert the .xlsm to an Excel add-in, by merely saving it as an .xlam file. This will end up (1) hiding the workbook associated with the code you have (though it is still accessible to itself.). Further, now, (2) the (invisible, as now it's an .xlam) workbook will load whenever Excel is loaded. (To keep this fast for when users use Excel but don’t run my VBA code, I don't run any code when the .xlam is loaded, I only run code when a button is pushed.)
You can manage the AddIn using Excel’s AddIn manager. To update the AddIn, you have to use some trickery. While you can copy over it when Excel is not running, on the other hand, you cannot directly uninstall the AddIn, you can only disable it from Excel. But once disabled, you can delete the .xlam, and relaunch Excel, go to the AddIn manager to try to work with the (now gone) AddIn and you’ll get Excel saying it can’t find it, so do you want to delete it. Say yes, and it will be uninstalled.
FYI, Notes
I keep the .xlsm to edit later, but you can actually debug and edit the .xlam and later convert it back to an .xlsm with a minor bit of trickery: find its "ThisWorkbook" entry in VBA, and then the "IsAddIn" property, set to false, its workbook will suddenly appear and you can save as .xlsm, or edit its workbook and set the property back to true to resave as .xlam directly.)
Answer 1
You can do that in VB.Net too. Just make a regular project. (comment by #SLaks)
This worked for me very well and was exactly what I was looking for.
Answer 2
The very descriptive answer posted by #ErikEidt
I haven't tried this, but it seems like a good and alternative way of getting macros to work.