How to execute VSTO AddIn code as VBA Macro - vba

Normally I can assign a VBA macro to a shape. However, I would like to write some set of instructions in C# and have it as a external method in the AddIn library that can be executed after clicking on that shape (or any other object that is on the spreadsheet and not in the ribbon, which accepts only macros to be assigned to it).

You are free to call add-in's code from VBA. See Walkthrough: Calling Code in a VSTO Add-in from VBA for more information.
Note, you are free to handle the object model events in the add-in as well.

Related

How to interact with the 'Microsoft Dynamics Office Add-in' by VBA

In order to set up an automation of data refresh task, I would like to use VBA to ask the add-in "Microsoft Dynamics Office Add-in", to refresh and then refresh the power query connections to this file, and then move to the next one.\
I have tried to record a macro but VBA won't detect the button within the add-in pane that I clicked;I have also tried to reference to the add-in, however this add-in wasn't showing up, neither in the project explorer nor the references list.\
Is there a way to create VBA against this add-in? I have read the article at:
https://www.exceltip.com/custom-functions/how-to-use-your-excel-add-in-functions-in-vba.html
about how you set a reference but I could not even spot the add-in at the first step.
If it is not possible to get access to this add-in via VBA, is there any other methods to do for such automation task?\
Thanks!

Outsourcing of VBA code

I am writing different VBA functions that I want to use out of different Excel files. How can I "outsource" these VBA functions that I don't have to write them in every Excel file redundantly!
Thank you for your help!
You could save your workbook as an add-in (.xlam) - an add-in loads with Excel, and runs at application level. You can reference an add-in in a macro-enabled workbook, and use the functions and objects it exposes:
The flipside is that anyone using workbooks that reference an add-in, will require that add-in in order to work.

Outlook 2007 - VBA macro in NormalEmail.dotm

I have a Word 2007 template that I use as Normal.dotm, which has corporate theme, styles, etc., plus a tiny bit of macro code that overrides the default behaviour of the bullet & number buttons on the ribbon.
I'd now like to use this same template when creating emails in Outlook 2007. I've tried just copying my Normal.dotm to NormalEmail.dotm, and to an extent this works, but although my macro is correctly invoked when I press the bullet/number buttons in the email editor, it gives an error:
Run time error 429: ActiveX component can't create object
On further investigation, it seems that anything I do within my macro that references an object in the normal Word object model (Application, Selection, etc.) causes this error. So, even if my macro consists of the following, it still fails:
MsgBox TypeName(Application)
I have an inkling that this is because I'm running in the context of Outlook, not Word, and so perhaps there is no (Word) application, or any of the objects associated with it. I know that the email editor in Outlook is Word-but-not-as-we-know-it-Jim.
Having said that, I did discover that ThisDocument does return a valid Document object; unfortunately, it corresponds to the template itself, not the email being edited.
By trial and error, I've discovered that I can get at the Document corresponding to the email by the following circuitous route (this displays the text in the email):
MsgBox ThisDocument.MailEnvelope.Item.Application.ActiveInspector.WordEditor.Content.Text
However, this code also seems to cause Outlook to crash - not at the time, but later, when you close Outlook.
At this point, I'm just about ready to admit defeat. No doubt the "correct" way to create macros in Outlook is to create them in Outlook itself, but from my limited experience, that's just horrible. (You can create macros in VBA, but you can't deploy them; or, you can create them in VSTO, but then you need an installer rather than simply deploying a template).
So. Any ideas out there that would let me do all that I hoped to do?
Override the behaviour of the bullet/number buttons on the ribbon in the email editor.
Deploy the macros in the NormalEmail.dotm file, rather than having to "install" them.
Not crash Outlook.
Have you tried adding the reference in the VBA editor to the Microsoft Outlook Object Library?
In Outlook 2007 you might also have to add VBA references to "Microsoft Word 12.0 Object Library" and maybe "Microsoft Forms 2.0 Object Library". Different versions may apply for other version years.
In addition, in the macro/code you have to set the objects to get/use the correct methods and properties.
I'm not an OO programmer and usually copy code from elsewhere and modify for my need so I can't help much more than what I said above. (And one of my macros is also giving me the same 429 error you get.)

Calling VB.NET code from Excel

I open an Excel workbook from VB.Net and then want an event in the workbook (such as pressing a button) to activate code in VB.Net. How do I accomplish this type of callback?
Make the .Net assembly act as a COM object which is quite easy to use from VBA.
See this page for a comprehensive example:
http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/

VSTO C# expose ribbon events to other document level components

I have an a custom Excel Workbook using C# and VSTO.
Is there anyway for a worksheet to reference the ribbon and subscribe to events, or manipulate controls in the ribbon?
I haven't been able to find the syntax in Globals.Ribbons that will give me a handle to the ribbon.
I've been working on this same problem. So far, no luck. I'll be watching this thread! The ribbon XML API seems to be how Office abstracts the ribbon and keeps you from having any direct access.