Using VBA in Microsoft WordIn Mastering VBA for Microsoft 2010, Richard Mansfield defines Visual Basic for Applications (VBA) as “a programming language created by Microsoft that can be built into applications.” What does that mean and why should you care? You can do so much more with Microsoft Office applications, specifically Word, than you think. Here is a typical scenario and a good reason why you should care about using VBA: your boss gives you a task of collecting information from several employees. Instead of compiling the information, each person’s responses need to be on their own document. It would take a lot of work to make sure all the documents everyone fills out are formatted the same without VBA. With VBA, you can send them a formatted document where everything, like font and size, is already picked for them. If that doesn’t work for you, and you know your coworkers are going to try to change your pre-picked settings anyway, you can have the document formatted when you open it. You don’t have to do anything, it does it for you. Having all the documents formatted the same may not be that impressive, but getting that done in so little time will definitely be notable.
MacrosThis may be as close as us normal people can get to having a robot personal assistant. A macro is a command which you put into the coding. That Word document that you wanted format automatically upon opening would require a macro. So, like an assistant, macros can get things done for you. You can set them to run at certain times, when you do certain actions, or when something in your document changes. There are so many possibilities in which VBA can impact your performance at work and at home, if you choose. So how do you record a macro, you may ask? You can’t just open up a word document and start typing in code. Here are two ways to do it:
- In a Word 2010 document, click on “View” at the top. On the right, you will see “Macro,” and it will have a down arrow. Click on that arrow and choose “Record macro…” Once you choose your settings, you can begin editing your word document and the macro will record all your actions.
- The other way to record a macro is to click on that same down arrow and choose “View macros” and then “edit.”
LanguageVBA uses the Visual Basic programming language. A good place to start when you have language questions is http://msdn.microsoft.com/en-us/library/sh9ywfdk.aspx.
Uses for VBA in Word:VBA in UserForm “Most businesses use form letters or standard documents, in which a few details need to be changed each time the document is created. The best way to deal with this is to save the document as a template. You can add form fields for the variable content to make the job of completing the document easier. But for the greatest control and ease of use, you can't beat the versatility of a VBA UserForm. A VBA UserForm is a custom dialog box, to which you can add all the features you would normally see in a regular Windows dialog box like text boxes, combo boxes, option groups, check boxes and more.” The link below describes in depth three detailed steps to build an “automatic template” for Word users. The template consists of VBA prompts to guide uses in creating dialog boxes to enter information. The first part prepares the normal document with step-by-step pictures to compare to your very own screen. VBA UserForm is designed in the second step and shortly after entering the information in VBA code you’re just a click away from powering the transferring of information into the document. *Below is a link to find detailed steps 1-3: http://fontstuff.com/vba/vbatut09b.htm Using VBA to Enumerate All Word 2007 Documents Containing Revisions and Comments “Using VBA to automate Word 2007 is a good way to gather comments and revisions in documents and summarize them. This helps you get an overview of changes in a group of documents without examining each one separately. Group work greatly benefits from this feature, because everyone can now see the changes that are being made.” This feature helps when multiple people in a group are adding changes to a document. The “track changes” option follows each member’s additives. “By default, Word 2007 uses balloons to display deletions, comments, formatting changes and content that has moved.”
- StartReporting – sets up the environment, creates the Excel 2007 workbook and sets up the column headers, creates the Word 2007 application, and calls the Report subroutine.
- Report – loops through and finds all the Word 2007 documents in the specified directory, and passes along each document to the DoComments and DoRevisions subroutines.
- DoComments – loops through the comments in the specified Word 2007 document and populates the reporting worksheet with the name of the author and the text of the comment.
- DoRevisions – loops through the revisions in the specified Word 2007 document and populates the reporting worksheet with the author's name, the date of the revision, and its type.