SaveAs interactively vs. programmatically

H

Hans Troost

Hi all,

When interactively having an open document A.doc using File SaveAs and
giving name B, Word saves the document as B.doc and keeps it open as
B.doc while A is closed.

In my specific situation however, using this function programmatically
it keeps A open in stead of the saved B.

My situation:

1. using an add-in, started with the command-line /L<add-in.dot>
switch
2. Intercepting the Save event by a class module with a
<application>_DocumentBeforeSave procedure.

and now the "special"

3. for a lot of reasons the document is first created as a temporary
document with a name <unique-name>.TMP and after when the user really
saves it the <application>_DocumentBeforeSave is fired:

back to "normal"

4. Class module code

Option Explicit

Public WithEvents WApp As Word.Application

Private Sub WApp_DocumentBeforeSave(ByVal Doc As Document,
SaveAsUI As Boolean, Cancel As Boolean)

If Mid(Doc.Name, Len(Doc.Name) - 3, 4) = ".TMP" Then
With Dialogs(wdDialogFileSaveAs)
.Name = Mid(Doc.FullName, 1, Len(Doc.FullName) - 4) &
".doc"
.Execute
End With
End If
MsgBox "DocumentBeforeSave"

End Sub
5. I am sure that this event is fired: the messagebox pops up and the
<unique-name>.doc appears in the folder.

6. My first attempt was even simpler (I still like it better), but
gave the same result:

....
....
If Mid(Doc.Name, Len(Doc.Name) - 3, 4) = ".TMP" Then
Doc.SaveAs Mid(Doc.FullName, 1, Len(Doc.FullName) - 4) &
".doc"
End If
....

Result:
=================
7: Which one of the 2 options above I try: still the <unique-name>.TMP
is the (only) open one and not <unique-name>.doc

Do I do something wrong or is it not allowed to use the .TMP
extension?

Of course, I can program around it with things like
<unique-name>-TEMP.doc, but I always try to take the most
straight-forward way as I see it, which was SaveAs in this case.

Above all: I don't like things happening that I don't understand or
that behave different then they - in my opinion - should.

Please comment on this.

Best regards,

Hans Troost
 
M

martinique

DocumentBeforeSave is not the same as Save or SaveAs -- this event is fired
BEFORE the save function is called; it is not intended as the location for
doing the actual save.

Try putting your code in an ordinary module under the name "FileSaveAs".
This is then called in place of the built-in Save function.

BTW, instead of Mid(Doc.Name, Len(Doc.Name) - 3, 4) you can use
Right(Doc.Name, 4)
 

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