Word 2004 Crashes on cut or copy or forward delete

E

Ed Togami

Word 2004 crashes on a brand new document with a copy, cut or forward
delete operation.

No warning, just gone. This is after a fresh startup, nothing else
running. Was occurring on Office X prior to this. Office 2004 was
installed to see if it would correct it.

Other programs do not have this problem (i.e. AppleWorks 6)

Machine is an iMac 17 inch, running Mac OS X (10.3.5), 512 MB RAM, 60
GB HD space available
 
T

Tony

etogami,

Sorry about it but... welcome to the club!

:-/

I have been experiencing exactly that issue with Word 2004 on Mac OS X
10.3.5 and previous versions of X and Word.

So far no fix even following all the directions on this forum.

As you say, other applications never crash and the OS X is rock solid.

There is something wrong with Word for Mac OS X.

Hopefully Microsoft will deliver a fix soon. Because it is utterly frustrating!
 
B

Beth Rosengard

J

John McGhie

Hi Ed:

The problem is in your Normal template.

When you installed Office 2004, Office 2004 preserved your Normal Template
so you keep your customisations. You also kept your problem.

Quit Word and delete your Normal Template. Empty the trash to make sure
it's gone, then re-start Word. Everything will be fine.

Cheers


Word 2004 crashes on a brand new document with a copy, cut or forward
delete operation.

No warning, just gone. This is after a fresh startup, nothing else
running. Was occurring on Office X prior to this. Office 2004 was
installed to see if it would correct it.

Other programs do not have this problem (i.e. AppleWorks 6)

Machine is an iMac 17 inch, running Mac OS X (10.3.5), 512 MB RAM, 60
GB HD space available

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 
T

Tony

Well, in my case removing the Normal Template does not fix it.

And I must say that Word 2004 is the only application that crashes of
all the dozens of applications that I use.

And yes, I have tried all tips on this forum. It is frustrating.

---
 
J

John McGhie

Hi Tony:

My answer was specific to Ed's problem.

This problem is caused by a "corruption" in the internal structure of the
document. You "can" inherit it from the Normal template if that is corrupt.
But it can also occur spontaneously in a document (particularly if it has
been chewed by users who do nasty things like use blank lines close to
section breaks and tables, or leave Change Tracking on when they don't mean
to.

The corruption is often, but not always, in either the numbering or the
tables. And once it happens, it will copy from document to document if you
copy a document that has it.

In Word 2004, one of the most reliable fixes is to save each document that
does it as a "Web Page". Make sure you do not have the "Save only display
information..." option checked in Web Options. Close the document. Open
the HTML version and re-save it as a document.

This completely re-expresses the document structure. In doing so, Word
throws away anything it can't understand.

The older method of doing this was to "Do a Maggie", named after Maggie on
the Tech Whirler's list who advised people prolifically on the art.
Basically, you copy everything in the document except the last paragraph
mark and paste into a fresh new document. That fixes many of the possible
causes of corruption (but not bad tables or paragraphs).

You will need to do this to every existing document that is causing
problems. Note that I am worried by your term "removing" -- I hope by that
you meant trashing it and emptying the trash. If the thing is in the trash
on the Mac, it can be still in service so you keep your problem.

There is a possibility that the reason that Word is the only application you
have which is crashing, is due to the fact that Word is the only application
with which you open corrupt Word documents :)

Cheers

Well, in my case removing the Normal Template does not fix it.

And I must say that Word 2004 is the only application that crashes of
all the dozens of applications that I use.

And yes, I have tried all tips on this forum. It is frustrating.

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 
T

Tony

John,

Thanks for the compressive reply. It is much appreciated.

Where is that "Change Tracking" thing? I cannot find it.

Such "save as web page" is interesting. I did not know about it.

The "copy all except the last carriage return2 worked for me sometimes
to fix corrupted Word files.

By "removing" I meant trashing and them choosing to empty the Trash.

Word 2004 SR1 on Mac OS X 10.3.5 (and previous versions) crashes with
both new (created from scratch) and old documents created on my Mac or
from other people (Mac or Windows).

It is really frustrating.

I guess these unexpected quits of Word might be due to the peculiar
memory handling of Word. And you can test it if you just copy a large
amount of text in a Word file and then try to quit Word. Your are asked:

