试图将文本写入文件错误'5'

时间:2013-05-21 22:40:13

标签: vba

我正在尝试获取代码,浏览并删除所有标记,然后将其写入新文档testfile.txt。出于某种原因,我在第5行遇到错误: 设置ts = f.openastextstream(forwriting,tristateusedefault) 并得到错误无效的程序。 这是我的代码:

Sub elizabethwhite()
Set fs = CreateObject("scripting.filesystemobject")
fs.createtextfile "testfile.txt"
Set f = fs.getfile("testfile.txt")
Set ts = f.openastextstream(forwriting, tristateusedefault)

textline = ""
Do While f.opentextstream(forwriting, tristateusedefault).atendofstream <> True
textline = textline & f.opentextstream(forwriting, tristateusedefault).readline & "<BR>"

count = 0
pOne = 1
Do While InStr(textline, "<img") <> 0
count = count + 1
pOne = InStr(pOne, textline, "<img")

Do While InStr(pOne, textline, ">") = 0 & ts.atendofstream <> True
pTwo = InStr(pOne, textline, ">")
Loop

If 0 < count < 10 Then
textline = Left(textline, pOne - 1) & "{{image00" & count & ".jpg}}" & Right(textline, pTwo + 1)
ElseIf 9 < count < 100 Then
textline = Left(textline, pOne - 1) & "{{image0" & count & "}}.jpg" & Right(textline, pTwo + 1)
End If
Loop
Loop
ts.write textline
ts.Close

End Sub

2 个答案:

答案 0 :(得分:1)

正确声明您的变量,并使用Option Explict来识别问题。不是说,这些是开发的好习惯,可以帮助你编写更好的代码。它们还启用了脚本辅助功能,非常方便。

问题是您尚未启用对MS Scripting Runtime库 AND 的引用,因此,ForReadingTriStateUseDefault被编译器解释为变量,它们是没有值的变量,因此您将无效参数传递给OpenAsTextStream方法。

Option Explicit会帮助您发现此错误:

Option Explicit helps identify errors with uninitialized variables

如果您添加对Microsoft Scripting Runtime的引用,您的代码将按原样运行,但仍会敦促您按类型声明所有变量,并使用Option Explicit。两者都将为您节省很多麻烦:)

enter image description here

Sub elizabethwhite()

    Dim fs As New Scripting.FileSystemObject
    Dim f As Scripting.File
    Dim ts As Scripting.TextStream


    fs.CreateTextFile "testfile.txt"
    Set f = fs.getfile("testfile.txt")

    Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)

    ts.WriteLine "Hello!"

    '
    '### The rest of your code goes here... remember to declare any other variables :)
    '

    Set ts = Nothing
    Set f = Nothing
    Set fs = Nothing

End Sub

另请参阅(有关OpenAsTextStream方法的文档):

http://msdn.microsoft.com/en-us/library/aa265341(v=vs.60).aspx

答案 1 :(得分:0)

当我尝试将unicode字符串写入非unicode文本文件时出现此错误。您有两种选择:

  1. 明确地将文件作为unicode打开

    Set ts = f.OpenAsTextStream(ForWriting, TristateTrue)   
    
  2. 在写入文件之前将字符串从unicode转换为ASCII

  3. 以下代码将帮助您在写入文件之前从输出字符串中删除unicode字符:

        Dim regex
        Set regex = CreateObject("vbscript.regexp")
        With regex
            .Global = True
            .Pattern = "[^\u0000-\u007F]+"
        End With
        MsgBox regex.Replace(Replace(yourStringHere, Chr(160), Chr(32)), vbNullString)  
    

    内部替换功能只是标准的VBA替换,它将一个空白字符转换为另一个空格字符。我不得不添加它,因为正则表达式替换剥离了字符\ u00A0也是出于某种原因。

    所以你的代码将是:

        Sub elizabethwhite()
    
        Dim regex
        Set regex = CreateObject("vbscript.regexp")
        With regex
            .Global = True
            .Pattern = "[^\u0000-\u007F]+"
        End With
    
        Set fs = CreateObject("scripting.filesystemobject")
        fs.createtextfile "testfile.txt"
        Set f = fs.getfile("testfile.txt")
        Set ts = f.openastextstream(forwriting, tristateusedefault)
    
        textline = ""
        Do While f.opentextstream(forwriting, tristateusedefault).atendofstream <> True
        textline = textline & f.opentextstream(forwriting, tristateusedefault).readline & "                <BR>"
    
        count = 0
        pOne = 1
        Do While InStr(textline, "<img") <> 0
        count = count + 1
        pOne = InStr(pOne, textline, "<img")
    
        Do While InStr(pOne, textline, ">") = 0 & ts.atendofstream <> True
        pTwo = InStr(pOne, textline, ">")
        Loop
    
        If 0 < count < 10 Then
        textline = Left(textline, pOne - 1) & "{{image00" & count & ".jpg}}" &         Right(textline, pTwo + 1)
        ElseIf 9 < count < 100 Then
        textline = Left(textline, pOne - 1) & "{{image0" & count & "}}.jpg" &         Right(textline, pTwo + 1)
        End If
        Loop
        Loop
        ts.write regex.Replace(Replace(textline, Chr(160), Chr(32)), vbNullString)
        ts.Close
    
        End Sub
    

    剥离unicode字符只是诊断的快速修复。您可能需要进行更彻底的故障排除(也许可以对unicode字符进行一些翻译,而不是简单地剥离它们。)