Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

backing up word files using VBA

Status
Not open for further replies.

shantanuo

Instructor
Jun 20, 2002
9
IN

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. It is working fine.
BUT
I want to include the sub-directories in my documents as well.

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 <> &quot;&quot;
Selection.InsertFile FileName:=MyFile
'Selection.InsertFile FileName:=&quot;doc4.doc&quot;

MyFile = Dir
Wend
ActiveDocument.SaveAs FileName:=&quot;backup.doc&quot;


End Sub

Thanks
Shantanu Oak
shantanuoak.com
 
Ratman's lock of the day ! (I used in Excel)

Sub LoopThruAllDocFilesInSubsToo()
Set fs = Application.FileSearch
With fs
.LookIn = &quot;C:\My Documents&quot;
.FileName = &quot;*.doc&quot;
.SearchSubFolders = True
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
'DO YOUR BAD SELF STUFF HERE
Next i
End If
End With
End Sub
 
Hi,
I have already managed this. The code is as follows.
Now I want to add the name of each file at the beginning of each text block and format it as Heading 1.

Code:
Sub Backup()
Documents.Add DocumentType:=wdNewBlankDocument
Set fs = Application.FileSearch
With fs
    .LookIn = &quot;C:\My Documents&quot;
    .SearchSubFolders = True
    .FileName = &quot;*.doc&quot;
    If .Execute() > 0 Then
        MsgBox &quot;There were &quot; & .FoundFiles.Count & _
         &quot; file(s) found.&quot;
        For i = 1 To .FoundFiles.Count
              Selection.InsertFile FileName:=.FoundFiles(i)
        Next i
    Else
        MsgBox &quot;There were no files found.&quot;
    End If
End With
ActiveDocument.SaveAs FileName:=&quot;c:\backup.doc&quot;
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top