"You placed a large amount of text on the Clipboard. Do you want this
text to be available to other applications after you quit Word?"

No other application does it and it demonstrates that Word handling of
memory is not the standard of the Mac OS X system but a peculiar and
particular one. In other words, the clipboard of Word is not the same
as the clipboard of Mac OS X. THAT IS NOT GOOD!!!

And since most of the unexpected quits of Word are produced when you
copy/cut/paste text, I guess that such peculiar Word memory handling is
where the culprit is.

If Only Microsoft did the things as all the other Mac developers do!

I repeat: It is really frustrating.

Thanks again.

---
 
B

Beth Rosengard

Hi Tony,

First, you *should* know about "save as web page" since you said you tried
all the fixes at <http://word.mvps.org/MacWordNew/TroubleshootingIndex.htm>
and this technique is in the article about corrupt documents at
<http://word.mvps.org/MacWordNew/DocumentCorruption.htm>. This makes me
wonder if you tried the other potential fixes you were referred to.

Secondly, "change tracking" or "track changes" is on the Reviewing toolbar
which you can activate from the View menu. Searching Word's Help for
"change tracking" brings this information up in a second.

Thirdly, I believe both Daiya (Dayo) and I have made the point that your
problem with Word crashing is *not* a general Word 2004 problem since the
large majority of people do not experience it! There is some problem with
your system, be it corrupt documents/templates, corrupt fonts, conflicts of
some kind or whatever. Finding the cause is the key to stopping the
crashes.

Fourthly, I believe the reason that Word asks if you want to keep a large
amount of text on the clipboard is because if you say Yes, then it has to
keep a temporary document containing the text in active memory. If you say
No, then it doesn't. I don't see a problem there but perhaps I have the
explanation wrong. I'm sure that when John comes back, he'll explain it
more fully.

--
***Please always reply to the newsgroup!***

Beth Rosengard
Mac MVP

Mac Word FAQ: <http://word.mvps.org/MacWordNew/index.htm>
Entourage Help Page: <http://www.entourage.mvps.org>
 
D

Daiya Mitchell

and this technique is in the article about corrupt documents at
<http://word.mvps.org/MacWordNew/DocumentCorruption.htm>. This makes me
wonder if you tried the other potential fixes you were referred to.

That page does say "if it only happens in some documents", implying it may
not apply to a systemic problem... Though I too would have expected someone
with such frequent crashes to have explored that site forwards and
backwards.

But Tony, some questions that occurred to me, just in the attempt to find a
common factor in the crashes.... You said it's about every 4th time, any
consistency in the type of doc or type of action?
Word 2004 SR1 on Mac OS X 10.3.5 (and previous versions) crashes with
both new (created from scratch) and old documents created on my Mac or
from other people (Mac or Windows).

Is it possible that there's something corrupting documents on your computer?
Any add-ins? I know you said you use EndNote, how do you use it? Do all
documents from others crash, or usually longer more complex documents? It'd
be so simple if they were all documents EndNote has a hand in.... :)
"You placed a large amount of text on the Clipboard. Do you want this
text to be available to other applications after you quit Word?" ....
And since most of the unexpected quits of Word are produced when you
copy/cut/paste text, I guess that such peculiar Word memory handling is
where the culprit is.

Do your copy/cut/paste crashes always occur when you are dealing with large
amounts of text?

Do you use any of the known corruption factors--Versions, Master Documents,
Nested Tables, Fast Saves (checked in Prefs)?

Daiya
 
T

Tony

Beth,

Thanks for the feedback.

Believe it or not I did try all tips on those pages. But when I said
that Word crashes I meant with any document, not a particular corrupted
one. Therefore, the issue is not a corrupted document --although
sometimes that was the case-- but rather a general issue with any
document. Usually long documents with tables, but sometimes long
documents with only text and even short documents with only text.

I know about "track changes", which I use from "Tools/Track Changes/
Highlight changes" but never heard before about "change tracking". I
understand now that they are the same.

I usually have off "Tools/Track Changes/ Highlight changes/Tack changes
while editing" but sometimes I have it active because I need it active.

And if then I spell check documents with English and Spanish, Word
usually crashes while adding Spanish words. For those of you who have
read my other posts about the Spanish custom dictionary issue, I must
say that I have done it (clicking the Add button) after selecting the
Spanish custom dictionary as "none" language in preferences. Otherwise
the "Add" button goes dimmed and grayed (inactive).

