错误运行时错误'1004'未保存文档. [英] Error Run-Time Error '1004' Document not saved.

查看:432
本文介绍了错误运行时错误'1004'未保存文档.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功运行了一个宏,该宏将Excel工作表另存为PDF并通过电子邮件发送给我的执行团队.

I have been successfully running a macro which saves my Excel sheet as a PDF and emails my Executive team.

我通过创建新工作表来重新设计它,并相应地更新了代码.

I redesigned it, by creating a new sheet, and updated the code accordingly.

Sub NewDashboardPDF()

' New Executive Daily Dashboard Macro
'
' Create and email the Executive TEAM the Daily Dashboard.
    Dim strPath As String, strFName As String
    Dim OutApp As Object, OutMail As Object

' Create and email the Daily Report to Mitch/Dave/John/Jason ALL PAGES.
    Sheets("Executive Dashboard").Select
    strPath = Environ$("temp") & "\" 'Or any other path, but include trailing "\"
    strFName = Worksheets("Executive Dashboard").Range("V2").Value & " " & Format(Date, "yyyymmdd") & ".pdf"

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        strPath & strFName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

     'Set up outlook
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
     'Create message
    On Error Resume Next
    With OutMail
        .to = xxx@testemail.com
        .CC = "steve@testemail.com"
        .BCC = ""
        .Subject = "Daily Dashboard"
        .Body = "All, " & vbNewLine & vbNewLine & _
                        "Please see the attached daily dashboard." & vbNewLine & _
                        "If you have any questions or concerns, please do not hesitate to contact me." & vbNewLine & _
                        "Steve"
        .Attachments.Add strPath & strFName
        .Display
        .Send
    End With
     'Delete any temp files created
    Kill strPath & strFName
    On Error GoTo 0

End Sub

我收到的错误消息是未保存运行时错误'1004'文档.该文档可能已打开或遇到错误.

The error message I get is Run-Time Error '1004' Document not saved. The document may be open or an error may have been encountered.

我调试时,以下部分在最后一行上用箭头突出显示.

When I debug, the following section is highlighted with the arrow on the last line.

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strPath & strFName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

所有对旧表的引用已更新为新表,因此我认为这不是问题.

All references to the old sheet were updated to the new one so I do not believe that is the issue.

另一方面,我想知道如何创建包含我的默认电子邮件签名的电子邮件.目前,它只是格式化为纯文本电子邮件.

On another note, I would love to know how to create this email with my default email signature included. Currently it is just formatted as a plain text email.

推荐答案

Document not saved错误消息是表明该PDF文件不可写的线索,可能是因为该文件已在您的PDF阅读器中打开.如果在尝试从VBA保存文档时打开了PDF文档,我可以重复该错误.

The Document not saved error message is the clue that the PDF file is not writable, probably because it is open in your PDF reader. I can repeat the error if I have the PDF document open while trying to save the document from VBA.

如果您没有打开文档,则Windows可能会无意中锁定了文件.您可能需要重新启动PC才能清除锁定.

If you don't have the document open, there is a chance that Windows has inadvertently left a lock on the file. You may need to restart your PC to clear the lock.

如果文件尚不存在,则需要确认您可以在目录位置中实际创建文件.

If the file doesn't yet exist, then you'll need to confirm that you can actually create files in the directory location.

如果V2中的值包含最终使文件名无效的字符,例如\/:*?",您将遇到类似的错误. ,<>|.

You will encounter a similar error if the value in V2 contains characters that ultimately make the filename invalid, such as \, /, :, *, ?, ", <, > or |.

这篇关于错误运行时错误'1004'未保存文档.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