エクセルでメール送信 VBA

使われていないコードがあるけれどそれは無視してください。
基本的にはコピーアンドペーストしてある程度カスタマイズすればつかえるとおもいます。
前提条件として、Outlookでメールの送信するためのコードです。
現状としてはB列に各種値を入力するものとして、
To アドレスはB8
CCアドレスはB9
件名はB12
添付ファイルはB13。
本文はB15から各行です。
あとはオリジナルを自身で作り上げてください。




Sub masenddt()
Application.ScreenUpdating = False
Dim toaddress, ccaddress, bccaddress As String
Dim subject, mailBody, credit As String '
Dim outlookObj As Outlook.Application
Dim mailItemObj As Outlook.mailItem
Dim MaxRow, MaxCol As Long
Dim test1 As String
Dim i As Long
i = 15
test = ""

     MaxRow = Cells.SpecialCells(xlCellTypeLastCell).Row

    
    
    


Do While i <= MaxRow
'If Range("B" & i) = "" Then
'test1 = test1 + vbCrLf
'Else
'test1 = test1 & Range("B" & i) + vbCrLf
'End If

If i < MaxRow + 1 Then
   If Range("B" & i) = "" Then
    test1 = test1 + vbCrLf
    Else
    
    test1 = test1 & Range("B" & i) & vbCrLf
    
    End If
Else
    test1 = test1

End If
i = i + 1
Loop

mailBody = test1

credit = "==============================================="  '署名設定はここ



'----------------以上メール内容、以下処理機構----------------




'toaddress = Range("B8")
'ccaddress = Range("B9")
'subject = Range("B12")

Set outlookObj = CreateObject("Outlook.Application")
Set mailItemObj = outlookObj.CreateItem(olMailItem)
mailItemObj.BodyFormat = 1
mailItemObj.To = toaddress
mailItemObj.cc = ccaddress
mailItemObj.BCC = bccaddress
mailItemObj.subject = subject


mailItemObj.Body = mailBody & vbCrLf & credit
'添付ファイルについては1ファイルですが可能です。

'Dim attached, attached2 As String
'Dim myattachments As Outlook.Attachments
'Set myattachments = mailItemObj.Attachments
'attached = Range("B13")


'myattachments.Add attached



'mailItemObj.Save
mailItemObj.Display
mailItemObj.Send


Set outlookObj = Nothing
Set mailItemObj = Nothing



Application.ScreenUpdating = True
ActiveWorkbook.Saved = True
  '  ActiveWorkbook.Close
End Sub




コメント

このブログの人気の投稿

エクセルマクロをすこし 2018年8月25日 リストから別のリストを作成