Runtime Error 5: Invalid procedure call or argument

webpage , vba , excel India
  • 11 years ago

    Hi all, I am trying to extract the texts from a web page and write it to a text file. I am able to read the text by using internetexplorer.application but when I try to write it to the notepad sometimes it gives Runtime Error 5: Invalid procedure call or argument. Microsoft VB Help says that it happens bcoz of "An argument probably exceeds the range of permitted values."

    Can someone help me to get rid of this problem.

    Thanks in advance.

    This is the code..

      Sub GetTextFromIe()
    Dim NPOFile$, myFile$
    Dim ie As Object, objDoc As Object, f As Object, fs As Object
    Dim strURI As String
    strURI = ""
    Set ie = CreateObject("internetexplorer.application")
    ie.Navigate strURI
    'Wait for page to load!
    If ie.ReadyState = 4 Then
    ie.Visible = False
    Exit Do
    End If
    Set objDoc = ie.Document
    strMyPage = objDoc.body.innerText
    'Use this Text file!
    Dim FileSave
    FileSave = Application.GetSaveAsFilename("Contents-" & Format(Date, "dd-mm-yy") & "-" & Format(Time, "hh") & "-" & Format(Time, "nn") & "-" & Format(Time, "ss"), "Text Files (*.txt),*.txt")
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.CreateTextFile(FileSave, True)
    myFile = FileSave
    NPOFile = "NotePad.exe " & myFile
    'Work with file [Append Text to File].
    f.Write (strMyPage)
    'Open NotePad with a data file!
    Call Shell(NPOFile, 1)
    Set objDoc = Nothing
    Set ie = Nothing
    End Sub

    The line f.Write (strMyPage) gives me the error.

  • 11 years ago

    Should that be : f.WriteLine instead of f.Write ?

    Is strMyPage declared as string ?

    Why don't you use simple file handling to save the string data to a TXT file?


    open filename write down the string close file

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”