0

Thanks to the wonderful teams of people at Stack Overflow, here is the following code that has been created. It works wonderfully, except for an error at the end that I cannot debunk.

Class:

Private m_arrBuffer
Private m_strDelimiter
Private Sub Class_Initialize()
    m_arrBuffer = Array()
    m_strDelimiter = “”
End Sub

Private Sub Class_Terminate()
    m_arrBuffer = Empty
End Sub

Public Property Get Delimiter()
    Delimiter = m_strDelimiter
End Property

Public Property Let Delimiter(strDelimiter)
    m_strDelimiter = strDelimiter
End Property

Public Sub Append(strValue)
    ReDim Preserve m_arrBuffer(UBound(m_arrBuffer) + 1)
    m_arrBuffer(UBound(m_arrBuffer)) = strValue
End Sub

Public Sub AppendLine(strValue)
    Me.Append strValue & vbCrLf
End Sub

Public Sub Compact()
    If Not Me.Delimiter = “” Then
        strOriginalDelimiter = Me.Delimiter
        Me.Delimiter = “”
    End If
    strTemp = Me.ToString
    m_arrBuffer = Array()
    Me.Append strTemp
    Me.Delimiter = strOriginalDelimiter
End Sub

Public Function ToArray()
    ToArray = m_arrBuffer
End Function

Public Function ToString()
    ToString = Join(m_arrBuffer, m_strDelimiter)
End Function

Code (credit to @Bond)

Dim sb
Set sb = New StringBuilder      ' Guessing here. You haven't shown the class name.
sb.Append "some string"
sb.Append "another string"
sb.Append "a third string"
....
sb.Delimiter = "<br>"
myHtmlFile.Write sb.ToString()

I am recieving an error at myHtmlFile.Write sb.ToString() Run-time error '424': Object required.

I am not sure but I believe this is the final hurdle before I have a working code. Any advice on how to alleviate this error? Thank you.

7
  • 1
    what you're doing is adding the result from your class to an existing and opened file. Therefore it would help us, if you show us the code which you use to create/open the html-file. This link gives you a short hint on how-to open textfiles Commented Aug 3, 2015 at 14:19
  • Im going to look into this and get back to you Commented Aug 3, 2015 at 14:23
  • I added an HTML file creation code to my own code and that itself is having issues. See changes above Commented Aug 3, 2015 at 14:31
  • ok you told IE to open the html-file but you didn't open the html-file with vba and added text so far. Commented Aug 3, 2015 at 14:36
  • I guess your myHtmlFile object has not been created. Can you call other methods/properties on that object properly? Commented Aug 3, 2015 at 14:42

1 Answer 1

1

for your problem i would say the solution would be: This code does already assume, that the string is finished

Sub CreateAfile
    Dim ie As Object, fs as object    

    Dim sb
    Set sb = New StringBuilder      ' Guessing here. You haven't shown the class name.
    sb.Append "some string"
    sb.Append "another string"
    sb.Append "a third string"
    ....
    sb.Delimiter = "<br>"

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set myHtmlFile = fs.CreateTextFile("U:\temp\MyHTMLfile.htm", True)
    myHtmlFile.WriteLine(sb.ToString())
    myHtmlFile.Close

    Set ie = CreateObject("Internetexplorer.Application")
    ie.Visible = True
    ie.Navigate "U:\temp\MyHTMLfile.htm"

End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

So Yes, this code does work on it's own accord. The issue remains that when my code hits MyHTMLfile.Write sb.ToString() I continue getting the object required error
could you please add the result you get from sb.toSTring() when i replace sb.toString() with "test</p>test1" in myhtmlfile.writeline() i get the expected result
As it turns out this code actually works perfectly :) Thank you so much @psychicebola

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.