Macro in Normal.dot prevents Word from opening or debugging!

F

Fern

Hi,

BACKGROUND:
Last night I wrote a code that was meant to save copies of certain open
documents to an archive folder, both when I manually tell Word to archive and
then also whenever those 'marked' documents are closed. It was based on
replies to my question about Word's File>Versions option (which recently ate
all of my file's saved versions!) that I'd posted in another discussion group
('General Questions': http://snipurl.com/Version). In particular, I relied a
lot on the recommended coding at
http://www.gmayor.com/save_numbered_versions.htm. And in the end, I got the
code to copy a new time/date-stamped image of the open file to an archive
folder using Sub AutoClose() and also let me save the current version to the
archive, even if I didn't want to close the file quite yet. Since I'd also
wanted to have the
archiving available to ANY file (if I chose to apply it), I put the coding
into my Normal.dot template & added some lines to check whether a given file
had had archiving enabled - if not, the file SHOULD have opened & closed as
usual - but if so, then it should have let me save to the archive at any
point while the file was open.

PROBLEM:
Well, the code worked GREAT when I was testing it last night (=opening &
closing documents, but never closing Word itself) but today, when I tried to
open new or existing documents that are based on the Normal.dot template,
Word immediately encountered a problem, sent up a nondescript error message,
and shut itself down. So not only can I not debug the code at this point, but
I can't even open the .dot file independently of Word in order to remove the
archiving code without deleting the whole template.

Luckily I happened to have written part of the Normal code into another
template and, when I open documents based on that template, they work just
fine - as did the archiving code in the 2nd template's AutoClose() macro. So
I know the archiving itself works a-ok (thank goodness). And so clearly the
trouble is coming from one of the additional codes that I added to the Normal
template: (1) code that lets users archive their file immediately & without
closing it, and that will 'mark' the open file as one that needs to be
archived if it's never been archived before (by adding an entry {called
'RegKey' - see below} to the registry), OR (2) code that tells the user
whether the open document has archiving enabled (based whether there's a
RegKey entry for that file in the Registry).

MY BEST GUESS:
So, my big worry is that I may have stupidly put one of the codes into an
AutoOpen() subroutine for some reason and that the error handling is just
redirecting Word to exit the macro as soon as it hits a problem - meaning
that it can't open anything and so has to shut itself down.

HELP NEEDED:
If that's the case, do I have any way to get into that .dot template file to
debug it?? Is there some other program or method to access its macros without
running them (especially if this is caused by a buggy AutoOpen)?? Or is my
only option to delete the flawed Normal template and let Word build a new
one, without all my settings & styles or archiving code??
And if I have to start from scratch again, can anyone tell me how I might
adapt the code below (which is from the WORKING 2nd template, NOT Normal.dot)
so I can at least call it from one of the toolbars/buttons and can 'mark'
files for archiving that way??

I'd be SO grateful for any help you can give me. I just don't know how to
get myself out of this catch-22.
Thank you!!


THE WORKING TEMPLATE CODE:
Sub AutoClose()
'Saves a numbered copy of the current file to an archive location
'
Dim WSHShell, RegKey, rkeyWord, Result
Set WSHShell = CreateObject("WScript.Shell")
Dim iCount As Integer, StrDate As String, StrPath As String, StrFile As
String, StrName As String
Dim StrVersionFile As String, intPos As Integer, DirExists As Boolean,
StrFileNPath As String
Dim NewStrPath As String, StrVersionFileNPath As String, fso,
MyRecentFile As RecentFile
Set fso = CreateObject("Scripting.FileSystemObject")

ActiveDocument.Save 'Ensures the saved doc is the most up to date
version

StrFile = ActiveDocument.Name 'Get document name
StrName = Left(StrFile, Len(StrFile) - 4) 'remove the file extention
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
On Error Resume Next 'No entry in registry will flag an error
rkeyWord = WSHShell.RegRead(RegKey & StrName)
If Not rkeyWord = "" Then 'If the registry entry DOES exist (i.e. the
file has already been marked for archiving)
StrDate = Format((Date), "yyyy-mm-dd")
intPos = InStrRev(StrFile, ".doc") 'Look at that name for an
extension
If intPos = 0 Then 'If document is not saved
On Error GoTo CancelledByUser
'ActiveDocument.Save 'Save it
End If

StrPath = ActiveDocument.Path 'Get path
StrFileNPath = StrPath & "\" & StrFile 'Get document's full name
& path

intPos = InStrRev(StrFile, " - Version") 'Mark the version number
If intPos = 0 Then 'No version number
intPos = InStrRev(StrFile, ".doc") 'Mark the extension instead
End If
StrFile = Left(StrFile, intPos - 1) 'Strip the extension or
version number

