运行时1004错误保存到C盘

时间:2017-04-20 07:28:14

标签: excel vba excel-vba

我有一个Excel工作簿,里面有几个工作表。每个工作表都是我想单独发送电子邮件的文档。

我在运行Office 2007的Windows XP SP3计算机上安装了工作簿.VBA代码运行正常,我可以通过电子邮件发送每个工作表。

我需要在Windows 7或Windows 10 PC上再次使用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 = "testuser@test.com"
        '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

我错过了什么?

1 个答案:

答案 0 :(得分:0)

默认情况下,Windows用户无权在C:\根目录下写入。

解决方案:

  • 使用用户有权写入的子目录
  • 更改用户权限以获取C:\(不推荐)
  • 的写访问权限