Remove Blank line from end of file (1 Viewer)

Snowflake68

Registered User.
Local time
Today, 11:09
Joined
May 28, 2014
Messages
452
How do I remove the blank line from the end of the text file that this function is creating. The text file is created using the Docmd.TransferText method and then calls this function which opens the text file and inserts a header line at the top of the file but in doing so it is creating an extra line at the end of the file which I need removing.

Code:
Public Function InsertHeader_CS()

'Insert Header line into Text File

        Dim strFilePath As String
        Dim strFileDate As String
            
        strFilePath = [Application].[CurrentProject].[Path]
        strFileDate = Format(Now(), "yyyy_mm_dd_hhnn")
        
        Const ForReading = 1
    
        Const ForWriting = 2
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        Set objFile = objFSO.OpenTextFile(strFilePath & "\Export_CS" & strFileDate & ".txt", ForReading)
    
        strContents = objFile.ReadAll
    
        objFile.Close
        strHeaderLine = DLookup("[Header]", "[tbl_HeaderLine]")
    
    
        strNewContents = strHeaderLine & vbCrLf & strContents
    
        Set objFile = objFSO.OpenTextFile(strFilePath & "\Export_CS" & strFileDate & ".txt", ForWriting)
        
        objFile.WriteLine strNewContents
    
        objFile.Close

End Function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:09
Joined
May 7, 2009
Messages
19,230
Public Function InsertHeader_CS()

'Insert Header line into Text File

Dim strFilePath As String
Dim strFileDate As String

strFilePath = [Application].[CurrentProject].[Path]
strFileDate = Format(Now(), "yyyy_mm_dd_hhnn")

Const ForReading = 1

Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFilePath & "\Export_CS" & strFileDate & ".txt", ForReading)

strContents = Trim(objFile.ReadAll)

objFile.Close
strHeaderLine = DLookup("[Header]", "[tbl_HeaderLine]")


strNewContents = strHeaderLine & vbCrLf & strContents

Set objFile = objFSO.OpenTextFile(strFilePath & "\Export_CS" & strFileDate & ".txt", ForWriting)

'objFile.WriteLine strNewContents
objFile.Write strNewContents

objFile.Close

End Function
 

Snowflake68

Registered User.
Local time
Today, 11:09
Joined
May 28, 2014
Messages
452
Public Function InsertHeader_CS()

'Insert Header line into Text File

Dim strFilePath As String
Dim strFileDate As String

strFilePath = [Application].[CurrentProject].[Path]
strFileDate = Format(Now(), "yyyy_mm_dd_hhnn")

Const ForReading = 1

Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFilePath & "\Export_CS" & strFileDate & ".txt", ForReading)

strContents = Trim(objFile.ReadAll)

objFile.Close
strHeaderLine = DLookup("[Header]", "[tbl_HeaderLine]")


strNewContents = strHeaderLine & vbCrLf & strContents

Set objFile = objFSO.OpenTextFile(strFilePath & "\Export_CS" & strFileDate & ".txt", ForWriting)

'objFile.WriteLine strNewContents
objFile.Write strNewContents

objFile.Close

End Function

Thank you so much works like a charm.
 

Users who are viewing this thread

Top Bottom