Excel进程无法访问该文件,因为其他进程正在使用该文件

Excel进程无法访问该文件,因为其他进程正在使用该文件,excel,runtime-error,vba,Excel,Runtime Error,Vba,我已经创建了一个代码,可以将Excel文件保存为CSV格式,并使用GMail作为附件发送电子邮件 但是,我得到一个运行时错误,该错误表示该进程无法访问该文件,因为它正被另一个进程使用 请参阅下面我得到错误的部分代码。想知道如何解决这个问题 非常感谢您的帮助。多谢各位 'set the current directory to the location of the template ChDir ThisWorkbook.Path myDir = ThisWorkbook.Path 'captu

我已经创建了一个代码,可以将Excel文件保存为CSV格式,并使用GMail作为附件发送电子邮件

但是,我得到一个运行时错误,该错误表示该进程无法访问该文件,因为它正被另一个进程使用

请参阅下面我得到错误的部分代码。想知道如何解决这个问题

非常感谢您的帮助。多谢各位

'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 = "ddoctor@yahoo.com"
  .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

这不是最有效的方法,但效果非常好。将Excel工作簿保存为CSV后,我使用不同的文件名再次将其保存为CSV,然后附加第一个CSV文件

'save workbook as csv file
ThisWorkbook.Sheets("Report").SaveAs Filename:= _
"report_" & userName & "_" & Format(Now, "mmddyy"), _
FileFormat:=xlCSVMSDOS, CreateBackup:=False

'save workbook as csv file with different filename
ThisWorkbook.Sheets("Report").SaveAs Filename:= _
"report_" & userName & "_" & Format(Now, "mmddyy") & "(copy)", _
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 = "ddoctor@yahoo.com"
  .CC = ""
  .BCC = ""
  .AddAttachment myDir & Dir(myDir & fName)
End With

NewMail.send

MsgBox ("Mail has been Sent")

'Set the NewMail Variable to Nothing
Set NewMail = Nothing

ThisWorkbook.Close SaveChanges:=False

End If

我们可以看到其余的代码吗?如果您使用VBA代码
open
写出文件,不要忘记也关闭它。否则它将被封锁我编辑了我的文章。请参阅代码的其余部分。非常感谢。