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
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