プログラミング

プログラミングに関するフォーラムです。VBAは除きます。
  • 解決済みのトピックにはコメントできません。
このトピックは解決済みです。
質問

 
(Windows 7 Professional : 指定なし)
vbsでメール送信
投稿日時: 19/05/23 11:50:06
投稿者: ラングドシャ

お願いします。
vbsをbatファイルで呼び出し、メール送信を行っています。
その際、Z:\test フォルダのファイルを添付して送っています。
今回、添付したいファイルが複数になり、Z:\testフォルダにある
すべてのファイルを添付したいのですが、vbsをどのように変更すれば
良いのかわかりません。ご指導お願いいたします。
以下のコードは、検索してサイトから使わせていただいているものです。

Dim Fs, strPath, Fl, F, NewFile, NewFileFP, OutF
Set Fs = CreateObject("Scripting.FileSystemObject")
strPath = "Z:\test"

NewFile = ""

Set Fl = Fs.GetFolder(strPath)
 
For Each F In Fl.Files

 If F.Name > NewFile Then
 NewFile = F.Name
 End If

Next

 NewFileFP = Fl & "\" & NewFile
 
Dim WshShell, BtnCode
 Set WshShell = WScript.CreateObject("WScript.Shell")
 Set objShell = CreateObject("Wscript.Shell")
 Set oMsg = CreateObject("CDO.Message")
 oMsg.From = "送信者 <test@xxxx.com>" '送信元
 oMsg.To = "受信1 様 <jyusin1@xxxx.com>" '送信先
 oMsg.Cc = "受信2 様 <jyusin2@xxxx.com>"
 oMsg.Bcc = ""
 oMsg.Subject = "TEST送信" '件名
 oMsg.TextBody = "各位" & vbCrLf & vbCrLf & "テスト送信です。" 'メール本文
 oMsg.AddAttachment NewFileFP '添付
 oMsg.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 oMsg.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.xxxx.jp"
 oMsg.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
 oMsg.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 oMsg.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxx@xxxx.com"
 oMsg.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
 oMsg.Configuration.Fields.Update
 oMsg.Send
 
Set OutF = Fs.OpenTextFile("C:\mail\send.log",8)
 OutF.WriteLine "Date[" & date & "] Time[" & time & "] File[" & NewFile & "]"
 OutF.Close
 
Fs.DeleteFile NewFileFP
 objShell.Popup "メール送信しました。", 0, "送信完了", 0
 Set Fs = Nothing
 Set Fl = Nothing
 Set OutF = Nothing 
	

回答
投稿日時: 19/05/23 15:04:34
投稿者: sk

引用:
vbsをbatファイルで呼び出し、メール送信を行っています。
その際、Z:\test フォルダのファイルを添付して送っています。
今回、添付したいファイルが複数になり、Z:\testフォルダにある
すべてのファイルを添付したいのですが、vbsをどのように変更すれば
良いのかわかりません。

引用:
oMsg.AddAttachment NewFileFP '添付

If Fs.FolderExists(strPath) Then
    Set Fl = Fs.GetFolder(strPath)
    For Each F In Fl.Files
        oMsg.AddAttachment F.Path
    Next
    Set Fl = Nothing
Else
    MsgBox "フォルダー """ & strPath & """ を参照出来ません。", vbCritical, "エラー"
End If
 
-----------------------------------------------------
 
以上のように書き換えればよろしいのではないかと。

投稿日時: 19/05/23 15:47:19
投稿者: ラングドシャ

sk様
 
いつもお世話になっております。
回答いただきありがとうございました。
 
教えていただきましたコードでうまく添付できました。
 
ありがとうございました。