0
ExcelファイルをCSV形式で保存し、GMailを添付ファイルとして電子メールで送信するコードを作成しました。Excelプロセスが別のプロセスで使用されているため、ファイルにアクセスできません。
しかし、別のプロセスで使用されているため、プロセスがファイルにアクセスできないというランタイムエラーが発生します。
エラーが発生している箇所のコードの一部をご覧ください。これを解決する方法を知りたいですか?
ご協力いただきまして誠にありがとうございます。ありがとうございました。
'set the current directory to the location of the template
ChDir ThisWorkbook.Path
myDir = ThisWorkbook.Path
'capture username
userName = ThisWorkbook.Sheets("Temp").Range("D2").Value
gMail = ThisWorkbook.Sheets("Tool").Range("B1").Value
sPassword = ThisWorkbook.Sheets("Tool").Range("B3").Value
'capture date
yDate = Format(Now, "mmddyy")
'save workbook as csv file
ThisWorkbook.Sheets("Report").SaveAs Filename:= _
"report_" & userName & "_" & Format(Now, "mmddyy"), _
FileFormat:=xlCSVMSDOS, CreateBackup:=False
'***********************************
'****send csv file as attachment****
'***********************************
Dim NewMail As Object
fName = "report_" & userName & "_" & yDate & ".csv"
myDir = myDir & "\"
Set NewMail = CreateObject("CDO.Message")
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = gMail
NewMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = sPassword
NewMail.Configuration.Fields.Update
'Set All Email Properties
With NewMail
.Subject = "Report for " & userName & " " & yDate
.From = gMail
.To = "[email protected]"
.CC = ""
.BCC = ""
.AddAttachment myDir & Dir(myDir & fName) 'This is where I am getting the error
End With
NewMail.send
MsgBox ("Mail has been Sent")
'Set the NewMail Variable to Nothing
Set NewMail = Nothing
ThisWorkbook.Close SaveChanges:=False
End If
残りのコードは表示できますか? – 0m3r
ファイルを書き出すためにVBAコード 'open'を使用している場合は、彼も閉じてください。それ以外の場合はブロックされます –
投稿を編集しました。コードの残りの部分を見てください。ありがとうございました。 – user3553285