0

I am trying to take code, go through it and delete all tags and then write it to a new document testfile.txt. For some reason I am getting an error with line 5: Set ts = f.openastextstream(forwriting, tristateusedefault) and am getting error invalid procedure. Here is my code:

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 Answers2

1

Properly declaring your variables, and using Option Explict will identify the problem. Not tomention, these are good habits to develop and will help you write better code. They also enable the script assist feature, which comes in very handy.

The problem is that you have not enabled a reference to MS Scripting Runtime library AND because of this, ForReading and TriStateUseDefault are being interpreted by the compiler as variables and they are variables with no values, so you are passing invalid parameters to the OpenAsTextStream method.

Option Explicit would have helped you identified this error:

Option Explicit helps identify errors with uninitialized variables

If you add a reference to the Microsoft Scripting Runtime, your code will work as-is, but would still urge you to declare ALL variables by type, and use Option Explicit. Both will save you a lot of trouble in the future :)

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

See also (documentation about the OpenAsTextStream method):

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

David Zemens
  • 53,033
  • 11
  • 81
  • 130
1

I got this error when I tried to write unicode string to non-unicode text file. You have two options:

  1. Open the file as unicode explicitly

    Set ts = f.OpenAsTextStream(ForWriting, TristateTrue)   
    
  2. Convert string from unicode to ASCII before writing to the file

Following code will help you strip unicode characters from output string before writing to the file:

    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)  

Inner Replace function is just standard VBA Replace which translates one whitespace character to another. I had to add it because regex replace stripped the character \u00A0 too for some reason .

So your code will be:

    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

Stripping unicode characters is just a quick-fix for diagnostics. You may have to do more thorough troubleshooting (and maybe to do some translation of unicode characters instead of simply stripping them).

JohnyCash
  • 399
  • 1
  • 4
  • 9