Start: 'Get Registry Data
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
On Error Resume Next 'No entry in registry will flag an error
rkeyWord = WSHShell.RegRead(RegKey & StrFile)
If rkeyWord = "" Then 'Registry entry does not exist
WSHShell.regwrite RegKey & StrFile, 0 'So create it
GoTo Start:
End If
iCount = Val(rkeyWord) + 1 'Increment number
WSHShell.regwrite RegKey & StrFile, iCount 'And write it to the
registry

NewStrPath = "C:\Documents and Settings\{USER}\Word Archive\Archived
'" & StrFile & "'"
'Checks if an archive folder exists &, if not, makes one
If Not fso.FolderExists(NewStrPath) Then
fso.CreateFolder (NewStrPath)
End If
'Define the new version's filename
StrVersionFile = StrFile & " (" & StrDate & "@" & Format((Time),
"Hh.Nn") & ").doc"
StrVersionFileNPath = NewStrPath & "\" & StrVersionFile
'copy the current/active document to the archive location & file
name (without adding it to the 'Recent Files' list)
WordBasic.CopyFileA FileName:=StrFileNPath,
Directory:=StrVersionFileNPath
End If
GoTo StopCode

CancelledByUser: 'Trap the pressing of the Cancel button
MsgBox "There was a problem running the code & this file version
wasn't saved or closed.", , "Archiving Cancelled"
GoTo StopCode
StopCode:
End Sub
 
D

DA

If you think its your code, disable the Macros in your security settings. My
guess is though that you've become the victim of the dreaded Normal.dot
corruption (shiver!). Sorry, its really not that bad. Rename your current
normal.dot to something else and restart Word. It will create a new
normal.dot for you. After you've done that, try and open the renamed copy of
the template - does Word still crash?

Although it has its advantages, I'm not a big fan of having macros in
Normal.dot precisely for this reason.

See how you go with the above. Most times you should be able to extract the
code you have in your corrupted dot file by opening it with the recovery
option.

Dennis
 
J

Jay Freedman

Hold down the Shift key while starting Word to prevent any AutoExec,
AutoOpen or AutoNew macros from running. Then you should be able to
open the VBA editor and try to figure out what's wrong.

I haven't tried to analyze the code you posted (and an AutoClose macro
isn't going to have any effect on what happens when you try to open a
document anyway).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
F

Fern

Neither method stopped the crashing.... Sigh.

Setting the security level to Medium & the choosing "Disable Macro" still
results in a crashed program (even if I'm pressing Shift) - which makes me
think it's not a problem with the coding iteself. And yet renaming the file &
running it under its new name (Normal->NotNormal.dot) to avoid the dreaded
corruption still ends up crashing the whole shebang anyway. What are my other
options??

So if it's not the code and it's not the fact that it's a Normal.dot file,
then what's making it act this way and how the heck do I fix it??

Well, thanks for trying DA. I appreciate the attempt :)
 
F

Fern

That didn't work either, Jay. It looks like this ISN'T a problem with the
code itself (or at least not with anything automatic). Could Word be finding
an error elsewhere in the code (i.e. not in AutoOpen or AutoClose) and then
following the handling directions by exiting the macro &/or crashing - all
without actually/officially running the code itself? Could the corruption be
elsewhere in the .dot file, somewhere that isn't controlled by a macro and
isn't affected by whether the file is named Normal or not??

What other likely explanations are there? I'm not familiar enough with this
type of automatic coding to really know where the trouble is likely to be
(which is probably why I bumbled into the problem in the 1st place). Maybe
I've just got to rewrite all that coding from scratch...
.... And yes I agree that it doesn't make sense to debug the WORKING
archiving code since it's actually not called until the document closes and
since it clearly doesn't contain whatever it is that's causing so much havoc
in that Normal.dot file.

Thanks for trying though.
 
F

Fern

THANKS GRAHAM!! Great site!

I've been working through your steps for "What to do.." and am stuck at the
DOS commands. You list the following 5 locations (from your own PC)...

D:\Templates
D:\Word Startup
C:\Program Files\Microsoft Office\Office11\Startup
C:\Program Files\TechSmith\SnagIt 8
D:\My Documents
C:\Documents and Settings\Graham Mayor\Local Settings\Temp

