【发布时间】:2016-09-19 00:39:32
【问题描述】:
我已经成功运行了一个宏,它将我的 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
我收到的错误消息是运行时错误“1004”文档未保存。文档可能已打开或遇到错误。
当我调试时,下面的部分用最后一行的箭头突出显示。
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strPath & strFName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
所有对旧工作表的引用都已更新为新工作表,所以我认为这不是问题。
另外,我很想知道如何使用我的默认电子邮件签名创建此电子邮件。目前它只是格式化为纯文本电子邮件。
【问题讨论】:
-
我看不出代码有什么问题,你换过 Excel Office 了吗?
-
崩溃时
strPath和strFName的值是多少?他们是你所期待的吗? -
在这种情况下,即使是非常基本的源代码控制也会对您有所帮助。如果你记录了你的代码在什么时候工作和什么时候不工作,你可以确切地看到有什么不同,而不是猜测。如果你的机器上没有 scc,你可以使用 github 在线或 gist 给你一个变化的差异。