*** Edited by GeoffW. Chris: I’ve removed the “pre” tags because text doesn’t wrap- There’s a lot of scrolling “right” to do otherwise ***
1. Option Explicit
I always use Option Explicit; anything that keeps my code strict is good.
2. Sub cmd_RemoveAllGraphics()
I try to previx user commands (can be invoked from a toolbar button) with cmd_
3. ‘ Tested: By the calls shown below.
Because my ProcStrip utility now sorts all procedures in a template, I am embedding the test code as a commented block immediately preceding the END statement.
To run the test you have to drag the commented TEST outside the procedure, decomment it, and run it.
The tests are supposed to (a) give you a chance to see how you might call the procedure (2) give you a chance to single-step through and observe the process (3) provide a testbed for any future changes.
4. Function boolClearAllGraphics(doc As Document, strReplace As String) As Boolean
I use, I think, Reddick naming convention. I try to be consistent at any rate.
My current theory on procedures (subroutines and functions) is that (a) functions are better than subroutines because they return a value ( writing a procedure as a subroutine is an admission of failure on my part © because digital computers are essentially mondaic and dyadic beasts, functions should contain no more than two arguments; this promotes a trend towards small building blocks of reusable code. Each function composes two arguments into one value. (d) any procedure that the user isn’t allowed to see should be written with an argument so that it doesn’t show up in the Word97 Tools, Macro, Macros list (e) The function doesn’t exist that can’t be made to return a value, even if it is only a boolean success/failure.
5. Selection.Find.ClearFormatting
I make no bones about it. I use Tools, Macro, record to initiate many of my efforts. It does not produce efficient code, but it produces code efficiently.
6. .Text = “^g”
7. .Replacement.Text = strReplace
I usually try to anticipate parameters to low-level functions. In this case I have anticipated the .Replacement string (you could replace a graphic object with the string “Graphic”); my mind went blank with the .Text string.
8. ‘Sub TESTboolClearAllGraphics()
9. ‘MsgBox boolClearAllGraphics(ActiveDocument, “”)
10. ‘End Sub
Typical TEST procedure, capitalised letters TEST followed by the name of the procedure being tested. Minimal work. MsgBox lets me see the result from Word without labouring through the Debug window.
Some TEST procedures carry a comment on each line predicting the result; very useful for re-testing after a change in code. Indebted to Poole&Waite for this approach.
11. lngRemoveShapes = ActiveDocument.Shapes.Count + ActiveDocument.InlineShapes.Count
I’s rather increment a local counter in each loop, but I’d get yelled at as “inefficient”. A local counter gives me more control over the debugging/inspection process, which is where most of my life goes