2017-04-20 64 views
0

我有一個Excel工作簿,其中有幾個工作表。每個工作表都是一個我想單獨發送電子郵件的文檔。運行時1004錯誤保存到C驅動器

我在運行Office 2007的Windows XP SP3計算機上有工作簿.VBA代碼完美工作,並且可以通過電子郵件發送每個單獨的工作表。

我需要在Windows 7或Windows 10的電腦上運行此,再次與Office 2007。這是我的錯誤:

1004 error

Sub EmailWithOutlook() 
    Dim oApp As Object 
    Dim oMail As Object 
    Dim WB As Workbook 
    Dim FileName As String 
    Dim wSht As Worksheet 
    Dim shtName As String 

    Application.ScreenUpdating = False 

    ' Make a copy of the active worksheet 
    ' and save it to a temporary file 
    ActiveSheet.Copy 
    Set WB = ActiveWorkbook 

    FileName = WB.Worksheets(1).Name 
    On Error Resume Next 
    Kill "C:\" & FileName 
    On Error GoTo 0 
    WB.SaveAs FileName:="C:\" & FileName 

    'Create and show the Outlook mail item 
    Set oApp = CreateObject("Outlook.Application") 
    Set oMail = oApp.CreateItem(0) 
    With oMail 
     'Uncomment the line below to hard code a recipient 
     '.To = "[email protected]" 
     'Uncomment the line below to hard code a subject 
     '.Subject = "Subject Line" 
     'Uncomment the lines below to hard code a body 
     '.body = "Dear John" & vbCrLf & vbCrLf & _ 
      '"Here is the file you asked for" 
     .Attachments.Add WB.FullName 
     .Display 
    End With 

    'Delete the temporary file 
    WB.ChangeFileAccess Mode:=xlReadOnly 
    Kill WB.FullName 
    WB.Close SaveChanges:=False 

    'Restore screen updating and release Outlook 
    Application.ScreenUpdating = True 
    Set oMail = Nothing 
    Set oApp = Nothing 
End Sub 

上包含該行的常規站如下: WB.SaveAs FileName:="C:\" & FileName

我在想什麼?

+0

這可能是用戶帳戶控制。嘗試保存到當前工作簿的路徑,而不是測試。 – Rory

回答

0

默認情況下,Windows用戶無權寫入C:\根目錄。

解決方案:

  • 使用在用戶具有寫權限
  • 更改用戶權限,以獲得在C:\(不推薦)寫訪問的子目錄