« new Article on Office Online ... and Check Out MSCOM Home Today! :) | Main | Did you know you could do this ... (OneNote) »
Hi, everyone! An interesting comment was posted on Friday asking about using VBA to add options to a Word setting that don't appear in the UI... and I thought that was a great topic for a post.
The question at hand was, essentially, whether you can specify a format for insertions and deletions in a Word document comparison other than the options that exist in the dropdown lists in the Options dialog box. The simple answer is no, but it's not that simple.
The fact is, if there's logic to it -- you can do it with code. In this case, I say that the simple answer is no because you can't do it through the built-in feature. The question of whether of not something can be automated often becomes whether the cost justifies the benefit. At the simplest end of the cost-benefit analysis is basic VBA you might use to automate a cumbersome document production task. If writing the code takes as long as executing the task -- and you only have to execute the task once -- it's not worth reinventing the wheel (unless you just want the practice writing code, of course :) But, if writing the code will take less time, or if you will need to execute this task frequently, the benefit is very likely to outweigh the cost of doing that bit of VBA work. No matter how complex the task becomes, and no matter what development solution you're using (some things might require moving outside of VBA into something like Visual Studio Tools for Office), the cost-benefit question is almost always what determines whether or not the thing can be done.
When the tasks get more complex - like trying to make an Office program do something it doesn't do (such as adding to the formatting options for document comparisons), you might need to create your own solution rather than utilizing the built-in feature to do this -- a potentially far more complex task.
Sometimes, the things that seem to be so simple on their face are really some of the most complex because of the capabilities or limitations of the built-in feature. One of the most common cases is the one introduced with the original question here about adding options to built-in formatting options. The limitation is that the feature comes with a fixed set of constants. So I'm going to discuss a bit about what constants are, how to use them, and when you can get around them.
Constants are just what they sound like -- constant...a fixed set of options.
Note that, from this point forward in this post, I'm going to assume that you have at least a very basic exposure to VBA and its terminology. If you're brand new to VBA but would like to jump in -- check out a webcast of mine from earlier this year Tips and Tricks for Using Basic Word VBA Every Day. Note that it's a level 300 webcast, so it's designed for Word power users who are new to VBA, but not at all new to Word.
Constants in VBA are a set of options available to modify an object or property. The simplest type of constants are boolean -- true or false. For example, when you set a userform control (such as an option button) to be enabled or disabled, you use the control's Enabled property, like so:
frmMyForm.optAccept.Enabled = True
When you type that equal sign in the line of code you will get a little dropdown list that contains just the options True and False. These are the available constants for the Enabled property. (Note that not all features that are limited to constants provide a dropdown list and whether or not you see these lists can depend on your VB Editor setup.)
In the case of the document comparison formatting options, you get a larger set of constants because the constants represent all of Word's available options for how inserted or deleted text should be formatted (such as bold or double-underlined). Take a look at the constants available for formatting inserted text:

There are, however, some cases when you get more options in VBA than you do in the UI ... one nice, simple example is with Bold or Italic font formatting. This formatting always toggles when you use it in a Word document ... you see this if you add bold or italic to a paragraph style. If, for example, you have a style that contains bold text and you create a new style based on the first that has regular text ... take the bold out of the first style and it will get added to the second style. That's because the setting always toggles -- if you base one style on another and change the bold or italic setting, the style definition will be set to have the opposite bold or italic (true or false) from the base style. So when one changes, the other toggles in kind.
But, in VBA, you can set the Bold or Italic font properties to always be true, always be false, or to toggle - which is the default.
But there are many cases where you can't expand on the available constants without going outside of the built-in feature. One good example was a question posed to me a few weeks ago -- asking for a way to add custom text into the page numbering within a table of contents. The page number formats are limited to the page number formatting constants available to the field code.
But, if you generate your TOC from styles -- you can write a quick and simple VBA solution to get exactly what you want in much less time than you might expect. Your TOC will be able to be updated as easily as a Word TOC created with the built-in Word TOC feature ... but, it will not actually be a Word TOC. It will look, smell, and taste like one -- but it will be a workaround. Of course, in this case, it might just be a workaround that's as simple as, and every bit as good as, the real thing -- depending upon what you need. Stay tuned later in the week for the code to do that ... I've got to get back to work now :)
Happy Monday everybody!
Posted by Stephanie
TrackBack URL for this entry:
http://www.arouet.net/cgi-bin/mt/mt-tb.cgi/122
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)
Comments
I can't install the CD from your book. After I click the option Install Microsoft Office Document Designer and it takes me to the next page when I click on setup nothing happens. Help?
Posted by: Karen D. Mullins | September 12, 2005 04:52 PM
Hi, Karen,
Sure - I can help. When you click setup, you should get a dialog box with the options to either Run or Save the file (presuming that you're running on the current version of Windows XP -- on earlier versions of the operating system it might say something similar instead -- such as save or open).
You need to choose te Run option (or Open if you see that instead of Run), which will start the installer (be sure to take note of the installation key on the installation page, which you will need to complete the installation).
If you don't see the dialog box that gives you those options, try switching tasks (it might get hidden behind another active Internet Explorer window). To switch tasks quickly, press Alt + Tab. Or, just close all Internet windows before running the CD to avoid that issue.
With Windows XP SP2 and\or the MSN toolbar, you might also be blocking pop-ups that would block that dialog box from appearing -- so check the top of the Internet Explorer window to see if it's asking you whether you want to allow pop-ups or active x controls and allow them if prompted.
If this still doesn't do the trick for you - drop me an email. You'll see the Click to Contact link on the bottom right of my main blog page.
Best,
Stephanie
Posted by: Stephanie | September 12, 2005 05:02 PM
Can you please help me out? I need create a macro that will check the "name of the document" on the title bar in Word XP and compare it to the document name in the footer. If the names are different a save should be performed to update just the document number? Can this work with a document management system?
thanks
Denise
Posted by: Denise | September 16, 2005 02:37 PM
Hi, Denise,
Create an If statement that compares the Name property of the ActiveDocument object (i.e., ActiveDocument.Name) to the Value property of the field in question and executes the Save method of the ActiveDocument object if they are not equal (but, if your goal is to update the field value, you can use the Update method of the Fields collection object (ActiveDocument.Fields.Update) instead of a save).
However, I can't tell you whether or not you can save programmatically with a document management system in use. It depends upon how the document management system is integrated with the save function. Comparing the document name to the field, however, and updating fields, should not have to interact with the document management system at all.
Good luck with it!
Stephanie
Posted by: Stephanie | September 16, 2005 02:48 PM