Find File, Save As and Close

s0m1ght1

New Member
Joined
Jun 19, 2009
Messages
2
Hi All
I have a workbook with some code which searches the C: for certain files and opens them. I am then trying to save them to another directory and close them. The problem with the code below is it saves the file that is used to search for the files, not the ones I have searched for and opened. Can anyone please help me with this as it is driving me insane. I have tried to find a way to make the files searched for Active but had no luck. See Code Below. Cheers!!!


Sub macro1()
Dim objExcel As Excel.Application
Dim SourceFileName As String

With Application.FileSearch
.NewSearch
.LookIn = "C:\Documents and Settings\"
.SearchSubFolders = True
.Filename = "File A.xls"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
For j = 1 To .FoundFiles.Count
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open Filename:=.FoundFiles(j)
objExcel.Visible = True
Application.DisplayAlerts = False
SourceFileName = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:="V:\File .xls"
Application.DisplayAlerts = True
Next j
Else
MsgBox "file Not found, please check number and try again"
End If
End With

With Application.FileSearch
.NewSearch
.LookIn = "C:\Documents and Settings\"
.SearchSubFolders = True
.Filename = "File B.xls"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
For j = 1 To .FoundFiles.Count
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open Filename:=.FoundFiles(j)
objExcel.Visible = True
Application.DisplayAlerts = False
SourceFileName = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:="V:\File B.xls"
Application.DisplayAlerts = True
Next j
Else
MsgBox "file Not found, please check number and try again"
End If
End With
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Not quite sure but perhaps this method might work for you...

Untested ...

Rich (BB code):
Sub macro1()
    Dim objExcel As Excel.Application
    Dim SourceFileName As String
    Dim wb As Workbook
    With Application.FileSearch
        .NewSearch
        .LookIn = "C:\Documents and Settings\"
        .SearchSubFolders = True
        .Filename = "File A.xls"
        .MatchTextExactly = True
        .FileType = msoFileTypeAllFiles
        If .Execute() > 0 Then
            For j = 1 To .FoundFiles.Count
                Set objExcel = CreateObject("Excel.Application")
                wb = .FoundFiles(j)
                objExcel.Workbooks.Open Filename:=.FoundFiles(j)
                objExcel.Visible = True
                Application.DisplayAlerts = False
                SourceFileName = ActiveWorkbook.Name
                With wb
                    .SaveCopyAs ("V:\File .xls")
                End With

                'ActiveWorkbook.SaveAs Filename:="V:\File .xls"
                Application.DisplayAlerts = True
            Next j
        Else
            MsgBox "file Not found, please check number and try again"
        End If
    End With
    With Application.FileSearch
        .NewSearch
        .LookIn = "C:\Documents and Settings\"
        .SearchSubFolders = True
        .Filename = "File B.xls"
        .MatchTextExactly = True
        .FileType = msoFileTypeAllFiles
        If .Execute() > 0 Then
            For j = 1 To .FoundFiles.Count
                Set objExcel = CreateObject("Excel.Application")
                wb = .FoundFiles(j)
                objExcel.Workbooks.Open Filename:=.FoundFiles(j)
                objExcel.Visible = True
                Application.DisplayAlerts = False
                SourceFileName = ActiveWorkbook.Name
                With wb
                    .SaveCopyAs ("V:\File .xls")
                End With
                'ActiveWorkbook.SaveAs Filename:="V:\File B.xls"
                Application.DisplayAlerts = True
            Next j
        Else
            MsgBox "file Not found, please check number and try again"
        End If
    End With
End Sub
 
Upvote 0
Thanks for your response mate, but it comes up with the following error

Run Time Error '91':
Object variable or With block variable not set
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top