Google Answers Logo
View Question
 
Q: backing up word files using VBA ( No Answer,   2 Comments )
Question  
Subject: backing up word files using VBA
Category: Computers > Programming
Asked by: shantanuo-ga
List Price: $2.00
Posted: 20 Jun 2002 06:34 PDT
Expires: 23 Jun 2002 04:00 PDT
Question ID: 29713
Hi

Here is the simple macro I have recorded in Word.
It creats a backup.doc file after inserting the contents of all 
the .doc files saved in My Documents.

I can run it if I know the exact file name. But if I don't know the 
names of all the files, how can get the first word file, insert the 
contents, then grab the second one for backup.


Sub Macro3()
'
' Macro3 Macro
' Macro recorded 04/06/02 by shantanu

Documents.Add DocumentType:=wdNewBlankDocument
ChangeFileOpenDirectory "C:\My Documents\"
Selection.InsertFile FileName:="AGREEMENT OF LEAVE AND 
LICENCE.doc"

Selection.InsertFile FileName:="elite.doc"

ActiveDocument.SaveAs FileName:="backup.doc"

End Sub

Thanks
Shantanu Oak

Request for Question Clarification by lazerfx-ga on 20 Jun 2002 07:09 PDT
Are you trying to create a macro/script or similar that will take the
contents of every document in My Documents, insert them into one
single document, and then save as a single backup file?

Clarification of Question by shantanuo-ga on 22 Jun 2002 19:21 PDT
Hi,
I found the answer. I am posting it for your review.

Highlight the following code and copy it. 
Start Word 
Tools - Macro - Visual Basic Editor 
Paste the code you have just copied. 
Save (ctrl + s or click on the save button on the toolbar) 
Close the Visual Basic window. 
Close Word. 
Now whenever you want to backup your files, choose the "Backup" macro
from Tools - Macro - Macros...


Sub Backup()

Documents.Add DocumentType:=wdNewBlankDocument

Set fs = Application.FileSearch
With fs
    .LookIn = "C:\My Documents"
    .SearchSubFolders = True
    .FileName = "*.doc"
    If .Execute() > 0 Then
        MsgBox "There were " & .FoundFiles.Count & _
         " file(s) found."
        For i = 1 To .FoundFiles.Count
              Selection.InsertFile FileName:=.FoundFiles(i)
        Next i
    Else
        MsgBox "There were no files found."
    End If
End With

ActiveDocument.SaveAs FileName:="c:\backup.doc"

End Sub

Note:
If the path of your "My Documents" folder is not "c:\My Documents"
(atten: NT Users), you will have to change the code.

The link to the above tutorial is available at
http://shantanuoak.com/sites/backup.htm
Answer  
There is no answer at this time.

Comments  
Subject: Re: backing up word files using VBA
From: f1jeeves-ga on 20 Jun 2002 07:11 PDT
 
Try this:

Sub Macro3()
'
' Macro3 Macro
' Macro recorded 04/06/02 by shantanu
 
 
Dim MyFile, MyDir
MyDir = "C:\My Documents\"
 
Documents.Add DocumentType:=wdNewBlankDocument
ChangeFileOpenDirectory MyDir

MyFile = Dir(MyDir & "*.doc")

While MyFile <> ""
    Selection.InsertFile FileName:=MyFile
    'Selection.InsertFile FileName:="doc4.doc"
    
    MyFile = Dir
Wend
ActiveDocument.SaveAs FileName:="backup.doc"


End Sub
Subject: backing up word files using VBA
From: shantanuo-ga on 20 Jun 2002 07:43 PDT
 
lazerfx - 
yes you are right.

f1jeeves - 
I want to include the sub-directories in my documents as well.
AND
Here is a request, though it was not a part of the question.
Can I add the name of the document at the beginning of each text block?

Thanks

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy