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

Ste*_*eve 6 excel vba excel-vba

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

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strPath & strFName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Run Code Online (Sandbox Code Playgroud)

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

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

Thu*_*ame 6

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

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

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

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


0m3*_*m3r 1

我看你的代码没有问题,你用的是哪个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
Run Code Online (Sandbox Code Playgroud)

也不要使用 vbNewLine 尝试& "<br>" &