Home » excel » excel – Using TextStream Method WriteBlankLines

excel – Using TextStream Method WriteBlankLines

Posted by: admin May 14, 2020 Leave a comment

Sub tryMethod()

    Dim objTxt as textstream

    Dim filename as string
        fileName = "Z:\New folder\TextDoc.txt"

    Set fSo = New Scripting.FileSystemObject

    Set objTxt = fSo.OpenTextFile(fileName, ForReading)

    str = objTxt.WriteBlankLines(1)

End Sub

No matter what number I put into the brackets after calling method writeblanklines I get the following error:

expected function or variable

I have checked documentation and do not see an example for this method. First two pages of google also didn’t give me an example to work off of.

How to&Answers:

You have opened the file for reading Set objTxt = fSo.OpenTextFile(fileName, ForReading) and you are trying to write.
This is how to open it for writing:

Sub TestMe()

    Dim objTxt As TextStream
    Dim fso As Object
    Dim filename As String
    filename = "C:\Users\User\Desktop\nd.txt"

    Set fso = New Scripting.FileSystemObject

    Set objTxt = fso.OpenTextFile(filename, ForWriting)
    objTxt.WriteBlankLines 23

End Sub

The MSDN documentation (from @braX comment) is not as good as one would expect – the ForWriting constant is present only in the example:

enter image description here

However, the ForWriting is present in the GitHub, maybe one day when the MSDN and the GitHub would be sync-ed it will be there as well:

enter image description here