I've run into a weird aspect (for me) of STATIC.

You'll remember my nifty little piece of code to <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=vb&Number=48114&page=&view= &sb=&o=&vc=1>skip over fields in text</A>? It's working, and it uses a static variable to remember the end point of any field-related stuff, so that on successive calls I can check "am I still in the current danger zone?", and if so, return a result saying "don't process this paragraph". However, if I'm out of the current danger zone, I test to see if I'm entering a new one, and so on.

The <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=vb&Number=19071&page=&view= &sb=&o=&vc=1>Static variable</A> holds the Range.End value of the current danger zone. It starts off as zero, then gets advanced as I work my way through the document.

So far, so good.


And if I have a document open on my desktop and call my TitleActiveDocument macro from the toolbar, everything works fine. The code for TitleActiveDocument steps through the document, paragraph by paragraph, avoiding the minefields of paragraphs that have fields, and so on.


Then I invoke the macro from another template, WebWord:
<pre>Function TitleActiveDocument()
Application.Run MacroName:="Indexer.Ix.cmd_TitleActiveDocument"
End Function
</pre>

and wouldn't ya know it, sometimes it works (and File, Properties shows me a title string, whoopee!), and then again, sometimes it doesn't. Apparently according to whether the leaves on the tree outside are waving up, or waving down.

Because "Variables declared with the Static statement retain their values as long as the code is running.", says the Word97SR2/VBA help file, and it is so.

When I ran the macro from the toolbar, the code stopped running after the Title had been set in the current document, so the Static dies a timely death. But when I call that macro repeatedly from another chunk of code, Static lives, yeah!


Try to imagine me as I process different sets of documents (as in a web site collection), and sometimes this document gets its title, and sometimes it doesn't! The Static was getting pushed up in value to an ever higher value, like the old mercury-bulb thermometers that record the maximum temperature. I could process a small document and Static might be left at 1,768, and the next document had all text at or below 1,768 declared as dangerous, which might not have mattered anyway, and I would have obtained some useful text from beyond the 1,768 point. However, if I processed the largest document first, then the Static would have been pushed to 29,002, or whataver on that first document, and that value would have remained to shadow out all the succeeding smaller documents. So that suddenly, a smaller document that got a title last time, didn't get one this time, and that document hadn't changed at all, at all, but other documents were getting titles, all on the same WebWord run, so obviously it wasn't a program bug .......