使用TextStream方法WriteBlankLines

时间:2018-06-04 15:16:00

标签: excel vba

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

无论我在调用方法writeblanklines后放入括号中的数字,我都会收到以下错误:

  

预期函数或变量

我检查过文档但没有看到此方法的示例。谷歌的前两页也没有给我一个工作的例子。

1 个答案:

答案 0 :(得分:2)

您已打开文件以阅读Set objTxt = fSo.OpenTextFile(fileName, ForReading),并且您正在尝试撰写。 这是如何打开它写作:

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(来自@braX评论)并不像人们期望的那样好 - ForWriting常量仅出现在示例中:

enter image description here

然而,ForWriting is present in the GitHub,也许有一天,当MSDN和GitHub同步时,它也会在那里:

enter image description here