错误运行时错误'1004'文档未保存。

我已经成功地运行了一个macros,它将我的Excel工作表保存为PDF,并通过电子邮件发送给我的执行团队。

我重新devise了它,通过创build一个新工作表,并相应地更新了代码。

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'文档未保存。 文档可能已打开,或者可能遇到错误。

当我debugging时,最后一行中的箭头将突出显示以下部分。

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

所有对旧表的引用都被更新为新的表,所以我不相信这是问题。

在另一个说明,我很想知道如何创build这个电子邮件与我的默认电子邮件签名包括在内。 目前它只是格式化为纯文本电子邮件。

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

如果您没有打开文档,则Windows有可能无意中在文件上留下了锁。 您可能需要重新启动电脑才能清除locking。

如果文件还不存在,那么您需要确认您可以在目录位置创build文件。

如果V2中的值包含最终导致文件名无效的字符(如\/:* ,?),则会遇到类似的错误?"<>|

我发现你的代码没有问题,你正在使用哪个Excel Office?

添加你的默认签名试试这个

  Dim Signature As String With OutMail .Display End With Signature = OutMail.HTMLBody 'Create message On Error Resume Next With OutMail .To = "xxx@testemail.com" .CC = "steve@testemail.com" .BCC = "" .Subject = "Daily Dashboard" .HTMLBody = "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" & vbNewLine & Signature .Attachments.Add strPath & strFName .Display ' .Send End With 

而不是使用vbNewLine尝试& "<br>" &