Text File manipulation

L

Legacy 15162

Guest
I am trying to replace a variable amount of characters at the beginning of a line. for this example, the file as 32 characters at the beginning to be deleted. Here is my code as such. I keep getting the "object variable or with block variable not set" error at the specified location.
Rich (BB code):
Public Function WebReport()
Dim i As Integer
Dim fso As New FileSystemObject
Dim folder As folder
Dim filename As String

Set folder = fso.GetFolder("c:\webreports")
For Each file In folder.Files
    filename = fso.GetFileName(file)
    Select Case True
        Case InStr(1, filename, "CC_CSHAUD")
            FormatReport filename, 32

    End Select

Next file
End Function

Public Function FormatReport(filename As String, k)
Dim fso As FileSystemObject
Dim ts As TextStream
Dim tsvar As String


Set ts = fso.OpenTextFile(filename, ForAppending, True)
tsvar = ts.ReadLine()
    While Not ts.AtEndOfStream
        tsvar = ts.ReadLine()
        tsvar = Replace(tsvar, Left(tsvar, k), "")
    Wend
End Function

any help would be greatly appreciated.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
this code works
Code:
Public Function WebReport()
Dim i As Integer
Dim fso As New FileSystemObject
Dim folder As folder
Dim fmtfolder As folder
Dim fmtfilename As String

Set folder = fso.GetFolder("c:\webreports")
Set fmtfolder = fso.GetFolder("c:\webreports\formattedWebReports")
For Each file In folder.Files
    fmtfilename = file.Name
    Select Case True
        Case InStr(1, file.Path, "CC_CSHAUD")
            FormatReport file.Path, fmtfolder & "\" & fmtfilename, 32

    End Select

Next file
End Function

Public Function FormatReport(fileName As String, fmtfilename As String, k)
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim tsvar As String
Dim tsnew As TextStream


Set ts = fso.OpenTextFile(fileName)
Set tsnew = fso.CreateTextFile(fmtfilename)
tsvar = ts.ReadLine()
    While Not ts.AtEndOfStream
        tsvar = ts.ReadLine()
        tsvar = Replace(tsvar, Left(tsvar, k), "")
        tsvar = LTrim(tsvar)
        tsvar = tsvar
        tsnew.WriteLine (tsvar)
    Wend
    tsnew.Close
End Function

now, to be able to migrate that into a vbs file is the next step. any suggestions?
 
Upvote 0

Forum statistics

Threads
1,221,899
Messages
6,162,686
Members
451,782
Latest member
LizN

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