Save as dialog box upon opening

J

Jerry Anderson

I want a document to automatically save a copy of itself to the user's
desktop immediately upon opening, and before allowing any changes.
I've done VBA in Access, but I have had no exposure to the Word programming
interface.Does anyone have an idea how to do this?
 
S

StevenM

Jerry,

I might be wrong, but I suspect that you might run into problems attempting
to copy an already open file.

What about copying the file each time you close the document? That would
almost be the same thing, assuming that no one deletes the copy before the
original file is opened again. At that point, a copy of the file would
already exist.

The reason for this, is that if one does a SaveAs, the name of the file
saved changes. I thought about re-opening the original file and closing the
saveAs-ed file, but then re-opening the file might trigger the macro to save
itself again.

Perhaps, someone will present a better answer. Anyway, this is what I came
up with:

'
' Save a copy of the document to the desktop on close
'
Sub AutoClose()
Dim sFilePath As String
Dim sFileName As String
Dim sNewFileName As String
Dim actDoc As Document
'
' Desktop path
'
sFilePath = "C:\Documents and Settings\Steven\Desktop\"
'
Set actDoc = ActiveDocument
sFileName = actDoc.Name
sNewFileName = UniqueFileName(sFilePath, sFileName)
actDoc.SaveAs FileName:=sNewFileName
End Sub

'
' UniqueFileName
'
' This macro creates a unique name for a document based on:
' (1) the name of the active document from which this macro was ran; and
' (2) and a two digit number starting with "01"
'
' And it also tests to see if such a document exists, if so, it increments
' the number and tries again.
'
Function UniqueFileName(ByVal sFilePath As String, _
ByVal sFileName As String) As String
Dim sNum As String
Dim sNewFileName As String
Dim nNumber As Long
Dim nPos As Long

nPos = InStrRev(sFileName, ".")
If nPos > 0 Then
sFileName = Left(sFileName, nPos - 1)
End If
Do
nNumber = nNumber + 1
sNum = CStr(nNumber)
If Len(sNum) = 1 Then
sNum = "0" & sNum
End If
sNewFileName = sFilePath & Application.PathSeparator _
& sFileName & "_" & sNum & ".Doc"
Loop While (Dir(sNewFileName) <> "" And nNumber < 1000)
UniqueFileName = sNewFileName
End Function

Steven Craig Miller
 
G

Gordon Bentley-Mix

Jerry,

Can you provide a bit more information on your intentions? Is your goal to
ensure that the user doesn't make changes to the "Master" document and only
edits the copy saved on the desktop? If so, then I'd recommend creating a
Word template instead of trying to do something with forcing a save of a copy
of the document. A template will provide the sort of security you're looking
for and will avoid the problems that might potentially arise from the use of
a macro - Steven's example of triggering the macro again when the document on
the Desktop is opened being one such problem, and duplicate filenames being
another.

While most of these problems can probably be addressed with the proper code,
IMHO it's more work than it's worth (just look at how much code Steven wrote
to address the duplicate filename issue!) - especially when Word provides
native functionality through the use of templates to achieve the same goal.

If you truly want to use a document over a template then Steven has provided
you with an excellent starting point. However, you may want to investigate
the use of the APPDATA environmental variable in relation to determining the
path to the Desktop. Otherwise, I'd recommend using a template. You can still
force the newly created document to be saved to the Desktop if that's your
requirement, but I wouldn't; I prefer to leave the process of saving a
document to the user.

At the very most, I would maybe write code to display the 'Save As' dialog
box (with a recommended filename) to prompt the user to save the document.
This can be accomplished with something as simple as:

Sub AutoNew()
With Dialogs(wdDialogFileSaveAs)
.Name = "My New Document"
.Show
End With
End Sub

Of course you would replace "My New Document" with something more
descriptive, and you could always combine this with Steven's code for setting
the path to default to the Desktop.
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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