Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 907
pecker Avatar asked Jul 09 '10 06:07

pecker


People also ask

Is VBA available in Office 2007?

You can access the VBA environment in Excel 2007 by opening the Microsoft Visual Basic window. First, be sure that the Developer tab is visible in the toolbar in Excel. The Developer tab is the toolbar that has the buttons to open the VBA editor and create Form/ActiveX Controls like buttons, checkboxes, etc.

What should I learn first VBA or macros?

Being able to create a basic macro in Excel is only the beginning in the process to become a really efficient and productive user of macros and VBA. If you really want to unleash the power of these tools, you must learn VBA due to the fact that, among others, recording a macro sometimes simply doesn't “cut it”.


2 Answers

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.

like image 148
Ben McCormack Avatar answered Sep 30 '22 13:09

Ben McCormack


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.

like image 20
Todd Main Avatar answered Sep 30 '22 14:09

Todd Main