Я сделал небольшую подпрограмму в Excel, которая отправляет электронное письмо в соответствии с введенными вами параметрами:
Sub Mail_LSP(mailsubject As String, consignee As String, coalcopy As String, mailbody As String)
Dim mlook As Outlook.MailItem
Set mlook = Outlook.Application.CreateItem(olMailItem)
mySubject = mailsubject
mlook.To = consignee
mlook.CC = coalcopy
mlook.Subject = mailsubject
mlook.Body = mailbody
mlook.Send
End Sub
Я хочу улучшить параметр mailbody. Есть ли способ присвоить содержимое файла .txt переменной, чтобы затем использовать эту переменную в качестве параметра моей процедуры?
решение1
Пытаться FileSystemObject
:
Private Sub CommandButton1_Click()
Set fso = CreateObject("Scripting.FileSystemObject")
Path = Range("E3")
Set filestream = fso.OpenTextFile(Path, 1)
contents = filestream.ReadAll()
Range("F4") = contents
End Sub
решение2
Нашел способ, используя: Open mailbody For Input As #1
и затемLine Input #1
Sub Mail_LSP(mailsubject As String, consignee As String, coalcopy As String, mailbody As String)
Dim mlook As Outlook.MailItem
Dim linecontent As String
Dim mailcontent As String
Open mailbody For Input As #1
Do Until EOF(1)
Line Input #1, linecontent
mailcontent = mailcontent & vbNewLine & linecontent & vbNewLine
Loop
Close #1
Set mlook = Outlook.Application.CreateItem(olMailItem)
mySubject = mailsubject
mlook.To = consignee
mlook.CC = coalcopy
mlook.Subject = mailsubject
mlook.Body = mailcontent
mlook.Send
End Sub