Is it possible to write Excel VBA Code in Visual Studio - vba

Is there a way to write VBA Code in Visual Studio. If not is there any other alternatives?

The best you can do is bend the office Visual Basic Editor (VBE) tool to your liking. If you stay in it's native environment you get the full power of error detection, Intellisense, live code running, etc.
My tips...
In the VBE go to Tools > Options > Editor tab.
Turn off 'Auto Syntax Check'. You still get code highlighted errors but no annoying popups.
Go to the Editor Format tab and change the Font to Consolas (Western), Size 11.
For code indenting install the awesome, free, Code Manager. It adds some sick keyboard shortcuts.
Make the Edit toolbar easily accessible for code commenting/uncommenting.
Use Rubberduck to add unit testing, source control, code inspections and refactoring functionality.
With those simple changes you end up with a half way decent, useful, and keyboard friendly environment to write your visually appealing code. :-D

VBA code for Excel can only be written inside Excel using the VBA IDE. VBA projects are stored as part of the Excel file and cannot be loaded into Visual Studio.
However, you can write VSTO (Visual Studio Tools for Office) managed add-ins for Excel using Visual Studio. The following MSDN page covers both developing with VBA and VSTO.
Excel for developers
You could also use the interop features of VBA to consume a (COM) object written in Visual Studio from your VBA code.

I've been looking for an answer to this question myself.
Best I've found myself is the option of exporting a Module ect from Excel with the code you've already written (or blank) and load that up in the Visual Studio Environment.
It doesn't offer much, but the highlighted text and auto indenting is nice and makes it much easier to read compared to the standard VBA environment.
Then once you're done just import it back into Excel.

There is a VSCode extension to do this.
It allows you to write code in VSCode and export it to Excel.
This extension is very useful when you develop in VBA.
Here's the link to download the XVBA extension
Edit :
As Peter Macej said in comment, this solution only works for Visual Studio Code not for Visual Studio

