Calling VB.NET code from Excel - vb.net

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/

Related

Hiding or not displaying excel VBA UDF module/code

I wrote a simple UDF in Excel VBA. I saved it as an Add-in and imported it in so that I can use the function like any other excel function. But whenever I open an Excel workbook and subsequently VBA window to create a macro, the module I wrote is displayed in the Project Explorer window and its code in the coding window.
Is there any way I can hide or not display every time I open VBA?
Please let me know if you need more information. Thanks in advance!

How to execute VSTO AddIn code as VBA Macro

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.

VBA what datatype is a dialogsheet?

I can't find DialogSheets in the object browser in VBA. Dim dialog as ??? I like to see the autocomplete so I can know all the properties and methods of a particular type. VarType tells me it is just an object but is there a better or more specific way of finding out the type of an object? For example the OptionsButtons method... how am I supposed to know this thing even exists when it is not in the object browser?
You will not find them in the object browser because they are obsolete. They are just available for copatibility reasons.
You should use UserForms instead. In your vba editor click Insert, UserForm. Now you can easily create a userform with drag and drop.
Have a look here for more about dialog sheet: What exactly is a "dialog sheet" in Excel

How to open add-in automatically on opening excel workbook

I want to know how I can have a workbook automatically open an add-in? The problem is after I create the workbook in code -- call it "A.xlsx" -- I want to add code to it (on creation) that will open the Excel add-in "B.xlam". How do I do this? Creating the workbook is no problem; that part is sorted, and I just want to add the code to that workbook, so that every time it opens it must open the add-in "B.xlam" with it.
From the Developer Ribbon, click Add-Ins,
then click Browse, then navigate to the location of the desired add-in file, then select it, and make sure the checkbox is selected for it.
Alternatively, store the code in PERSONAL.XLS/PERSONAL.XLSB, and these macros/functions will be available to all open workbooks.
If you're somehow asking how to programmatically insert code in to new workbook files, I'm afraid you're out of your element. If you don't know about the Workbook_Open event, nor how to manage your Add-Ins, etc., manipulating the VBE is a pretty high-level operation, and I would not be able to help you with that.
Update from comments
Here is one method that will export VB Components to a specific path, example also includes code to import modules from path to a workbook. You should be able to adapt this to your purposes.
http://www.rondebruin.nl/win/s9/win002.htm
If you have specific problems implementing this solution, please post as a new question.

Populate fields in VB6 executable from MS office VBA

I'm developing a VB6 standalone application that I'd like to be able to call from VBA modules running in Excel, Outlook, etc. Ideally, I'd like the VBA module to check whether the VB6 application is already running, if not, open it, then populate certain controls (textbox, listbox, etc) in the VB6 application with information from the VBA module. Is this even possible? Can I just create a reference somehow to the VB6 application, then treat it like any other object? Thanks for your help!
Make the Vb6 app into an ActiveX Exe project. Here's the VB6 manual tutorial on creating an ActiveX exe. Add a reference to the vb6 from the VBA code. You will be able to call into objects in the Vb6 from your VBA.
Use GetObject to instantiate a Vb6 object from the VBA. That will connect to any existing instance of the vb6 app, or start a new instance if necessary.
You cannot do as you describe and treat the VB6 app like and object but you could do it in the following way:
Use the FindWindow API call to determine if the VB6 application is running
Use the Shell command to start it
Use AppActivate to make VB6 window active and SendKeys to send the data to it
That would be the simplest "out of the box" solution. However, this will be quite brittle. For example, if you removed controls from the VB6 form or changed the tab order of the controls, your app will malfunction!
Another option is DDE but I think the DDE link is intended to go from the VB6 app to Word or Excel, not the other way around.