0

我正在尝试获取代码,通过它并删除所有标签,然后将其写入新文档 testfile.txt。出于某种原因,我在第 5 行收到错误: Set 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
4

2 回答 2

1

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

问题是您没有启用对 MS Scripting Runtime 库的引用因此,编译器将其解释为变量ForReading并且它们是没有值的变量,因此您将无效参数传递给该方法。TriStateUseDefaultOpenAsTextStream

Option Explicit会帮助您识别此错误:

Option Explicit 有助于识别未初始化变量的错误

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

在此处输入图像描述

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

于 2013-05-22T02:00:10.490 回答
0

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

  1. 显式以 unicode 格式打开文件

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

以下代码将帮助您在写入文件之前从输出字符串中去除 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 字符进行一些翻译,而不是简单地剥离它们)。

于 2013-08-21T19:43:03.857 回答