Log in

View Full Version : Macro to save a copy



shekhu
03-19-2007, 11:26 PM
This macro is to save a copy of the active document to the local drive. The active document is opened from the network. The copy is saved in D: WordBackup folder. I want that the file be saved in that WordBackup folder by creating a new folder with the current date (i.e. 032007 or any other format), and any further files I save the same day are saved in that current folder only. The next day I save any file are saved in the next day?s date folder (current date for that day). Thanx

Currently I am using this macro which doent create a date folder, only the document is saved.

Sub SaveBackup()
'Attribute SavToTwoLocations.VB_Description = "Macro created 2/4/05 by Mr."
'Attribute SavToTwoLocations.VB_ProcData.VB_Invoke_Func = "Normal.NewMacros.SaveToTwoLocations"

' SaveBackup Macro
' Macro created 2/4/2005 by Mr.

On Error GoTo Mainstop
Dim strFileA, strFileB, strFileC

ActiveDocument.Save
strFileA = ActiveDocument.Name

strFileB = "D:\WordBackup\" & strFileA
strFileC = ActiveDocument.FullName

If Dir(strFileB) = "" Then
ActiveDocument.SaveAs FileName:=strFileB
Else
With Dialogs(wdDialogFileSaveAs)
.Name = strFileB
.Show
End With
End If

ActiveDocument.SaveAs FileName:=strFileC

Mainstop:
If Err.Number <> 0 Then
MsgBox "This macro Not Correct" & vbCr & "" & vbCr & ""
End If

End Sub

fumei
03-20-2007, 12:24 AM
Use FileSystemObject to create your new folder. You must set a Reference to Microsoft Scripting Runtime in order to use FSO.

If you are going to be doing any serious work with files, you need to learn how to use FSO.

Once you do learn it, what you want to do is easy. It took me less than five minutes to write the code up. FileSystemObject has a CreateFolder method.strNewPath = "D:\WordBackup\" & Format(Now, "ddmmyy")
fso.CreateFolder strNewPathcreated a folder - D:\WordBackup\200307.

As it is a new folder, there is obviously nothing in it. Your files can be saved asActiveDocument.SaveAs Filename:= strNewPath & _
ActiveDocument.Name

Why are you showing the SaveAs dialog? Why not just save it?

shekhu
03-20-2007, 12:54 AM
Why are you showing the SaveAs dialog? Why not just save it?

I want to save a copy and at the same time I want that the active document that is open from the network should not be closed.

Secondly, I dont have any knowledge of VBA. I just manage to toggle things here and there so that my purpose is acheived. This is the only reason I am here in this forum, and you people have done a great job.

Finally, please give me full code. For me, it is very difficult to sumup the code.

mdmackillop
03-20-2007, 01:27 AM
Hi Sheku,
If you are going to have many "date" folders, it may be better to format the name as "yymmdd", which allows them to be sorted in date order for ease of finding.

shekhu
03-20-2007, 11:26 PM
fumei, I really dont understand what you mean, as I am a layman as far as VBA, pl. post in a full code so that I may try using that.

Thanx mdmackillop for your suggestion, I will try that when I receive the full code.

fumei
03-21-2007, 05:06 AM
Sure I will help you, but first...look up FileSystemObject. Use Google.

One link: http://www.tutorial-web.com/asp/fso/

Try writing some code. If you have problems, post a question.

You did not answer the question. WHY are you displaying the File SaveAs dialog? You can just save the file directly. WHY give the user a chance to change it? Just save the file as whatever YOU want.


I want to save a copy and at the same time I want that the active document that is open from the network should not be closed.This is NOT possible. Using FileSaveAs takes the current file and saves it as something else. The CURRENT file.

If the current file is: U:\Blah\ThisNThat\Some.doc

and you do a File Save As: D:\WordBackup\Some.doc

the original doc is NOT there anymore. You have saved it As the new name. The original document is not "closed", but once saved as, it is not Open either.

The only way you could keep the original open, AND make a copy that is saved as, is to......make a copy. Using SaveAs does NOT make a copy. It saves the existing document with a different name.

shekhu
03-21-2007, 05:44 AM
The macro I have given in the beginning saves a copy in the background (as the given path at D:Backup) and the active document is still open for any further changes. If I make any further changes then I again run the macro to save a copy and then a option comes to either overwrite or save the copy with a new name. Also, if I edit a new document with a same name then I am reminded to save it with a new name, as there is already a doc with the same name.

fumei
03-21-2007, 08:15 PM
The macro I have given in the beginning saves a copy in the background (as the given path at D:Backup) and the active document is still open for any further changes. This is not accurate. I will repeat.

You open U:\Blah\ThisNThat\Some.doc

You File Save As: D:\WordBackup\Some.doc

Yes, you still have an active document, but it is NOT, repeat NOT, U:\Blah\ThisNThat\Some.doc

There is no such thing as saving in the background. Well there is background saving of the active document, but it is saving OF the active document. SaveAs saves the current document AS the different filename, and THAT is now the current active document. The original is NOT open anymore.

What you are doing is:

Open U:\Blah\ThisNThat\Some.doc

ActiveDocument = U:\Blah\ThisNThat\Some.doc

SaveAs D:\WordBackup\Some.doc

ActiveDocument = D:\WordBackup\Some.doc

SaveAs U:\Blah\ThisNThat\Some.doc

ActiveDocument = U:\Blah\ThisNThat\Some.doc

You are doing two SaveAs in order to have the current activedocument named the same as the original.

mdmackillop
03-22-2007, 01:21 AM
There is in Excel a function called SaveCopyAs which does what you require. In Word, you have to create a workaround, as you have seen.

shekhu
03-22-2007, 03:00 AM
Fumei okay whatever, you are right, but that is the way I want. Further, the files I save with this macro get saved in this folder(WordBackup) and the next day they start accumulating there, and so on. I have to manually create folders by date and put the respective date files in that folder. As my question, I want that folders for respective date be created there so I dont have to create them manually.

As far as your concern, please suggest me that I get what I want either by coding a new macro or ammending the macro I am using. I welcome your suggestions. Thanx

fumei
03-22-2007, 08:06 AM
Have you looked up FileSystemObject yet? It is what you will need to make your folders.

When you have tried using it, and can post your code trying to do it, if you have problems with it I can help.

lucas
03-22-2007, 08:26 AM
Gerry has actually given you the code you need in post #2

fumei
03-23-2007, 06:37 AM
Technically, I showed part of it. I gave the nitty gritty bit, but not the background bit.

Sorry shekhu, I am not trying to be difficult, but this is not a Help Desk, and I am not getting paid to give solutions. You say you do not know VBA. OK, but you are trying to use/code VBA. So....try.

You need to use FileSystemObject to create your folders. FSO is very powerful, and it is an important part of VBA when doing work on, well, the File System.

I will repeat some directions.

You need to use FSO.
To use FSO you need to have a Reference for Microsoft Scripting Runtime.
When you have that Reference, you make a FileSystemObject. You then use that object to make your folder. This is the fso infso.CreateFolder strNewPath with the variable strNewPath being D:\Wordbackup\today's date.

True, I did not post the code on how to make that object. You can find that out.

I would also reiterate Malcolm's suggestion on the name of the folder. If you are making many folders, it would be better to make them yymmdd. It will make finding one much easier.

Again, look up FSO. Try to write code for what you want to do. I will help with specific problems if you have any.

shekhu
03-27-2007, 06:14 AM
fumei, I am trying my best to learn that. I will post my findings as I find something constructive. Nothing to this time.