Word X: Macros vs. built-in commands
Posted by Pierre Igot in: MacintoshAugust 19th, 2003 • 6:36 pm
I have the following Word macro command in my Normal template:
Sub lowerCase() Selection.Range.Case = wdLowerCase End Sub
As you can see, it’s as simple as it gets. The only reason I created it is that I was getting tired of going to the “Format” menu, choosing “Change Case” and selecting “lowercase” and clicking on “OK” every time I wanted to change the case of a selection to lowercase — and, as far as I can tell, Word’s list of built-in commands does not include a direct command for “Change case to lowercase”. The only command it has is the “Change Case” command, which is equivalent to selecting the “Change Case” command in the “Format” menu (i.e. you still have to go through the “Change Case” dialog box).
Now, with the macro above, I can simply add it as a tool bar button to one of my Word tool bars, or assign a keyboard shortcut to it. And it works fine… in most situations.
Unfortunately, if you try to combine this macro command with Word X’s rectangular selection tool, it fails miserably.
For example, I often have lists that look like this:
1. First Item 2. Second Item 3. Third Item
I want to change the first letter of each item — and only the first letter — to lowercase. So I use Word X’s rectangular selection (option-select) to SELECT the first letter of each item only, like so:
And then I click on my custom-made “lowercase” tool bar button, and… Word only changes the first letter of the first item in the list!
It gets worse. Instead of using the rectangular selection tool, I use the new “discontinuous selection” tool provided in Word X (select first item, then command-select other items), like so:
(Note that the selection doesn’t look the same as with the previous procedure, even though technically it is supposed to be exactly the same.)
If I then click on my custom-made “lowercase” tool bar button, Word only changes… the first letter of the last item in the list!
(Needless to say, even if things worked properly with discontinuous selections, it wouldn’t be practical. Rectangular selection is the only practical solution in this case.)
Yet with either type of selection, if, instead of using my custom-made tool bar button, I go to the “Format” menu, SELECT “Change Case” and then “lowercase” and click on “OK”, things work as expected.
In other words, even though Word macros are supposed to access Word’s built-in commands and just let you use them in different ways, this is not actually true. There is obviously something in Word that prevents its built-in commands to work properly in all situations when called from within a Word macro.
How’s that for slopping programming?
(For more on the limitations and bugs in Word X’s macro architecture, please refer to the following two items in this blog: “Word X: Can’t repeat” and “Word X: Minor bug becomes major irritant“.)
August 19th, 2003 at Aug 19, 03 | 8:05 pm
Perhaps you’re just using the wrong property of the selection. Although MS don’t have a track record of being particularly good at software, judging from the language, a ‘range’ is a contiguous block of text.
So selection.range most likely is only one (first? last?) connected component of the selection, which in a way is what you asked for… To handle things properly you’d have to iterate over an array Selection.Ranges() or so. Or, if VB is/were (?) any good, all you’d need is selection.case with the selection object sorting out everything for you.
While I am happy to blame anything on Office, this could be in fact a bit of documentation mis/non reading on your side… which of course doesn’t mean there actually is a reasonable solution for this, depending on how much of a quick hack the support for discontiguous selections is.
Disclaimer: This has been written without knowledge about the current state of the mess that is MSOffice. My considerations are purely based on general principles rather than tha particular bit of software
Disclaimer 2: Although this may be a hint regarding the origin of your problem, I don’t want to encourage or recommend the use of said software in anyway. Any help would be unintentional.
August 19th, 2003 at Aug 19, 03 | 8:19 pm
Sorry, but
gives the error message “Method or data not found.”
I am not a VisualBasic excerpt. I’m just a Word “power user” who’s trying to customize the software. Most of the included documentation on VB is frankly beyond me. It’s documentation that assumes that you already know about what it’s supposed to help you learn about.
In the past, I’ve tried to spend a few hours “learning the ropes”, mostly to no avail. I’m not about to spend weeks working full time on mastering VisualBasic. I just want a “change to lowercase” command that doesn’t require half a dozen clicks!
I guess that’s just too much to ask.
August 19th, 2003 at Aug 19, 03 | 8:46 pm
Maybe that’s asking too much ;)
I spent quite some time having to use VB (to automate things for an office where I worked -> bad results and to write additional functions for excel -> quite good modulo bugs in Excel) and I am not sure you can really ‘master’ VB. It sometimes looks very convoluted (not a surprise, is it?) and messy. However, having a basic understanding of objects (as in ‘object-oriented’) and general structures in programming (loops, arrays etc.) won’t hurt, I guess.
I don’t know MS’ object libraries in VB, so I can’t say what works and what doesn’t. However, from natural language it should be clear that you can’t expect something called ‘Range’ to represent disjoint chunks of text.
But then again, you’ve probably used MS products for so long that trying to stick square pegs in round holes feels quite natural by now.
August 19th, 2003 at Aug 19, 03 | 9:30 pm
Yes, the fact that most of my programming skills predate the era of “object oriented” languages doesn’t help me in this situation. If I had the time…
I agree that a “range” doesn’t sound like something discontinuous — but, as you know, with MS, you never know :).
I have no idea how they actually implemented rectangular selection and discontinuous selection “under the hood” in Word X. My guess is that it is through after-the-fact hacks that are simply not accessible through VisualBasic.
And no amount of time spent using MS products will ever make sticking square pegs in round holes feel natural, as far as I am concerned. I have my pride! ;-)