【问题标题】:Sending outlook email with body as contents of a text file发送带有正文作为文本文件内容的 Outlook 电子邮件
【发布时间】:2018-11-23 21:50:25
【问题描述】:

我想使用 VBScript 发送 Outlook 电子邮件。电子邮件的正文应包含文本文件的内容,例如sha.txt。下面是我正在使用的代码,但它给了我这个错误:

运行时错误“287”:应用程序定义或对象定义错误

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

【问题讨论】:

  • 您有 4 完全不同版本的 VB 的标签...System.IO.File.ReadAllText 是 .net,请参阅stackoverflow.com/questions/3117121/… 了解 VBScript/VB6/VBA 的读取方法文件(As String 也不是有效的 VBScript)
  • 嗨 Aex,我怎样才能在 vb srcipt 中实现这个...????我是初学者,所以请指导解决方案..谢谢!!!
  • 如果我使用上面编辑的代码,它会给我“运行时错误'287':应用程序定义或对象定义错误”。请建议并指导我找到可能的解决方案..谢谢跨度>
  • 您尚未声明您的 objFileToRead (Dim objFileToRead),但是您显示的代码在 VB6 中为我运行,并在我运行 Outlook 10 的 Windows 7 机器上作为 VB 脚本运行。
  • 哪一行给你错误?此外,是否可以选择不使用 Outlook(使用 CDO)发送邮件?

标签: vbscript


【解决方案1】:
    '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

【讨论】:

  • 非常感谢弗拉德...非常感谢您的帮助...您的代码有效.. :)
  • 还有1个问题--当我运行此代码时,我面前出现了一封电子邮件草稿,但是当我发送该电子邮件时,我的收件箱中没有任何内容。请您告诉我可能是什么问题这里...??
  • 问题已解决,它卡在我的发件箱中.. :)...感谢大家的努力..特别感谢 Vlad...!!!干杯..!!!
猜你喜欢
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多