....but then only go on to explain how to find 4 of them on OUR computers and
only seem to refer to 4 in the batch file coding. Plus you don't really
explain the inclusion of SnagIt - is that just where YOU happen to have your
default Word document folder or is that something _I_ need to have too? Or,
as I suspect, is the inclusion of "D:\My Documents" a reference to what most
people would use as their default folder? ...meaning that the 3rd & 4th items
on the DOS list are just two alternatives for the same DOS entry and that
most people would then have something like the following entry in their batch
file, instead of the SnagIt reference that you use for your PC:

REM Select default Word document folder

CD \Documents and Settings\{USER}\My Documents *** or *** CD \My Documents
DEL /A:h ~$*.dot

Does that sound right??
Thanks again for your VERY helpful website and link. I'll have to spend some
time poking around your site to see what other gems I can find :)
 
G

Graham Mayor

Basically the batch file is intended to simplify the removal of temporary
files left when Word crashes.
The temporary files are created in any document folder related to an open
document
The user template folder (tools > options > file locations > user templates)
The Word startup folder (tools > options > file locations > startup)
The Office startup folder (C:\Program Files\Microsoft
Office\Office11\Startup)
The Windows Temp folder (C:\Documents and Settings\<username>\Local
Settings\Temp)
and any folder related to an add-in in use (eg SnagIt).
If you are creating the batch file (which is more convenient than essential)
you need to identify each folder that contains the files that need to be
moved on your PC and insert the paths in the batch file.
If you are uncomfortable with DOS, as an alternative to creating the batch
you could simply search with Windows Explorer for
~*.do?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Shane

As far a batch files go, this is one I use to delete the annoying temp
files.

@echo off
cd /d "c:\"
if /i "%cd%"=="C:\" del /s /f /q ~*.do? 1>nul 2>&1

It changes to the root of c: drive then recursively deletes temp files
without echoing to the screen. Run via a Task Scheduler to keep you
c: drive clean.

HTHs
Cheers
Shane
 
F

Fern

Ohhhhhhhhhhhh, I didn't realise that the batch file was an ALTERNATIVE to not
in ADDITION to the Windows Explorer search. Ok, well since that's the case
then I've already removed the temp files in question. So no more DOS for me.
Phew!

Unfortuniately tho, none of the steps in either of the links you provided
succeeded in letting me open the original Normal template. The
‘winword.exe/a’ step doesn't let me open the template file. Neither does
renaming the file or pressing shift while it loads. And moving/renaming the 1
file in Word's startup doesn't do any good either. No change when I delete
the data key and I know for sure that it's not a printer problem (cuz I
already solved that for myself a couple weeks ago). And any way those
couldn't be an issue since I AM able to open other docs and the 2nd version
of the template/macro.

So what other options are there?? I've nearly figured out how to archive my
files without crashing word, but that still won't get me my old settings &
the remaining code back...
Any ideas??
Thx
 
G

Graham Mayor

Rename normal.dot to normal.old. Start Word then see if you can open
normal.old from the organizer (tools > macro > macros > organizer) and copy
the macros from old to new. If you cannot open it, it is corrupt.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Fern

Wow! That actually worked! I'm shocked!! THANKS GRAHAM!

I guess I'd misunderstood your website's explanation (about changing .dot to
..old) and just figured that was just a random example of how to rename the
template file so it wouldn't automatically kick in as Normal.dot when Word
opened.

Anyway... This is EXCELLENT. Not only do I have access to the macros again
but I also know that the problem is somewhere in the coding itself because
Word crashes when I try to open it after moving the 3 macros from Normal.old
into Normal.dot. Hmm, I wonder what the snag is. I just took a quick peek at
the codes and they SEEMED fine. But clearly something is still wrong.

