MS Word extensibility: VBA macro versus .Net VSTO? - vba

One of my customers asked us to develop a "VBA macro". However, in the 2010s, it seems weird to me to still use such outdated language, and I'm thinking about trying to convince the customer to use VSTO dev instead. However, as I'm new to both worlds, I need help to fill a pro/cons page to be able to argue this.
Of course, the answer can't come without the actual requirement, let me try to resume:
Target : Word 2003/2007 (but I'm suspecting 2010 as a not yet known requirement) edit 2010 requirement confirmed
An external publishing system requires .doc file as input. The .doc file must have some specific styles applied : "Custom Header 1", "Custom header 2", etc.
The user can build documents, using Word, using two possible ways:
Start the new document using a .dot file deployed on the computer
Transform any existing document to match the target template
Users can "apply" the styles "simply" (simple UI): context menu, styles menu, custom action pane, etc.
By now, I see the following pro/cons:
VBA
Pros:
?
quick and dirty development (quick part of the sentence)
The customer has already some in production macro
Cons:
hard to find skilled developer
quick and dirty development (dirty part of the sentence)
VSTO
Pros:
benefits of the .Net language (compiled, typed, rigorous, class library, etc.)
security model more flexible and powerful (trusting code signed with a trusted authority)
bridge to WPF panes possible
You work in Visual Studio and have access to its full set of features: refactoring, source control, etc.
Cons:
requires installation of the .Net framework (probably not an issue today) and VSTO runtime
harder to deploy
slightly more work at the start (but less in long term)

I am not familiar enough to .NET but here is my humble opinion about VBA:
VBA
Pros :
easy to deploy and to make it work with the Office apps
quick and dirty development (quick part of the sentence) - agreed
Cons :
hard to find skilled developer
hard to select a skilled developer and explain your customer he needs to invest in this skill
quick and dirty development (dirty part of the sentence) - partial agreement. It will be dirty if:
you give the project to a VBA beginner and don't frame him/her
your project gets too big in terms of requirements
requires to have the .Net framework installed (probably not an issue today) I don't think so (maybe a CONS of VSTO?)
I would say that if you only want to have some code or add-in to merge some syles, you could easily do it with VBA and it won't be dirty (unless you really want it to).

I'm answering myself to the question, as the project is finished.
I finally decided to write the application using a VBA macro. Here is the post-mortem conclusion of this choice :
Pros:
The team that will maintains the application has no C# knowledge, only VBA (main reason of my choice).
Poor security model : it's a pro because there is no other setup that putting the files in the right place.
No runtime prerequisites
Cons:
The deployment was supposed to be simple.
I was counting of the possibility to play with Word options "User-template directory" and "Startup template directory". But it wasn't possible, because the app is not related to a specific entity in the customer organisation. I wasn't allowed to "take ownership" of this settings for this application.
I ended in writing a custom NSIS script to deploy the application on the correct folders. With VSTO, a simple setup project (clickonce?) would have done the job.
The language is so prehistoric ! Collections index starts to 1, array to 0, no OOP, poor out-of-the box language and library feature. That is not always a problem, but in my case, modeling the business rule was a bit painful (representing hierarchical data in a tree was not an easy job)
Very limited integration with source control (code, as it's embeded in the .dot files, was not historisable. Only the full .dot was)
Error management very limited with VBA
Poor IDE
Remark:
As an experienced C# developer, the pros/cons may be partial a bit.

I am a heavy Excel VBA developer.
VBA pro:
One of the major hurdles for me switching over to VSTO from VBA - and believe me, I love C# coding - is that there's no debug-on-the-fly which my userbase has got used to. I often jump straight into a VBA problem on the user's PC as it is happening, but with VSTO, that's not possible. (Unless someone can correct me.)
If your users have no such expectations this might be something you can easily live without.
VBA con:
VBA is one of the those languages that are easy to play with and thus easy to make a mess with. It doesn't enforce "clean coding" principles which means while decent programmers can make great applications with them, VBA can become associated with hackjobs and sprawling, organic code due to its low bar of entry. VBA developers are often considered a lower class of developer for this reason, when really there is a failure to distinguish between those that use it wisely and those that don't.
I doubt anyone chooses VBA as their career language of choice, it just sort of happens to them. Aside from being hard to find skilled developers, too much VBA work might turn away potential hires as they don't want to be associated with the "quagmire of another unmanaged VBA sprawl". Some people take use of VBA as a statement about how "serious" you are about technology.
(I tend to see Perl in the same light; great for short scripts but when someone used to using for scripting starts to use for a larger piece of work - you tend to get something that's a bit unwieldy.)

In this article I tried to summarize similar questions, in the context of Excel. Same arguments apply for all of the Office applications including word.

According to my observations :
VSTO :
CONS : that if we have developed Addins ribbons control for word 2007, we can only deploy with word 2007, but we can not deploy with word 2010 or word 2013.
This is major drawback for me to develop Addins for word and excel for all its versions like 2007, 2010 and 2013. plz correct me if am wrong or suggest me how could I develop Single Addins for all office version.

Related

How can I expose Word VBA macro code as text files in VSC to better commit and track code changes?

Background and Problem
I lead a team of tech writers. Our team has a Word template (.dotm file) that has a bunch of VBA macros we've created over the years to help us automate tasks in Word. The macros are obviously stored inside of a .dotm template. Given that the code is stored in a binary file, the code is not easily traceable across commits.
Currently, we have to pull the changes on the branch and then open the macro inside Word's embedded code editor from the .dotm. But there's no way to see what exact code changes someone else made line by line or resolve conflicts using something like Beyond Compare.
What I Need
I'd like to find a way to automatically expose the VBA code from the .dotm as some kind of text file so that we can:
Do Word VBA coding tasks within VSC
Easily track and review each others code changes at the line level using our git repository
Resolve merge conflicts more easily
Has anyone found a way to do this?
What I've tried
I've tried searching online and on stackoverflow.
Hoping it might help, I installed ScriptLab, and I after following the MS tutorial, I now have Office Add-Ons working with VSC.
(https://code.visualstudio.com/docs/other/office),
(https://learn.microsoft.com/en-us/office/dev/add-ins/tutorials/word-tutorial) While the Office Add-Ons through VSC does provide an alternate (more modern) way of automating using Javascript, and the .js files are commit-able, it's not providing a way to expose the Word VBA, at least not that I could see.
Thank you in advance!
ScriptLab add-on is for playing with JavaScript code instantly, not designed as a macro infrastructure in Office.
Sounds like you need to switch your VBA macros to COM add-ins rails. There is no converters that can do this part of work for you automatically.
You may consider creating a VB.NET based add-in. In that case you will avoid language translating issues. You may find the Converting Code from VBA to Visual Basic .NET article helpful. It presents issues to consider when converting your Microsoft Office solutions from Visual Basic for Applications (VBA) to Visual Basic .NET. Provides a general overview of their differences, and then uses examples from Word and Excel to describe how you can use Visual Studio Tools for the Microsoft Office System to convert your code.

XLA vs COM vs Automation vs Managed-Code Add-in

I need your help in deciding the best strategy and technology to choose from for the continuing development of a successful in-house VBA application.
In our company (a web-based distributor of financial information), over the years, we have developed a VBA XLA Add-in application for use only inside our company, which helps in the linking of our database of financial information with Excel worksheets, through the use of several UDF's, supported by ADO/SQL and other business objects logic. This app became a solid, fast, and useful tool for us, somewhat similar to old DDE links, but much more sophisticated and flexible than that.
Recently we replaced the ADO/SQL portion of the system with a SOAP based Web-Service and XMLHTTP MSXML 6.0 technology, literally putting our database into the cloud. The goal was to transform the application into a product which could be used outside of our company. That work is already done, and its behaving quite well, with all functionality, load at startup, user authentication and session control login/logout, seamless integration with EXCEL, user-friendly messages, all done in a single 2.036Kb XLA Add-In file, spanning more than 15.000 lines of good VBA code. However, we feel that it cannot yet be distributed like it is...
We feel that in order to be successfully published as a product to our clients, this application must be transformed into compiled code instead of the interpreted VBA. There are many reasons to justify doing that, including security, speed, robustness, etc.. but we don't need to go into these details right now.
Our fist thought was to use VB6 and Automation Designers to quickly transform our VBA code into a VB6 Automation Add-in. Apart from the fact that VB6 is old technology, it seems that Automation Add-in's are not the ideal solution, for our app requires some interaction with Excel events and the End-User, at least during the "login" and "logout" into the web-service based database (and some other functionality that requires user interaction through forms), BUT is seems that Automation Add-ins are not suitable to anything other than UDF's only. Here we would like to learn about other's experiences with Automation Add-ins and end-user interaction.
So, COM Add-in's are the next option. Again, these allow interaction through menu buttons and commands, but do not allow UDF's in the worksheets. Or so we've read. Also, we have read that COM Add-ins can be made as Automation Add-ins (allowing UDF's after all), but that they will work as two separate entities inside the Excel environment (one COM and one Add-In), one half not communicating with the other. That's not acceptable to us. Again, we'd like to learn more about other's experiences in that regard.
Then there is Managed Code (.NET, Interop, and VSTO) as other viable options. However, while simple to get started with, it is not clear that Interop came to stay, and it is not clear what is the best strategy with managed code. Again, we'd like to learn about other's experiences in this realm.
So, the questions final is: given our requirements (i.e., load at startup, access to data through SOAP based web-service (MSXML 6.0), UDF's functions, login/logout session control, user-friendly error handling, etc.), and the fact that we already have 15.000 lines of good VBA code, which is the best technology for us to continue to develop this Excel component, in order to make it into an easily and safely distributable product? All comments and thoughts in that regard are very welcome.
I would take a look at Excel DNA: https://github.com/Excel-DNA
I had a very similar problem developing a VBA add-in that communicates with an ASP.Net Web API. Couldn't really give all the code out in VBA, so we needed a way of giving out compiled code. Connecting to the web service was freezing up excel making it harder for users working with massive spreadsheets with multiple API calls.
You can develop them in visual studios easily using VB so a lot of the code could just be copied and pasted over then changed slightly to fit the later version of VB you're using.
Add-ins compile to .xll 64-bit and 32-bit versions.
We handle all login and connection to the API in the Ribbon: https://exceldna.codeplex.com/wikipage?title=Ribbon%20Customization&referringTitle=Documentation
And you can use newer VB forms which are more user-friendly.
Connect to other .dll files for sharing logic across multiple add-ins.
The UDF we use which call the API can all run asynchronously freeing up Excels main thread.
https://exceldna.codeplex.com/wikipage?title=Asynchronous%20Functions&referringTitle=Documentation
It can run any VBA specific or CAPI code by queuing as a macro.
Pretty much do everything that you could do in VBA plus more.
Govert the Developer https://stackoverflow.com/users/44264/govert is very active on StackOverflow and forums. He's helped us out loads in the development. And Excel DNA is still being updated and worked on.
Let me know if you want some more details.
great summary of your project.
I'm also curious what other thinks about the "right" solution for such task. It won't be an easy decision though.
My short answer will be "stick with VBA" unless you are really concerned about stealing your code from the great VBA add-in.
The reason I'd go for VBA is that the longer I've been working with VSTO/COM the better I've found VBA to be the best for handling tasks that are closely related to Excel (understand MS Office) object model. I'm saying that even I have written almost zero lines of VBA code in last 4 years. I do understand that you are using webservices and other dependencies but I'd say that if you have good progress and the add-in is working as expected I'd NOT throw myself to a completely new world of development (VSTO & C#) just to be cool, you will gain not too much value of this, especially if you know that
deploying managed code is harder that just copy your add-in to a
folder, set registry, done
troubleshooting is a way harder with managed code, basically you will have to log much more and trying to reproduce issues that may not happen in your environment but happens in clients
re-engineering of managed code is not such hard so if people really wants to steal your code they can do that unless you use obfuscation
and the last and probably the most important for you as much as I'm aware there is no easy way to do UDFs in VSTO
I have very minimal experience with VB6/ COM Automation so I'd love to hear opinion of guys who have done something similar before
Re: the VSTO & UDF, at my job, we have a VSTO add-in that somehow handles UDFs for a large project. I'm not the main developer of the application but I believe we use Excel DNA there so do check it to explore the managed code option further
Hope that helps

Protecting Excel Module Code (.dll?)

We have just written an Excel VBA Oil and Gas Reservoir analysis program with lots of Active X controls and many code modules. (Excel 2007 with a 1000 x 1000 calculating C++ grid running in the background some of the more daunting calculations).
We are concerned about security. Locking the Worksheets or Workbook does not prevent a right-click/View Code, which exposes all of the modules.
Should we or can we convert or write these Modules into .dlls? Is there any literature to consult?? So far, I can't understand anything that I have found on the web.
Is there another way of protecting the module by password?
Comments and thoughts greatly appreciated.
From your post, it seems you need to set the VBA project protection: in the VBA editor, right-click the project in the Project Explorer window, select Properties\Protection.
That said, there is much discussion on SO and the web generally on the (lack of) strength of Office VBA protection and the ease of cracking it. Given you mention a 'C++ grid' component, it would seem you already use externally (to Excel) developed components: if security is a major concern it may be best to develop the whole app in VSTO.

Where do I get started with VBA and macros programming in Word 2007?

I just came to know that macros in Word/Excel/PowerPoint can be programmed. That is awesome because I've a Word document with 70 tables for styling.
I'm a programmer but I don't know VB, VBA or VB .NET. I'm confused with these three. I'm familiar with .NET programming using C#. Now I want to write new macros. Where should I get started? What are VB, VBA, VB.NET and which one should I learn? Please suggest some material.
If I learn for Word 2007 will that help with other Office applications life Excel & PowerPoint?
Here's a brief explanation of the different Visual Basics:
Visual Basic 6 (VB6, or classic VB)
Released around 1998, this was the last iteration of Microsoft's original "Visual Basic." It has the beginnings of object-oriented development, but it requires the Visual Basic Runtime for applications to run. A lot of companies have used VB6 for internal business applications. It was superceded by VB.NET and the .NET Framework.
Visual Basic for Applications (VBA)
VBA shares the same code base as VB6 and in 1996 was available to be licensed to developers to include in their own applications. This is how VBA appears in Microsoft Office, as an embedded language that can be used to control Office's various applications. It's important to remember that VBA, which is still used to code office applications, is over a decade old and may feel as such when one is used to working with .NET.
Visual Basic .NET (VB.NET)
VB.NET was a radical departure from VB6. Though subsequent iterations of VB.NET have been referred to in sequence (e.g. VB7, VB8, etc.) by many developers, VB.NET shares very little with VB6 and VBA other than the BASIC syntax. Many consider it more of a new evolution in BASIC rather than an evolution in Visual Basic. Because it's entirely different from VBA and VB6, you cannot not use VB.NET code directly in VBA.
Because VB.NET code compiles down to the same managed intermediate language code as C# and shares the same .NET APIs, you may feel more commonality between C# and VB.NET than VB6 and VB.NET from a programming perspective.
If you anticipate doing a lot of development in VBA, I would highly recommend the VBA Developer's Handbook, Second Edition, by Getz and Gilbert.
Learning the VBA syntax for Word will certainly help you when you go to use Excel, Access, etc. However, each application has its own API that provides a set of objects and methods unique to its domain. For example, I'm very familiar with programming in VBA in Excel and Access, but I have never done macro programming in Word. Although the code syntax would be the same, I'd have to learn Word's API to be able to program against it.
The nice thing about some of the Office applications (Excel, for example) is that you can record a macro, see what code it generates, and then tweak that code to do what you want. That's largely how I got started in programming.
There are some good answers here - I'd like to offer one more set of suggestions:
First, if supported in your environment, you can use Visual Studio 2005/8/10 and your C# skills to program against Office with "Visual Studio Tools for Office". See this thread for more details.
If you want to delve into VBA instead (which I personally love because development is so fast compared to VS), start with this article Ten Code Conversions for VBA, Visual Basic .NET, and C# which will show you samples from all three languages. Next, watch this webcast: Using Visual Basic for Applications (VBA) Every Day Is Easier Than You Think.
Thirdly- MSDN, read through this: Getting Started with VBA in Word 2010. 99% of it applies to Word 2007. There are many other articles linked from this one or you can always go to the main page of Office VBA Developer Center as a single jump page.
Then it's probably time to get started to directly program. See how things work, learn Word's Object Model, etc. You can always browse SO under the word-vba tag to see what other people are trying to do and the answers.
You always have MSDN.
If you go to Microsoft Word > Developer > Visual Basic and open up ThisDocument in the left menu, you will have the editor. If you press F2 you will get the libraries that can be used in Microsoft Word (the Object Browser). The easiest program would be the following:
Sub Hello()
MsgBox "Hello World"
End Sub
When you have the sub marked, press F5 (to run).
In the Object Browser you will have three different objects, properties, functions and events. When you see something that strikes your interest, go to the reference for word vba and locate it.
[If the link changes, you can find it in the tree under MSDN Library > Office Development > 2007 Microsoft Office System > Word 2007 > Word 2007 Developer Reference > Word Object Model Reference]
I think the easiest thing to do is to define a problem you need fixed and try to program it, similar to learning any other language. Don't make the problem to easy, but not to hard that you give up.

What StyleCop like tools are there for VB.NET

see also VB.NET Static Code Anaylsis
For better or for worst we now have a VB.NET coding standards document that is based on a C# coding standard as enforced by StyleCop.
For example
the number of spaces you should put in each side of a “+” sign etc
all instance Members (fields and methods!) must be access as “me.fieldName”
all shared members must be accessed as “className.fieldName”
As I tend to think:
If it’s in a requirements document it
should be check for by an automatic
system
I am looking for (ideally free) tools that will check for that short of rules on VB.NET code, as these are style issues that don’t make it into the compiled output, FxCop is not useful.
(I would personally match rather that we just check for important things like duplicated code and single reasonability for each class (so no more multi thousand line classes!), but as I need to keep to the coding standard document I wish to have a tool to help me do so.)
see also Enforcing using the class name whenever a shared member is accessed.
About the bounty.
I am looking for a list of VB.NET code checking tools, with a short summery of what each tool can do and its limitations. If the tools are not free, please include some ideal of cost.
Does anyone have experience using CodeRush/Refactor! or ReSharper with VB.NET to check for this type of coding style issues?
I know of no free source code analysis tools with good VB support. There are, however, at least two commercial tools that may be suitable:
submain CodeIt.Right
SSW Code Auditor
Personally, I prefer the CodeIt.Right rule authoring mechanism, so I would favour it if considerable custom rule development were planned. However, if you just want to use out-of-the box rules, Code Auditor ships with quite a few more code style rules than CodeIt.Right, most of whose built-in rules target the compiled IL (like FxCop).
The only ones I know of are:
Microsoft's FxCop
Of course, this only operates on compiled assemblies, so doesn't give the same functionality as StyleCop, and certainly won't help with things like naming schemes.
However, the closest thing is:
Aivosto's Project Analyzer v9.0 for Visual Basic, VB.NET and VBA
The full version is not free, but this is the closest thing to StyleCop for VB.NET that I can find.
There have been a number of calls for a VB.NET version of Microsoft's StyleCop, such as those in this thread on the code.msdn.microsoft.com site. That same thread also gives some good insight into why a VB.NET version doesn't exist.
I use ReSharper on a daily basis and I find it fine for both code formatting and for solving naming issues. It allows to configure how naming must be enforced, how issues are displayed (hint, suggestion, warning, etc) and provides a precise code formatter (space, paranthesis, line breaks, this qualifier, etc).
Note that I don't know if it can be run in batch mode.
Turning Option Explicit on by default is always a great idea and should be standard practice. I would argue it should be turned on by default in VS out of the box. But it doesn't come close to enforcing the out of the box rules that StyleCop does for C#, nor does it allow for you to create your own rules.
The whole reason for StyleCop's existence is because FxCop only works on compiled assemblies, leaving web projects out in the cold for a similar tool. With StyleCop, web developers get the same great rule enforcement and tight VS integration. It is a great tool for any C# developer.
It is unfortunate that it is only C# capable, a VB version would satisfy a large community that is left wanting something similar.
There already is a very good style tool built into the VB compiler. It is called Option Explicit On, put it at the top of the source code file or use Tools + Options + Project and Solutions + VB Defaults, Option Explicit = On. If that wasn't turned on previously there could be a mountain of errors when you compile your code after changing that.
If it is clean or already turned on, consider that you are 95% close to writing clean C# code and that the language doesn't really matter anymore.