PDA

View Full Version : VBA : Save a copy of the active document



Fab117
06-08-2007, 12:00 AM
Hi,
When I write a word document, I have to put it in a common share
For security I would like to always do a copy of it in my folder.
I did it without trouble for my Excel workooks with : ThisWorkbook.SaveCopyAs Filename:=...
I tried to do the same for the word documents, but SaveCopyAs as not the same meaning for word than for Excel :

This code doesn't work (error message on SaveCopyAs) :

Private Sub Document_Close()
Dossier = "H:\DATA\GF\Tmp\Dev macro enregistrement copie\2\"
Count = Len(ActiveDocument.Name)
Nom = Left(ActiveDocument.Name, Count - 4)
strDate = Format(Date, "dd-mm-yy") & " - " & Format(Time, "h-mm-ss")
ThisDocument.SaveCopyAs FileName:=Dossier & Nom & " - " & strDate & ".doc"
End Sub



Could someone help me to solve this problem ?
To resume, I want that each time thet I close my document, word save a Copy of it in the folder "H:\DATA\GF\Tmp\Dev macro enregistrement copie\2\"


Thanks.

Fab

lucas
06-10-2007, 07:48 AM
This seems to work....let us know...
Option Explicit
Private Sub Document_Close()
'Dim Dossier As String
Dim Count As Long
Dim Nom As String
Dim strDate As String
'Dossier = "F:\Temp\Test\"
Count = Len(ActiveDocument.Name)
ActiveDocument.Save
Nom = Left(ActiveDocument.Name, Count - 4)
strDate = Format(Date, "dd-mm-yy") & " - " & Format(Time, "h-mm-ss")
ThisDocument.SaveAs FileName:="F:\Temp\Test\" & Nom & " - " & strDate & ".doc"
End Sub

I changed your path to test it so you will have to correct that.

Fab117
06-10-2007, 11:53 PM
Thank you for your help.
The trouble with your proposition is that if I forget to save the official document before closing it, word will just save the changes for the copy.

Finally, I used :

Private Sub Document_Close()
Dim Confirmation As Long
' Quitte la macro si je ne suis pas l'utilisateur
If Environ("UserName" ) <> "Toto" Then Exit Sub
nom = ActiveDocument.Name
' Demande s'il faut enregistrer les modifications
Confirmation = MsgBox("Voulez vous enregistrer le fichier " & nom & " ? ", vbYesNo)
If Confirmation = vbYes Then
ThisDocument.Save
' Fait une copie
Dossier = "H:\DATA\GF\Gestion de projets\Circuit d'eau\Copie des documents qualite"
Count = Len(ActiveDocument.Name)
nom = Left(ActiveDocument.Name, Count - 4)
strDate = Format(Date, "dd-mm-yy" ) & " - " & Format(Time, "h-mm-ss" )
ThisDocument.SaveAs FileName:=Dossier & nom & " - " & strDate & ".doc"
End If

End Sub

Bye,

Fab