With Application.FileSearch - Runtime error '445': Object doesn't support this action

Hooley17

New Member
Joined
Jan 23, 2013
Messages
6
Hi all - Would some kind soul please put me out of my misery - worked in XL2003 - but not in XL2010. Have searched forum, but need someone to amend my specific bit of inherited code. Cheers !


Rich (BB code):
    'RECORDED FROM A CSV FILE SAVED TO XL
    'ChDir "C:\Users\Mark\Desktop"
    'ActiveWorkbook.SaveAs Filename:="C:\Users\Mark\Desktop\test.xls", FileFormat _
        ':=xlExcel9795, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
        'False, CreateBackup:=False
    
    With Application.FileSearch
        .LookIn = CSVPath
        .Filename = "*.csv"
        .Execute
        
        For CSVCount = 1 To .FoundFiles.Count
            Set wb = Application.Workbooks.Open(.FoundFiles(CSVCount))
            'wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlXMLSpreadsheet
            wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlExcel9795
 
Last edited by a moderator:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Filesearch was deprecated with Excel 2007 but you can use Dir instead - something like:
Code:
    Dim strFile As String
    strFile = Dir(CSVPath & "*.csv")
    Do While strFile <> vbNullString

        Set wb = Application.Workbooks.Open(CSVPath & strFile)
        'wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlXMLSpreadsheet
        wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlExcel9795
        wb.Close False
        strFile = Dir()
    Loop
 
Upvote 0
Great - almost there, but now I have a Compile error - here's the code in full. It's just some VBA to convert files from CSV to XLS to enable me to import them into ACCESS 2000


Code:
Option Explicit
Sub CSVtoXLS()
Dim wb As Workbook
Dim CSVPath As String
Dim CSVCount As Integer
    
    'CSVPath = "[URL="file://\\DFZ7***.link2.gpn.***.uk\10014699$\"]\\DFZ7***.link2.gpn.***.uk\10014699$\[/URL]"
    'CSVPath = "d:\BT\"
    
    CSVPath = InputBox("Type the full path (eg. c:\data\...")
    
    
    'RECORDED FROM A CSV FILE SAVED TO XL
    'ChDir "C:\Users\Mark\Desktop"
    'ActiveWorkbook.SaveAs Filename:="C:\Users\Mark\Desktop\test.xls", FileFormat _
        ':=xlExcel9795, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
        'False, CreateBackup:=False
    
  Dim strFile As String
    strFile = Dir(CSVPath & "*.csv")
    Do While strFile <> vbNullString
        Set wb = Application.Workbooks.Open(CSVPath & strFile)
        'wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlXMLSpreadsheet
        wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlExcel9795
        wb.Close False
        strFile = Dir()
    Loop
                
            
            Range("A1").Select
    If ActiveCell = "TARIFFTYPE" Then
    Range("A1").Select
    wb.Save
    wb.Close
    
    Else
    
    Selection.EntireRow.Insert
    ActiveCell.FormulaR1C1 = "TARIFFTYPE"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "ORIGINATINGNUMBER"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "RECEIVINGNUMBER"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "DURATION"
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "TIMEOFCALL"
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "TIMEBAND"
    Range("G1").Select
    ActiveCell.FormulaR1C1 = "CALLDATE"
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "COSTOFCALL"
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "DISTANCEBAND"
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "SLOC"
    Range("A1").Select
    wb.Save
    wb.Close
    End If
    Next
         
         'MsgBox (CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls" & " - DONE")
    End With
End Sub
 
Upvote 0
Always better to post the full code. ;)

Try this:
Code:
Sub CSVtoXLS()
    Dim wb                    As Workbook
    Dim CSVPath               As String
    Dim CSVCount              As Integer
    Dim strFile               As String

    'CSVPath = "\\DFZ7***.link2.gpn.***.uk\10014699$\"
    'CSVPath = "d:\BT\"

    CSVPath = InputBox("Type the full path (eg. c:\data\...")


    'RECORDED FROM A CSV FILE SAVED TO XL
    'ChDir "C:\Users\Mark\Desktop"
    'ActiveWorkbook.SaveAs Filename:="C:\Users\Mark\Desktop\test.xls", FileFormat _
     ':=xlExcel9795, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
     'False, CreateBackup:=False

    strFile = Dir(CSVPath & "*.csv")
    Do While strFile <> vbNullString
        Set wb = Application.Workbooks.Open(CSVPath & strFile)
        'wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlXMLSpreadsheet
        wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlExcel9795

        If Range("A1").Value <> "TARIFFTYPE" Then
            Rows(1).Insert
            Range("A1:J1").Value = Array("TARIFFTYPE", "ORIGINATINGNUMBER", "RECEIVINGNUMBER", _
                                         "DURATION", "TIMEOFCALL", "TIMEBAND", "CALLDATE", _
                                         "COSTOFCALL", "DISTANCEBAND", "SLOC")
            wb.Save
        End If
        wb.Close False
        strFile = Dir()
    Loop

    'MsgBox (CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls" & " - DONE")
End Sub
 
Upvote 0
Rory - greatly apreciate your help - one error still showing -

Run-time error '1004':
Method 'SaveAs' of object ' _Workbook' failed.

Code:
Sub CSVtoXLS()
    Dim wb                    As Workbook
    Dim CSVPath               As String
    Dim CSVCount              As Integer
    Dim strFile               As String
    'CSVPath = "[URL="file://\\DFZ7***.link2.gpn.***.uk\10014699$\"]\\DFZ7***.link2.gpn.***.uk\10014699$\[/URL]"
    'CSVPath = "d:\BT\"
    CSVPath = InputBox("Type the full path (eg. c:\data\...")

    'RECORDED FROM A CSV FILE SAVED TO XL
    'ChDir "C:\Users\Mark\Desktop"
    'ActiveWorkbook.SaveAs Filename:="C:\Users\Mark\Desktop\test.xls", FileFormat _
     ':=xlExcel9795, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
     'False, CreateBackup:=False
    strFile = Dir(CSVPath & "*.csv")
    Do While strFile <> vbNullString
        Set wb = Application.Workbooks.Open(CSVPath & strFile)
        'wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlXMLSpreadsheet
[COLOR=#FF0000]        wb.SaveAs CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls", xlExcel9795[/COLOR]
        If Range("A1").Value <> "TARIFFTYPE" Then
            Rows(1).Insert
            Range("A1:J1").Value = Array("TARIFFTYPE", "ORIGINATINGNUMBER", "RECEIVINGNUMBER", _
                                         "DURATION", "TIMEOFCALL", "TIMEBAND", "CALLDATE", _
                                         "COSTOFCALL", "DISTANCEBAND", "SLOC")
            wb.Save
        End If
        wb.Close False
        strFile = Dir()
    Loop
    'MsgBox (CSVPath & Left(wb.Name, Len(wb.Name) - 4) & ".xls" & " - DONE")
End Sub
 
Upvote 0
Do you have Excel set to block saving as Excel95-97 format in Trust Center? (is there a reason for using that rather than xlExcel8?)
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,300
Members
449,095
Latest member
Chestertim

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