I have asked this elsewhere, but have never found anyone knows how to build an add-in for VBA IDE using VB.NET. Is it even possible? Could someone point me to an example?
相关问题
- 'System.Threading.ThreadAbortException' in
- how to use special characters like '<'
- Error handling only works once
- Excel formula in VBA code
- C# to VB - How do I convert this anonymous method
相关文章
- vb.net 关于xps文件操作问题
- Checking for DBNull throws a StrongTypingException
- Using the typical get set properties in C#… with p
- Unregister a XLL in Excel (VBA)
- Load a .NET assembly from the application's re
- C# equivalent of VB DLL function declaration (Inte
- numeric up down control in vba
- Declare a Range relative to the Active Cell with V
It is possible you need to write a com addin using IDTExtensibility2 interface, select the shared addin project template from new project.
EDIT
Otherwise to create this addin from scratch you will need to do the following:
Heres an implementation to get you started, Firstly replace the "YourAddinName" with your AppName and Create a Guid for "YourGeneratedGuid". You will need to register the Addin into the right Registry location, see the registry keys that follow to get an idea, also replace some vars in the registry keys.
Here is the registry .key script to register the Addin, note you will need to change some of the settings in order to register it properly.
NOTE the tokens "YourGeneratedGuid" must have the curly braces {} included and be the same as the Guid in the attrib above, the token "YourAssemblyNameFullTypeName" must be the Assembly full name, the token "YourAddinName.Connect" must be the same ProgId set in the attrib above.
SIDE NOTE
Also found this helpful, might save you couple hours googling.
I also found this reference to be helpful in making a VBA DLL from C# or VB.NET:
Create a new C# (or VB.Net) project and select Class Library as the template type.
Configure project properties to make it COM visible.
Use the DLL in your VBA code.
Made available by GeeksEngine.com
Also take care that the Guid of the project (in case of c# in assamblyInfo.cs) is different from the Guid of the Class "Connect".
Having the same Guid results in a "could not be converted to a type library"-error when checking: Project Properties > Build Tab > Register for COM Interop
I would imagine you could call a .NET DLL from your VBA code (never actually done this myself). Just create a VB Class Library project and create a DLL to use in your VBA.
After a quick google it looks like you would need to set "Register for Com Interop" = True under Project Properties->Build, but like I said, I've never actually tried this before.
Unfortunately almog.ori's steps didn't work for me. Here is my version to aid people in the future:
Create a C# or VB.NET Class Library project named "VBEAddIn"
Add the following Interop assemblies as references to the project, using the "Project", "Add Reference..." menu, "Browse" tab.
Extensibility (C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Common\Extensibility.dll) - if its not there try the C:\Program Files (x86)\ if your using a x64 PC.
Microsoft.Office.Interop.Excel (C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll)
Microsoft.Vbe.Interop (C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Vbe.Interop.dll)
(optionally) Microsoft.Vbe.Interop.Forms (C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Vbe.Interop.Forms.dll)
Add a class to your project with the following code:
VB.Net:
C#:
In the Project properties window of the project:
In the "Application" tab, ensure that both the Assembly name and Root namespace are set to "VBEAddIn".
In the "Compile" tab, ensure that the "Register for COM interop" checkbox is checked. We wont bother with registering assembly for COM Interop manually with the proper regasm.exe tool. However note that the "Register for COM interop" checkbox would only register the add-in dll as 32-bit COM library, not as 64-bit COM library.
In the "Compile" tab, "Advanced Compile Options" button, ensure that the "Target CPU" combobox is set to "AnyCPU", which means that the assembly can be executed as 64-bit or 32-bit, depending on the executing .NET Framework that loads it.
In the "Signing" tab, make sure the "Sign the assembly" is unticked.
Next add the registry Keys, save the below snippet as a ASCI file with a reg extension and double click it to add the values to the registry.
Important Note: Before you execute the reg file, change the path: "CodeBase"="file:///C:\Dev\VBEAddIn\VBEAddIn\bin\Debug\VBEAddIn.dll"
If you get this error:
Its likely you didn't change the path "CodeBase"="file:///C:\Dev\VBEAddIn\VBEAddIn\bin\Debug\VBEAddIn.dll"
And check that the CodeBase key is in the registry (add a string regkey with the CodeBase if it doesn't exist):
Then close down the Office application, build the VBE AddIn again from Visual Studio, Open Office (Excel, Outlook, Word, etc) and Alt + F11, AddIns menu > AddIn Manager and select the AddIn and Tick Loaded/UnLoaded.
Final Trick to overcome this issue:
If that still fails, close the Office app, goto Visual Studio, Project Properties > Build Tab > Tick Register for COM Interop > Build Solution and open the Office Add In > Alt + F11 > AddIns Menu > AddIn Manager and click Loaded/Unloaded.
This answer uses some info from Carlo's Quintero (MZTools) that I've modified, Ref: http://www.mztools.com/articles/2012/MZ2012013.aspx