As far as I understand the Word crashing issue is something about the
interaction of my system with Word itself. I mean, no other application
crashes on my system except Word. I have clean installed the whole
system, removed and deleted Office and re-installed it again. Yet the
problem persists.

The reason Word asks if you want to keep a large amount of text is not
only that one you indicate. Because if you say that you do not want to
keep it, once Word quits you cannot paste such text in other
application like an eMail client. But you can paste it before quitting
Word.

Therefore, such text copied from Word is not stored in the Mac OS X
clipboard --as it should be to behave like other applications do-- but
inside the particular memory manager or Word.

And choosing "yes" to the transfer question when quitting Word simply
transfers such text from the private Word memory management system to
the Mac OS X system clipboard so that it can be used with other
application after Word quits.

And that supports my idea that this crash issue has to do with the
particular non-standard (ie., non-Mac OS X system clipboard-based)
memory management of Word.

Hopefully, someday this crashing nightmare will be over.

Thanks again.

---
 
T

Tony

Daiya,

Thanks for the explanations.

The only consistency is that the crash strikes only when I copy, paste
or cut text. Sometimes also when I drag text. And the more complex the
document is, the more likely it will strike. For instance, I experience
it with short documents with text, but if they are long and with tables
it is more likely.

I must also say that I always work as "View/Page Layout". Just in case
you work with other views.

And if I activate "Tools/Track changes/Highlight changes/Tack changes
while editing" and then spell check documents with English and Spanish
text, a crash will likely strike while adding words to the Spanish
custom dictionary (set as "none" language in preferences, so that the
"Add" button shows active).

Crashes are also more likely when spell checking that way the Word
files created on Windows machines.

I do not know if such crashes have the same cause. I guess the spell
checking crashes are a different kind of crash than the
copy/paste/cut/drag ones.

I had add-ins from Acrobat PDF, EndNote and MathType, but removing them
did not fix it. I have not been using EndNote 7 for a while since it is
not compatible (CWYW) with Word 2004.

When a crash hits, usually opening the file again and performing the
same task usually brings the crash again.

I may copy/cut/paste large amounts of text, but also small ones (even a
single word) when the crash hits.

The more complex the document and the action the more likely the crash,
but as said also on simple situations.

It seems to me that saving frequently (Command S) makes crashes less
likely. I do not have active the autosave preference.

I do not have active any of the preferences for:

"Word/Preferences/Save/Allow fast saves".

I cannot find the preferences for "Versions, Master Documents,
Nested Tables". Are they in preferences? As far as I know I never use
such features.

Thanks.

---
 
B

Beth Rosengard

Hi Tony,

When a crash hits, usually opening the file again and performing the
same task usually brings the crash again.

I may copy/cut/paste large amounts of text, but also small ones (even a
single word) when the crash hits.

The more complex the document and the action the more likely the crash,
but as said also on simple situations.

This all makes sense if indeed the documents are corrupt and the longer and
more complex (i.e., tables and graphics) the documents are, the more likely
they are to be corrupt. That doesn't mean it can't happen with short docs
also.

My suggestions are these: First create a brand new Normal template (save
the old one under a different name). If you can do without any
customizations you may have, do *not* transfer them over to the new Normal.
If you can't do without, recreate your customizations wherever possible
instead of transferring them. (Could you be using any questionable macros?)

Now, the first time any document crashes, immediately open it and
"uncorrupt" it according to the instructions in the article I referred you
to (or that John outlined). Then open the newly-uncorrupted document and
perform the same task that caused the crash. Does it crash or not? If not,
keep track of what you do to that document and how often you work on it, so
that if it crashes again perhaps we can nail down the cause.

Do this with *every* document, short or long, the first time it crashes and
see what happens. (My guess is that most of these documents were ported
over from another computer, most likely a PC? And that a history of track
changes, along with tables, are responsible for the corruption.)

If a document which you have "uncorrupted" crashes Word the same way when
you perform the same task ­ and if it contains table(s) ­ uncorrupt the
tables individually, as also explained in the article (convert table to text
and then immediately convert text to table again). What happens now?
It seems to me that saving frequently (Command S) makes crashes less
likely. I do not have active the autosave preference.

