PDA

View Full Version : VBA crashing on 'Documents.Add'



RoyLasris
01-17-2018, 04:25 AM
When my code executes and encounters Documents.Add, Word shuts down. I cannot even capture the error.

Why would this happen? What should I be looking for? Not happening on all of my computers. 5 process this line normally. 2 crash repeatedly.

It happens regardless of whether I assign a template (Documents.add template:="c:\path\template.doc" or just Documents.add)

Driving me crazy. :banghead:

Roy

gmayor
01-17-2018, 05:23 AM
Reboot the PC and try again. Word has a bad habit of storing up minor VBA errors and eventually it will crash with nonsensical errors. Rebooting will clear it.
This is a particular problem on laptops which tend not to be rebooted frequently.

RoyLasris
01-17-2018, 08:47 AM
Thanks, Graham, but did that first, and often.

Plus I can consistently repeat this on two (so far) different machines at any time. (Actually, on one machine, Word crashes and the restarts, as described in the initial post. On the other machine, Word actually adds the new document but then totally jumps out of the module. It won't process any lines that follow. The steps beneath the 'Documents.Add' line are not processed; neither are any subsequent lines in the 'parent' module that calls the 'Documents.Add' containing module processed. I cannot capture anything with an error handler (for the reasons stated in the previous sentence).

It is insane, but very real and readily duplicable. Even a module as simple as the single line 'Documents.Add' will behave consistently as described, but (so far) only on these 2 machines. (Does Microsoft follow these boards?)

Roy

gmayor
01-18-2018, 02:45 AM
This forum is independent from Microsoft.

This is not a normal error. A macro that creates a new document e.g.


Sub Macro1
Documents.Add
End Sub

Should not cause a crash in any Word version. The fact that it does suggests a local issue.
Repair Office and then before starting Word again see http://www.gmayor.com/what_to_do_when_word_crashes.htm (http://www.gmayor.com/what_to_do_when_word_crashes.htm)
Rename the normal template to (say) Normal.old and then try again.

RoyLasris
01-18-2018, 04:23 AM
We had done that as well (both Repair -- and both kinds of repairs -- and recreating normal.dotm). Same issue. Is it helpful to know that Ctrl-N works (i.e., it doesn't crash Word)? Of course, it's not a macro, but it performs the same function as Documents.Add.

I had not tried the long list of other clean-ups proposed in your link. I will try those today. Thanks.

Paul_Hossler
01-18-2018, 08:08 AM
I have a DOT with Rob Bovey's Code Cleaner for Word (circa 2001), but it's based on his original Excel Code Cleaner, which has been converted to a COM add in.

The Word code clean doesn't seem to be maintained after 16 years

http://www.appspro.com/Utilities/CodeCleaner.htm


During the process of creating VBA programs a lot of junk code builds up in your files. If you don't clean your files periodically you will begin to experience strange problems caused by this extra baggage. Cleaning a project involves exporting the contents of all its VBComponents to text files, deleting the components and then importing the components back from the text files.

However, the issue and the fix are still the same, and you might give it a try

It's fixed a 'Why did it do that????' macro issue in Excel many times, and occasionally in Word

It's a mite tedious to do the Exports / Imports manually, but you could try to export each module (as pure text), delete the modules (standard, userform, class, document) from the document, and then import the text files.

RoyLasris
01-18-2018, 11:43 AM
Just got an email from a 'distant user' who reports: "I've had a recentproblem with my computer which is causing my computer to crash when I use your program. About aweek ago, the computer shut down unexpectedly while I was in the middle ofworking (I suspect it was one of those updates that get installed withoutnotice). After I rebooted, I noticed problems with Word, as my Normaltemplate (which I had modified to include additional styles, plus a few macros)had disappeared and had been replaced by the plain one that comes withWord. When ever I use a tool that creates or adds a new document, Word crashes . . . ."

The problem I described in my first letter is now acting in the same fashion on 3 of 9 computers, and seems to be occurring in direct response to an upgrade of Word (I checked that out with the other two users). Is this something I should report to Microsoft? What's the best board for that?

RoyLasris
01-19-2018, 06:05 AM
On more helpful(?) update. I programmed a SendKeys "^n" in place of Documents.Add and it works fine. There is a 'speed' issue and I found I needed to add a 'Sleep' command, but at least I have a workaround. But why would one work and not the other?