You can certainly add and edit a VBA file (.vb) in your Visual Studio solution, but the intellisense will be worthless/screwed up.
This extension for VScode would probably provide a much better experience: https://marketplace.visualstudio.com/items?itemName=spences10.VBA
If your goal is have your VBA code exposed to source control so you can track changes, then it's still worth it to include in your Visual Studio solution, but just store that VBA code in a plain text file and then use the Excel interop to load it into the appropriate module within the excel workbook, e.g.:
xlWorkbook.VBProject.VBComponents["ThisWorkbook"].CodeModule.AddFromFile(#"C:\PathToYour\VBAcode.txt");
And there are other methods to delete/replace code lines, etc....

You can try xlWings package for python and use it with VS Code https://youtu.be/xoO-Fx0fTpM

Related

VSTO Sample code For Sensitivity label Ribbon Item OnClick Events in C#

I need to get changes to a sensitivity label done by clicking the Ribbon item in Microsoft Word.
Does anyone have sample code on catching event changes for sensitivity labels in particular for VSTO C#?
We are using ribbon XML and not ribbon designer and extending AddInBase to start up the word application.
I have tried Document events. I have tried Commands.
Any help will be greatly appreciated. I can't find much documentation for VSTO
These are some VBA references.
You can try using the Document.SensitivityLabel property on ThisDocument/ActiveDocument and listen to the SensitivityLabel.LabelChanged event
The answer is a mixture of #cs answer and #Dimitry's answer (I don't have enough reputation points to mark that your comment was very useful). So thank you very much both of you for your assistance
VSTO no longer has support, so visual studio office tools include interop dll v15.0.0 for .net framework version 4.7.2. On our development machine, we have office 365, which from what I read means the latest interop dll will be installed as part of the installation process which has Microsoft word object library v 16.0
Adding Lastest interop to Visual Studio as a com reference
However visual studio still did not update the interop interfaces. What I had to do was change the wrapper property in the csproject from
<WrapperTool>primary</WrapperTool> to <WrapperTool>tlbimp</WrapperTool>
This updated the interfaces and allowed me to get access to the Document.SensitivityLabel object.
So thank you to all that commented it was greatly appreciated.

How to install a theme color in vba

I just downloaded a color theme on that link (a vssetting file) and would like to know how to install it in VBA.
I have the file but no option to put it into VBA. How do I do that?
You can't use a .vssetting file, that's a Visual Studio thing - the VBE wouldn't know what to do with it.
Its colors are "configurable" through the Tools > Options menu; they're the same rather limited 16-bit colors from 1997... and there's no file you can import that will make a "theme" (a .reg file could act like it, perhaps)
There is a hack, though - you can hex-edit VBE7.DLL and change the available colors to whatever you like. Obviously the risk is that you completely wreck VBA in every single VBA host application at once, but hey shiny colors are worth it eh?
The only thing I'm seeing that's remotely close to "theming" the VBE (and doesn't involve rewriting VBA itself) is what we want to achieve in Rubberduck (an OSS VBIDE add-in project I manage) by version 3.0 (we just kicked off the v2.1.x cycle), when we inject our own AvalonEdit WPF code pane into the VBE, implement our own IntelliSense and take full control of the VBE code panes' appearance and functionality - but there's a lot of work to be done before we can even dream of shipping that wet dream of a feature - so if you know a bit of C# and WPF, by all means join the Rubberduck dev team and help us do it!

{System.__ComObject} properties in Visual Studio debugger not the same as Excel VB

I've been trying everything for a week and I cannot find a soltion for this. The problem is that I have been automating process in CATIA with Excel VB and now I want to use Visual Studio 2015. The only problem that I have is the debugger in Visual Studio. When I use:
Dim CATIA As INFITF.Application = GetObject(, "CATIA.Application");
I have a {System.__ComObject} as a result, clicking on "Dynamic View" (Debugger/Locals) I dont get any propertie of the object. If instead of CATIA.Application, Excel.Application is used I can see in the "Dynamic View" the different properties of the object.
http://stpserver.webcindario.com/vstudiodebugger.png
Also when I type the code:
Set CATIA = GetObject(,"CATIA.Application")
In Excel VBA, in the debugger it will appear all the properties of the object.
http://stpserver.webcindario.com/excevbadebugger.png
My question is how I can see in the Visual Studio 2015 debugger window the properties of the CATIA object as I can see them in Excel VB debugger.
PD: I have imported all the necessary references in Visual Studio 2015
Related post: Use C# for Catia V5 Automation
Sadly I do not know the exact VB.net code you will need but this should point you in the right direction. Since you want the COM object the process is the same, only the syntax is different.
In your Visual Studio project right click and Add Reference. Under the COM tab, Type Libraries section look for: CATIA V5 InfInterfaces Object Library. This is needed for INFITF.Application. If you want to work with other objects in Catia you will need MecModInterfaces, PartInterfaces, ProductInterfaces, and for HybridShape types you will need the CATGSMIDLItf Object Library (This has a different name prior to R23 but I do not remember what it was).
After that you need to use Marshal to attach to the Catia process.
The version of Visual Studio should not matter since you are using COM. It does matter if you are using CAA-RADE which has version specific tools.
I have successfully used Visual Studio 2010 (with Catia R19-20), VS2012 (with Catia R22 and R23), VS2013 and VS2015 (with Catia R23).
I think your problem might be with the missing references.
Latest version supported by Dassault Systemes for their CATIA (by the way, which version? what OS?) is VS2013 (hope I'm not doing a mistake).

Is it possible to enable syntax highlighting for Microsoft Visual Basic?

I am working on a very old project which needs to be updated. Because the program needs to be backwards compatible, the changes need to be made in this older IDE.
There is no built in syntax highlighting that I see. Is there a plugin or menu that I have missed?
Within VB Editor, go to Options then Editor Format and adjust colours.
There are also some 3rd party softwares out there that will enable you to edit your code with syntax highlighting.
UltraEdit Visual Basic Editor

Visual Studio Macros Editor

Is there a way to change the macro editor from this default one:
It feels like such a downgrade from Visual Studio 2010. Can I configure Office to use that instead?
no there is no way to my knowledge, VS.NET does not support VBA.
what you can do if you are creating a new word or excel file is create a managed office solution, having installed the VSTO 2010 (Visual Studio Tools for Office) you are able to create managed documents which are handled inside VS 2010 and the automation language is not VBA anymore but C# or VB.NET instead.
this is a viable solution for new files, for legacy files either you convert them if they are small or you keep old VBA code and old VBA editor as in your picture.
Mark I wish MS made a tool to convert from the old format to the new one! :(
There are things you can do to improve it:
Tools > Options > Editor :
Uncheck "Auto Syntax Check"
Tools > Options > Edit Format:
Customize the colors and font
Install the VBA Rubberduck:
http://rubberduckvba.com/
The RubberDuck does auto-indent for you. It also helps you with "unit testing, source control, from code inspections, and refactorings"