我想使用 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 = "[email protected]"
.Subject = "Testmail"
.Body = strFileText
.Send
End With
'Clear the memory
Set outobj = Nothing
Set mailobj = Nothing
End Sub
'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 = "[email protected]"
.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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)