我已經成功地運行了一個宏,它將我的Excel工作表保存爲PDF並通過電子郵件發送給我的執行團隊。錯誤運行時錯誤'1004'文檔未保存。
我重新設計了它,通過創建一個新工作表並相應地更新了代碼。
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 = [email protected]
.CC = "[email protected]"
.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? – 0m3r
'strPath'和'strFName'在崩潰時的值是什麼?他們是你所期望的嗎? – YowE3K
這種情況下,即使非常基本的源代碼控制可以幫助你。如果你有一個關於你的代碼是什麼時候的工作的記錄,什麼時候它沒有,你可以看到什麼是不同的,而不是猜測。如果你的機器上沒有scc,你可以使用github在線或gist來給你一些變化。 –