Do you mean AutoRecover? There's no reason not to have it on, set to the
default of every 10 minutes. I think we discussed this before.
I do not have active any of the preferences for:

"Word/Preferences/Save/Allow fast saves".

I cannot find the preferences for "Versions, Master Documents,
Nested Tables". Are they in preferences? As far as I know I never use
such features.

These are not preferences. They are features. Are you sure you don't use
nested tables (tables within tables).?

--
***Please always reply to the newsgroup!***

Beth Rosengard
Mac MVP

Mac Word FAQ: <http://word.mvps.org/MacWordNew/index.htm>
Entourage Help Page: <http://www.entourage.mvps.org>
 
T

Tony

Beth,

Thanks for the reply.

I must say that I rarely use tables or documents from other people. I
do sometimes that, but most of the time I work with brand new documents
created by me on the Mac without pictures, tables or any other thing.
Just text files. They are not usually long (say, about 10 pages or
less) and as said I always work as "View/Page layout".

I do not use macros. I have never used nested tables for sure.

As far as I understand there are two different situations:

1 - Word unexpectedly quits and the document (simple or complex)
corrupts for some reason (before or at the Word crash). I have had
examples of this and the workaround was to cure the document with the
tips on these forums and web links.

2 - Word unexpectedly quits, but the document is not corrupt. That is
the main concern for me. Because Word may crash at any time. it is not
here a matter of "healing" a corrupt file.

Yet, in any case why on Earth all these problems? This is shocking.
Why Word should crash with uncorrupted documents? Why Word documents
should corrupt? Why the Word preferences or Normal templates or font
caches or this or that should corrupt? This is ludicrous. It seems to
me that Office in general and Word in particular have not optimized
code but a lot of legacy code that is causing all these problems.

