临时文件试图保存到不存在的位置

时间:2015-09-30 14:47:02

标签: excel excel-vba vba

我遇到以下模块的问题。好吧,我的同事们,而且我不知道如何解决。当我自己运行子程序时,它工作正常,但我的一些同事正在收到错误。它看起来好像试图将临时文件保存到一个不存在的文件夹中。

有没有办法可以修改代码以将临时文件保存到特定位置?

谢谢。

Sub Email_Sheet()

Dim oApp As Object
Dim oMail As Object
Dim LWorkbook As Workbook
Dim LFileName As String

ActiveSheet.Calculate
SubjectPanel = InputBox("Please Enter the Time") & " text"

'Turn off screen updating
Application.ScreenUpdating = False

'Copy the active worksheet and save to a temporary workbook
ActiveSheet.Copy
Set LWorkbook = ActiveWorkbook

'Create a temporary file
LFileName = LWorkbook.Worksheets(1).Name
On Error Resume Next
'Delete the file if it already exists
Kill LFileName
On Error GoTo 0
'Save temporary file
LWorkbook.SaveAs Filename:=LFileName

'Create an Outlook object and new mail message
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)


With oMail
    .SentOnBehalfOfName = ""
    .To = "" & _
    "address"
    .CC = "" & _
    "other address"
    .Subject = SubjectPanel
    .Body = "Hello all," & vbNewLine & vbNewLine & _
            "body" & vbNewLine & vbNewLine & _
            "Thank you."
    .Attachments.Add LWorkbook.FullName
  .Display
End With

'Delete the temporary file and close temporary Workbook
LWorkbook.ChangeFileAccess Mode:=xlReadOnly
Kill LWorkbook.FullName
LWorkbook.Close SaveChanges:=False

'Turn back on screen updating
Application.ScreenUpdating = True
Set oMail = Nothing
Set oApp = Nothing

End Sub

2 个答案:

答案 0 :(得分:2)

行后

LWorkbook.SaveAs Filename:=LFileName

把这一行:

Debug.Print LWorkbook.FullName

现在,当您的同事运行它时,请他们先打开VBA编辑器,并要求他们确保显示立即窗口。然后让他们运行它,并在错误发生后询问他们在立即窗口中显示的内容。 (如果你说出实际的错误是什么,以及代码停在哪里,它也会很有用,但我会对此做出一些假设!)。

当我使用我的额外行运行代码时,在新工作簿上,我的立即窗口显示C:\Users\Simon\Documents\Sheet1.xlsx。然后它使用该名称和文件夹保存临时文件。

如果您可以向您的同事询问他们的说法,那么您可以查看该文件夹是否允许写入权限(请他们尝试将某些内容保存到该文件夹​​中)。

如果这是问题(如果我假设正确!),那么您需要更改SaveAs行以添加他们有权限的路径。例如:

LWorkbook.SaveAs Filename:= "C:\Users\xxx\Documents\" & LFileName

答案 1 :(得分:1)

您可以为临时文件添加保存位置的路径。

'Save temporary file
LWorkbook.SaveAs Filename:="C:\Temp\" & LFileName