3

I want to send an outlook email using VBScript. The body of the email should contains the contents of a text file, say sha.txt. Below is the code I am using but it's giving me this error:

Run Time error '287': Application-defined or Object defined error

Sub email1()   
  Dim outobj, mailobj    
  Dim strFileText 
  Dim objFileToRead    

  Set outobj = CreateObject("Outlook.Application")    
  Set mailobj = outobj.CreateItem(0)    
  Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\sonu\Desktop\auto\sha.txt", 1)    
  strFileText = objFileToRead.ReadAll()
  objFileToRead.Close
  Set objFileToRead = Nothing    
  With mailobj    
    .To = "user@user.com"    
    .Subject = "Testmail"    
    .Body = strFileText    
    .Send    
  End With    

  'Clear the memory
  Set outobj = Nothing    
  Set mailobj = Nothing    
End Sub
Shashank
  • 41
  • 2
  • 2
  • 6
  • You have tags for **4** completely different versions of VB... `System.IO.File.ReadAllText` is .net, see http://stackoverflow.com/questions/3117121/reading-and-writing-value-from-a-textfile-by-using-vbscript-code for a VBScript/VB6/VBA method for reading a file (`As String` is also not valid VBScript) – Alex K. Jul 03 '13 at 13:52
  • Hi Aex, How Can I implement this in vb srcipt...???? I am a beginner so please guide to the solution..Thanks!!! – Shashank Jul 03 '13 at 14:19
  • If I use the above edited code,It gives me "Run Time error '287': Apllication-defined or Object defined error".Please advise and guide me to the possible solution..Thanks – Shashank Jul 03 '13 at 15:09
  • You have not declared your objFileToRead (Dim objFileToRead), however the code you show runs for me in VB6 and as a VB Script on my windows 7 machine running Outlook 10. – jac Jul 03 '13 at 19:24
  • Which line is giving you the error? Also, would sending mail without Outlook (using [CDO](http://www.paulsadowski.com/wsh/cdo.htm)) be an option? – Ansgar Wiechers Jul 03 '13 at 22:16
  • @ Jac,Even if I define the Dim objFileToRead,it gives me the same error.Me too is running the same code in VB6 and when I run it as a VB srcipt,it does noothing nor it gives error neither desire output.. – Shashank Jul 04 '13 at 05:06
  • @ Jac I am trying to Run this code in Excel 2007 – Shashank Jul 04 '13 at 05:20
  • Hi Ansgar,Can you help me in Debuging this code or you can give me alternate as I am a beginnner and don't know much about it..Thanks!! – Shashank Jul 04 '13 at 05:33

1 Answers1

4
    'I didn't look into the particular issue with your file reading.
    'Below my example, just tested it works.
    'Good luck mate :)

        Sub CatchMe()

          Dim outobj, mailobj
          Dim strFileText
          Dim objFileToRead

          Set outobj = CreateObject("Outlook.Application")
          Set mailobj = outobj.CreateItem(0)
          strFileText = GetText("C:\Share\1.txt")

            With mailobj
            .To = "user@user.com"
            .Subject = "Testmail"
            .Body = strFileText
            .Display
          End With

          'Clear the memory
          Set outobj = Nothing
          Set mailobj = Nothing

        End Sub

        Function GetText(sFile As String) As String
           Dim nSourceFile As Integer, sText As String
           nSourceFile = FreeFile
           'Write the entire file to sText
           Open sFile For Input As #nSourceFile
           sText = Input$(LOF(1), 1)
           Close
           GetText = sText
        End Function
  • Thank You so much Vlad...your help much appreciated...Your code worked.. :) – Shashank Jul 04 '13 at 12:03
  • 1 more question--When I am Running this code,a draft email comes before me but when I send that email,nothing is coming in my INBOX.Could you please advise me what could be the problem here...?? – Shashank Jul 04 '13 at 12:39
  • Issue resolved,It was stuck in my Outbox.. :)...Thanks to all of you for your efforts..special thanks to Vlad...!!! Cheers..!!! – Shashank Jul 04 '13 at 13:49