我在 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