none
VB Script Error RRS feed

  • Question

  • Hi

    I am a newbie to VB, I am trying to write a script to include the contents of a text file into the body of the email,

    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    'Open the file for reading
    Set f = fso.OpenTextFile("C:\SendLogs\dailylog.txt", ForReading)
    'The ReadAll method reads the entire file into the variable BodyText
    BodyText = f.ReadAll
    'Close the file
    f.Close
    Set f = Nothing
    Set fso = Nothing

    It says line4 char 1 invalid procedure or argument.

    Tuesday, June 23, 2015 9:02 AM

Answers

  • Try this way - 

    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpenTextFile("C:\SendLogs\Dailylog.txt", 1)
    
    BodyText = f.ReadAll
    
    f.Close
    Set f = Nothing
    Set fso = Nothing

    • Marked as answer by Jayaram_Live Tuesday, June 23, 2015 10:19 AM
    Tuesday, June 23, 2015 9:10 AM
  • Don't just be a copy cat and copy everything blindly.

    Here is all you need.

    Setfso = CreateObject("Scripting.FileSystemObject")
    setf =fso.OpenTextFile("C:\SendLogs\Dailylog.txt")
    BodyText =f.ReadAll()
    f
    .Close

    "OpenRead" is the default and dose not need to be specified.

    You do not need to set everything to nothing. That is an old hangover from VB4 and earlier on Windows 98 where memory cleanup was thought to be slow.  If you are creating hundreds of objects in a loop this might improve memory slightly.  today you seldom see it used.


    \_(ツ)_/

    • Marked as answer by Jayaram_Live Tuesday, June 23, 2015 10:19 AM
    Tuesday, June 23, 2015 10:00 AM

All replies

  • Try this way - 

    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpenTextFile("C:\SendLogs\Dailylog.txt", 1)
    
    BodyText = f.ReadAll
    
    f.Close
    Set f = Nothing
    Set fso = Nothing

    • Marked as answer by Jayaram_Live Tuesday, June 23, 2015 10:19 AM
    Tuesday, June 23, 2015 9:10 AM
  • Don't just be a copy cat and copy everything blindly.

    Here is all you need.

    Setfso = CreateObject("Scripting.FileSystemObject")
    setf =fso.OpenTextFile("C:\SendLogs\Dailylog.txt")
    BodyText =f.ReadAll()
    f
    .Close

    "OpenRead" is the default and dose not need to be specified.

    You do not need to set everything to nothing. That is an old hangover from VB4 and earlier on Windows 98 where memory cleanup was thought to be slow.  If you are creating hundreds of objects in a loop this might improve memory slightly.  today you seldom see it used.


    \_(ツ)_/

    • Marked as answer by Jayaram_Live Tuesday, June 23, 2015 10:19 AM
    Tuesday, June 23, 2015 10:00 AM
  • Thank You Braham and JRV, it worked with a minor Tweak

    Tuesday, June 23, 2015 10:20 AM