I have also found that bookmarks ("Insert/Bookmarks") also contribute
to the crash. I have never ever created a bookmark, yet many of my Word
files are plagued with them. Hundreds of them on a single document (you
can see them as weird brackets after activating
“Word/Preferences/View/Show/Bookmarks". But Word also crashes if I
clean them all.

They get there simply when you paste text from web pages, eMail, other
Word files, etc. And worse of all, you cannot delete them at once but
one-by-one at "Insert/Bookmarks". Imagine deleting 800 of those nasty
bookmarks one-by-one on each of 1,000 Word files I may have. I wish
Microsoft would implement a "delete all" button on such
"Insert/Bookmarks" menu.

I have created a Normal template and new preferences for Office, Word
and fonts and all that so many times I just cannot remember. And with
it go away all my Word customizations. For instance, I hate Word
selecting the whole word when I just want to select some character. And
I hate Word autosaving (yes I want to do things when I do want and not
when Word wants) or red-underlying Words not included in the dictionary.

I do not want Word to get in my way. And using the default settings it
does all the time. I particularly hate that. I hate typing a word and
then finding a bullet or number that I have not typed on the left. I
hate opening a Word file only to find that Word has made editing that I
do not want (like the numbering and bullets or placing “st†as a
superscript after a number or capitalizing words I do not want to
capitalize). The only automatic features that I use are smart quotes
and automatic page numbering.

I have grammar correction turned off because Word does not correct
grammar well enough and even makes mistakes. I also have my own margins
and not to capitalize any word or change anything automatically or do
any autonumbering or bullets. I want to do things myself. Well, every
time that I trash the Normal template and the myriad of scattered Word
preferences I must re-configure again. A real pain.

I wish at lest Word had a preference to turn off all those features.
And then let you activate only the ones that you want to activate.
Every time that I trash Word preferences and Normal template and so on
it takes me several days to return my preferences to what I want.

But, OK, I have just deleted them again.

When a crash happens and I "uncorrupt" the document (in most cases it
is not corrupt at all) as you indicate two things may happen:

1 - I can reproduce the crash.

2 - I cannot reproduce the crash.

That depends on the document and also on the particular situation.
Usually I cannot reproduce the crash immediately; even when I do not
"uncorrupt" the document. Unless of course the document is really
corrupted, in which case the cure is to uncorrupt it and then the crash
goes away until a few minutes, hours or days later.

But most of the time the document is not corrupted. And working with if
for some minutes, hours or days (usually minutes) will eventually bring
another unexpected crash doing other different task which usually is
also a copy/paste/cut/drag of text as previously explained.

Open it again and work with it to eventually have another crash in a
few minutes or hours (usually minutes). That is my experience with
Word. Really frustrating.

What I do to those documents is: open it, write text and sometimes
copy/paste/cut/drag some text within the document or from other sources
like web pages, eMails, etc. That simple. And eventually when doing it
Word unexpectedly quits. Sad.

I must say that I also work with other Word processors using similar
files and similar tasks and they never crash:

AppleWorks
BBEdit
Mariner Write
Mellel
Nisus Writer Express
TextEdit
TextWorks
Ulysses

It is probably something of my system, but it is also something of Word
because the only applications that crash on my Mac are the Office ones
(Word, Excel and PowerPoint, which are the only ones that I use from
Office). To me Office is really buggy. A real pain to use it but I must
do it because of compatibility with the rest of the World. I really
hate the situation, but I am "forced" to use the buggy Word. This is
not fair but is the real World.

I only wish Microsoft would build a real good package, but Office for
Mac is not certainly such a product. At least in my experience. For me
the best Word processor ever is MacWrite Pro. So easy, intuitive and
robust that new system versions did not require any MacWrite Pro fix or
update. Never ever crashed after years of intensive daily use. But
sadly it is not available for Mac OS X and nobody uses it these days
(Word killed it as also did with WordPerfect and others). Again, that
is not fair but is the real world.

The last one is that I cannot use the Spanish custom dictionary (see my
other post) since May on Word 2004 (no problem on earlier versions of
Word). After reporting it here and to Microsoft web site feedback many
times I find that Word 2004 SP1 has not yet fix it. This is
frustrating. Are all these flaw and bug reports --even the ones sent
directly to Microsoft web site feedback-- a monumental waste of time.
It seems to me. At least in my case.

Also lots of crashes while spell checking and adding words on documents
with English and Spanish. But that is another "endless" story of Word.

What else can I say? I could write a book on Office issues.

Sorry for all these troubles.

I only wish Microsoft would build real good software for the Mac.
simply because I am "forced" to use it every day. These are the sad
consequences of a monopoly. And I would not mind if only Office worked
fine. But it does not. At least for me. And, as said, all the rest of
applications including other word processors work like a charm.

Thanks.

---
 
D

Daiya Mitchell

Some thoughts:

Tony, you might want to try Normal View, just because using Page Layout
makes Word work a little harder, conceivably a little less stress could
decrease the frequency of crashes. That's grasping at straws, but...
It seems to
me that Office in general and Word in particular have not optimized
code but a lot of legacy code that is causing all these problems.

They do have a lot of legacy code, but it's not causing these problem for
everybody....
I have also found that bookmarks ("Insert/Bookmarks") also contribute
to the crash. I have never ever created a bookmark, yet many of my Word
files are plagued with them. Hundreds of them on a single document (you
can see them as weird brackets after activating
³Word/Preferences/View/Show/Bookmarks". But Word also crashes if I
clean them all.

They get there simply when you paste text from web pages, eMail, other
Word files, etc. And worse of all, you cannot delete them at once but
one-by-one at "Insert/Bookmarks". Imagine deleting 800 of those nasty
bookmarks one-by-one on each of 1,000 Word files I may have. I wish
Microsoft would implement a "delete all" button on such
"Insert/Bookmarks" menu.

Okay, that sounds like really useful information. Word does occasionally
create bookmarks like that, but not usually. I don't think that happens to
everyone either (and just turned on bookmarks view so I can check), so
perhaps that's related to all the crashes. Anybody know what controls
creation of Bookmarks?

Also, if no one comes back with one, I think I should be able to dig out a
macro that deletes the bookmarks for you. Still have to run it once per
doc, but should be better.
Well, every
time that I trash the Normal template and the myriad of scattered Word
preferences I must re-configure again. A real pain.
We all have most of those things turned off, it's not related to your
problem.
I wish at lest Word had a preference to turn off all those features.
And then let you activate only the ones that you want to activate.
Every time that I trash Word preferences and Normal template and so on
it takes me several days to return my preferences to what I want.
Okay, you should look here, where JE McGimpsey spells out a procedure to run
a macro on launch that sets all your preferences as you like. Then you
don't have to worry about trashing prefs. You should also keep most of your
customizations (toolbars, etc) in a global template instead of Normal, and
trashing Normal becomes a lot less annoying. Same article tells you how.
http://www.mcgimpsey.com/macoffice/word/globaltemplate.html

I know you said you don't use macros. Using macros is a really good way to
reduce your frustration with Word (or at least transfer it to frustration
with macros).

By the way, it's not clear whether you get any temporary relief from
deleting Normal or deleting Preferences. Can you tell?
I must say that I also work with other Word processors using similar
files and similar tasks and they never crash:

AppleWorks
BBEdit
Mariner Write
Mellel
Nisus Writer Express
TextEdit
TextWorks
Ulysses

If you are constantly switching the *same* file in and out of different
software programs, that could be a factor...each program may leave a little
bit of residue on the file.

Daiya
 
B

Beth Rosengard

Hi Tony,

I have created a Normal template and new preferences for Office, Word
and fonts and all that so many times I just cannot remember.

In addition to Daiya's excellent response, note that I did not suggest you
trash the preferences again, just the Normal template. I don't think
preferences are the cause of your problem so there's no need to keep
trashing and resetting them.

I have one last suggestion. Send me 2 or 3 documents that seem to crash
pretty reliably (not more than 10 pages). I'd like to see if they crash my
installation of Word as well. When you email, put "TONYDOCS" somewhere in
the subject so I don't automatically send your post to the trash.

--
***Please always reply to the newsgroup!***

Beth Rosengard
Mac MVP

Mac Word FAQ: <http://word.mvps.org/MacWordNew/index.htm>
Entourage Help Page: <http://www.entourage.mvps.org>
 
D

Daiya Mitchell

Update:

Bookmarks just randomly showed up in my Word 2004 doc, when I copied text
from it, and pasted the text into Safari, then went back to my Word doc,
suddenly I had bookmarks. Note that I had not pasted anything into Word.
This is the first time I have noticed this behavior since turning on show
bookmarks several days ago, but I have not been using Word very intensively.

Here is a macro to delete bookmarks (maybe. I edited it from a more complex
one that I didn't really understand, so I don't totally understand this one
either, but it just worked in my single test). Tony, you might want to test
it on a copy.

Sub DeleteBookmarks()
'
Dim i As Long
ActiveDocument.Bookmarks.ShowHidden = True
For i = ActiveDocument.Bookmarks.Count To 1 Step -1
ActiveDocument.Bookmarks(i).Delete
Next i


Go to Tools | Macros, Macros.... In the Dialog box, type DeleteBookmarks,
then click Create, which flips you into Visual Basic. Paste the above text
in where the cursor is (hope for no error messages). Word | Close and Return
to Word. In a copy of a doc with a lot of bookmarks, go to Tools | Macro,
Macros, select DeleteBookmarks, and click Run. See if the bookmarks go
away. Mess with the doc to see if it seems to crash less with no bookmarks.

Let us know whether that has any effect.

DM
 
J

John McGhie

Hi Daiya:

Oh dear: Daiya's into VBA now -- no place is now safe...

OK:
Sub DeleteBookmarks()
'
Dim i As Long

The "Dim" statement is a throwback to the original Basic language. It means
"Dimension an area of memory to hold this thing." In VBA you do not have to
DIM anything, the compiler will automatically instantiate any variables it
needs when it runs.

However, if you set the line "Option Explicit" at the top of your module,
the compiler will then insist that you DIM everything. This is a VERY
useful protection if you are a bad typist inclined to misspell your variable
names, like I am.

OK, it's difficult to misspell the variable name "i". The rest of that
statement "as long" "types" the variable. It defines the kind of content it
can contain (which also, in VBA, defines the size of the thing in memory).

In this case, you are defining i to be a "long integer". From the Help: "
Long (long integer) variables are stored as signed 32-bit (4-byte) numbers
ranging in value from -2,147,483,648 to 2,147,483,647. The type-declaration
character for Long is the ampersand (&)."

While we know that no document can possibly contain two million bookmarks,
and we could assume that a simple "integer" type would do us (and save two
bytes of memory) it is useful to know that Word VBA is a 32 bit machine.
Thus a "long" is its native word size. In this case, the variable will be
read and written on every iteration, and Word handles long variables much
much faster than integers. It has to do extra processing to write data to
and from a "half word". So in VBA always use a long unless you have a very
good reason to use something else: two bytes extra memory will not kill you,
but slow macros will.

ActiveDocument.Bookmarks.ShowHidden = True

That statement is not necessary in this particular macro. There are two
kinds of bookmarks: those whose names begin with an underscore, which are
hidden from normal display, and others which are visible in the document if
you turn on Show Bookmarks. The statement makes the hidden ones visible.

The statement itself is a classic VBA "dot delimited" statement, where the
"dot operator" means "within the foregoing". ActiveDocument is a "property"
that provides a shorthand way of saying "The document the cursor is in" (the
one that has focus). Word can have twenty-odd documents "open", but only
one at a time can be "active". Since you have to write several lines of
code to explicitly retrieve the name of the document you want to work in,
then store it, most people use "ActiveDocument" where speed doesn't matter.
In this case, you only want to do it once, so if it takes Word a millisecond
to figure out what "ActiveDocument" means, who cares? But if this statement
was inside the loop, you would Dim and Set a variable to store a reference
to the active document in memory. This would be a thousand times faster.
ActiveDocument is actually a property of "Application", but nobody bothers
to write "Application.ActiveDocument" because if you are running a macro,
you must be running it within Word, and so ActiveDocument is explicit in
itself.

This next statement is the only one Daiya actually didn't understand. It's
a particularly convoluted construct due to the peculiarities of the
bookmarks collection.

Normally, you would code it as:
For (each of) TheBookmarks in (the) ActiveDocument
.delete
Next

The For...Each...Next statement is one of the most stunningly powerful and
useful "nice to have's" in VBA. Behind the scenes, VBA adds a whole lot of
code to define a variable, load it with the number of "things" in whatever
collection you use it with, then work down the collection from 1 to last,
doing whatever (in this case, deleting) to each thing as it comes to it.

However, in the case of a physical object such as a bookmark or a hyperlink,
if you delete it, Word instantly renumbers them all. Since the variable
(TheBookmarks in the example) is actually simply an Item number, the
sequence would be this:
First Word would find bookmark number 1 and delete it. Then it would
increment TheBookmarks from "1" to "2). Then it would shuffle the bookmarks
collection and renumber them all. It would then delete bookmark "2". See
the problem? Word has renumbered the bookmarks. The "next" bookmark is now
Item number "1" in the collection. But you are deleting number "2", which
used to be "3". If you run this loop "forwards", you end up deleting every
SECOND object.

To handle this, Daiyo has coded the loop to run BACKWARDS, deleting each one
from the BOTTOM of the document to the top. And to do that, she had to do
it the hard way. She had to load her own variable: "For i =
activedocument.bookmarks.count" means "Let i become the count of all the
bookmarks in the document". The rest of the line: "To 1 Step -1" causes
Word to decrement the variable instead of incrementing it. Usually in a
For...Each...Next you do not need to specify the "Step" parameter, because
the default is +1, which means the variable increases by 1 on each run
through the loop. In this case, you have to specify Step because you want
the increment to be "-1", which subtracts 1 from the variable "i" on each
pass.

The next but of the statement deletes the bookmark.
"ActiveDocument.Bookmarks" says "Of all the bookmarks in the active
document, choose the one indicated by the value of "i" and delete it.

The last part of the statement is "next i", which simply tells the compiler
where the statements embedded in the loop end.

For i = ActiveDocument.Bookmarks.Count To 1 Step -1
ActiveDocument.Bookmarks(i).Delete
Next i

Had we wanted to take a purists approach, we could have coded this as:

With ActiveDocument.Bookmarks
for i = .count to 1 step -1
.item(i).delete
next ' i
End with

And in the process, we would have improved the speed of the macro by more
than a thousand times. And made the code completely incomprehensible, and
thus unmaintainable. When coding VBA, performance is rarely a
consideration. But maintainability is ALWAYS paramount. Particularly if it
is YOU who is going to have to figure out what the HELL this thing does in a
year's time :)

The reason the second version is faster is because because of a technique
called "early binding". Word only has to figure out where the bookmarks are
in the active document once, in the second example, instead of on each pass
through the loop. Similarly, the ' character on the last line turns
everything following it into a comment. In this case, that tells Word that
the For... Each... Next statement ends on this line, but prevents Word from
having to evaluate the variable "i". If you leave out the apostrophe, Word
will first find i then open it and move its content into memory, before
discovering that it doesn't need to know what is in i at that point. It
wastes about 300 machine cycles on each look.

On the other hand, if you do not put "i" on the last line, and you start
nesting For... Each... Next statements vigorously, in a year's time you will
find it almost impossible to figure out which loop ends at that point. Word
doesn't need to evaluate "i" until it gets to the top of the loop, when it
can do so with a simple bit-wise operation. It doesn't need to know "how
much" is left in the variable, it needs only find out if there is "any"
left. It can do that in four machine cycles compared with 300.

Similarly, with the first example Word has to evaluate the entire collection
of bookmarks twice on each run through, once to figure out how many there
are, and again to find the one to delete. This will burn maybe a million
CPU cycles on each pass.

But faced with the choice of writing it the first way and being able to
figure it out in a year's time, or producing a macro that takes two seconds
to run (on a large document) versus 500 microseconds, I would write it the
first way, every time. MY time and effort is worth a hell of a lot more to
me than the computer's :)

So there: for anyone who really wanted to know, that's how it works! The
macro Daiyo offered here is one of the more intricate ones you will see in
your travels: she has done a lot with a few lines of VBA!

Hope you enjoyed it.


Update:

Bookmarks just randomly showed up in my Word 2004 doc, when I copied text
from it, and pasted the text into Safari, then went back to my Word doc,
suddenly I had bookmarks. Note that I had not pasted anything into Word.
This is the first time I have noticed this behavior since turning on show
bookmarks several days ago, but I have not been using Word very intensively.

Here is a macro to delete bookmarks (maybe. I edited it from a more complex
one that I didn't really understand, so I don't totally understand this one
either, but it just worked in my single test). Tony, you might want to test
it on a copy.
Go to Tools | Macros, Macros.... In the Dialog box, type DeleteBookmarks,
then click Create, which flips you into Visual Basic. Paste the above text
in where the cursor is (hope for no error messages). Word | Close and Return
to Word. In a copy of a doc with a lot of bookmarks, go to Tools | Macro,
Macros, select DeleteBookmarks, and click Run. See if the bookmarks go
away. Mess with the doc to see if it seems to crash less with no bookmarks.

Let us know whether that has any effect.

DM

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 
D

Daiya Mitchell

Thanks very much, John, I'm going to have to print that out and study it
more carefully, but it's very appreciated.

I actually took the macro that Mike had written a while back to delete
bookmarks for the guy who was doing genealogy and couldn't create a TOC
because he was way over the 16K limit for bookmarks (Tom somebody, the macro
was never on the list, but he sent it to me privately), and deleted the
parts I *knew* I didn't need. I coulda/shoulda done more random deletion
and then tested it, but I didn't think of that until after posting.

That said, do you have any deep knowledge about why bookmarks would randomly
appear in documents? I like the theory that excess bookmarks are causing
Tony's Word to quit 1 in every 4 copy/cut/paste operations (possibly because
it's the only theory we've got).

Daiya
 
J

John McGhie

Hi Daiya:

If I remember correctly, it's part of the function of OLE (Object Linking
and Embedding). The Mac doesn't support that technology directly, so they
fake it.

When you select an area and drag it, Word surrounds the area with bookmarks
so it can remember what you selected. When you paste or drop, it links the
two areas.

Clunky, but it works...

Cheers


Thanks very much, John, I'm going to have to print that out and study it
more carefully, but it's very appreciated.

I actually took the macro that Mike had written a while back to delete
bookmarks for the guy who was doing genealogy and couldn't create a TOC
because he was way over the 16K limit for bookmarks (Tom somebody, the macro
was never on the list, but he sent it to me privately), and deleted the
parts I *knew* I didn't need. I coulda/shoulda done more random deletion
and then tested it, but I didn't think of that until after posting.

That said, do you have any deep knowledge about why bookmarks would randomly
appear in documents? I like the theory that excess bookmarks are causing
Tony's Word to quit 1 in every 4 copy/cut/paste operations (possibly because
it's the only theory we've got).

Daiya

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top