So here's what's odd: (1) when I tried to open Word with all 3 macros in the
new Normal.dot it crashed, (2) when I tried to open it with only 1 macro at a
time in Normal.dot it crashed, BUT (3) when I copied those 1-3 macros to a
different template file (NotNormal.dot) & then tried to open Word by opening
a document formatted with NotNormal.dot, Word opened perfectly. So what does
that tell us?? I haven't a clue. I guess there's something in one or all of
the codes (& that wasn't there in the 2nd template that worked all along)
that really doesn't work well with the built in processes of Normal.dot. Hmm.

The same problem is happening when I try to open my current attempt at
writing an archiving code. It works great in any other template, but crashes
the program when I put it into Normal.
Could some good samaritan look at the code and tell me which part is
incompatible with Normal.dot?? The following is the AutoClose routine from my
most recent attempt at this archiving macro. It's supposed to check whether
the open doc is 'flagged' for archiving and, if it is, to copy it to an
archive folder. I've got a separate subroutine that can be called from the
toolbar and that's used to 'flag' the open doc & archive an instant
'snapshot' of the file without having to wait until the doc is closed. Ok, so
here's the code (& yes I know it's wordy but I like it to be intuitively
readable even months after it's written):
________________________________________________________--
Sub AutoClose()
'
Dim WSHShell, RegKey, rkeyWord, Result
Set WSHShell = CreateObject("WScript.Shell")
Dim TodaysDate As String, OldVersionPath As String, OldVersionFile As
String, OldVersionName As String
Dim NewVersionFile As String, intPos As Integer, DirExists As Boolean,
OldVersionFileNPath As String
Dim NewVersionPath As String, NewVersionFileNPath As String, fso,
MyRecentFile As RecentFile
Dim FirstTimeArchiving As Boolean
Set fso = CreateObject("Scripting.FileSystemObject")

On Error GoTo CancelledByUser
ActiveDocument.Save

FirstTimeArchiving = False
OldVersionFile = ActiveDocument.Name 'Get document name
OldVersionName = Left(OldVersionFile, Len(OldVersionFile) - 4)
'remove the file extention

Start:
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
On Error Resume Next 'No entry in registry will flag an error
rkeyWord = WSHShell.RegRead(RegKey & OldVersionName)
If rkeyWord = "" Then 'If the registry entry doesn't exist (i.e. the
file hasn't been flagged to be archived)
GoTo StopCode
Else 'if it HAS been set for archiving...
On Error GoTo CancelledByUser
OldVersionPath = ActiveDocument.Path 'Get path
OldVersionFileNPath = OldVersionPath & "\" & OldVersionFile 'Get
document's full name & path
NewVersionPath = "C:\Documents and Settings\{USER}\Word
Archive\Archived '" & OldVersionName & "'"
'Checks if an archive folder exists &, if not, makes one
If Not fso.FolderExists(NewVersionPath) Then
fso.CreateFolder (NewVersionPath)
End If
'Define the new version's filename
TodaysDate = Format((Date), "yyyy-mm-dd")
NewVersionFile = OldVersionName & " (" & TodaysDate & ").doc"
NewVersionFileNPath = NewVersionPath & "\" & NewVersionFile
'copy the current/active document to the archive location & file
name (without adding it to the 'Recent Files' list)
WordBasic.CopyFileA FileName:=OldVersionFileNPath,
Directory:=NewVersionFileNPath
If FirstTimeArchiving = True Then
MsgBox "The current version of this document has been archived.
It will now be archived whenever it's closed."
End If
End If
GoTo StopCode

CancelledByUser: 'Trap the pressing of the Cancel button
MsgBox "There was a problem running the code & this file version wasn't
archived.", , "Archiving Cancelled"
GoTo StopCode
StopCode:
End Sub
_________________________________________________-
So can you what might be messing with my code when it's put in Normal.dot?

Thanks for all of your help so far!! You guys have been great, especially
you Graham!!
 
G

Graham Mayor

I would be inclined to use a settings file to hold the data rather than the
registry (you'll find out how to do that from my web site - eg
http://www.gmayor.com/automatic_numbering_documents.htm), but I don't see
why an autoclose macro should have any effect on *starting* Word. The macro
doesn't run until you *close* a document. I would be more interested in
autoopen/autonew/autoexec macros and the content of any add-ins.

Don't forget to clear the temps after a crash and it might be worth
repairing Word.

A defective printer driver can be a cause of hard-to-identify crashes. Try
updating/reinstalling the driver.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Fern

LOOKS LIKE WE MIGHT HAVE A SOLUTION!!!

Settings file: check!
Printer driver: already dealt with - I'm on a network and so that problem
came up a couple weeks ago...
Temps cleared: check!
Add-ins: I removed both of my add-ins (RefWorks & MathType), saved AutoClose
to Normal.dot, closed the program, and tried to re-open it.... AND IT
WORKED!! YIPPEEE! I've got to test it a few more times & ways jsut to be
sure, but it looks like we've got a winner!!

THANK YOU SO MUCH GRAHAM! You've been a HUGE help. I'm really grateful!! :-D
 
F

Fern

Ooops, looks like I spoke too soon. Despite removing both add-ins from their
start-up folders, and only using one archiving macro at a time, Word still
crashes whenever it's opened. Don't know why it worked that one time...
And fyi, other than the nearly-identical AutoClose & manual archiving
macros, I haven't added to or changed the default Normal.dot template.

So I guess the search continues... Sigh.
 
G

Graham Mayor

Start Word in its safe mode by holding the CTRL key as you start it. Does it
work now? If it does then you still have some external problem - look again
at the printer driver on your PC.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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