vba 错误运行时错误“1004”文档未保存。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39563933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error Run-Time Error '1004' Document not saved.
提问by Steve
I have been successfully running a macro which saves my Excel sheet as a PDF and emails my Executive team.
我已经成功运行了一个宏,它将我的 Excel 工作表保存为 PDF 并通过电子邮件发送给我的执行团队。
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 = [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
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.
我收到的错误消息是运行时错误“1004”文档未保存。文档可能已打开或可能遇到错误。
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.
另一方面,我很想知道如何使用我的默认电子邮件签名创建此电子邮件。目前它只是格式化为纯文本电子邮件。
回答by ThunderFrame
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.
该Document not saved
错误消息是线索的PDF文件不可写,可能是因为它是在你的PDF阅读器打开。如果在尝试从 VBA 保存文档时打开了 PDF 文档,我可以重复该错误。
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.
如果您没有打开该文档,则 Windows 可能无意中在该文件上留下了锁。您可能需要重新启动 PC 才能清除锁定。
If the file doesn't yet exist, then you'll need to confirm that you can actually create files in the directory location.
如果该文件尚不存在,则您需要确认您实际上可以在目录位置创建文件。
You will encounter a similar error if the value in V2
contains characters that ultimately make the filename invalid, such as \
, /
, :
, *
, ?
, "
, <
, >
or |
.
你会遇到类似的错误如果值V2
包含的字符,最终使文件名无效,如\
,/
,:
,*
,?
,"
,<
,>
或|
。
回答by 0m3r
I see nothing wrong with your code, which Excel Office are you using?
我看不出你的代码有什么问题,你使用的是哪个 Excel Office?
To add your default signature try this:
要添加您的默认签名,请尝试以下操作:
Dim Signature As String
With OutMail
.Display
End With
Signature = OutMail.HTMLBody
'Create message
On Error Resume Next
With OutMail
.To = "[email protected]"
.CC = "[email protected]"
.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
Also instead of using vbNewLine try & "<br>" &
也不要使用 vbNewLine 尝试 & "<br>" &
回答by M Ryder
Make sure that you have created your folder structure into which the file will be saved. You can code your solution to check that the folders have been created and if not, it can created them. I have had a similar problem before. Check that the folder path used in the following code exists:
确保您已创建用于保存文件的文件夹结构。您可以编写解决方案以检查文件夹是否已创建,如果未创建,则可以创建它们。我以前也遇到过类似的问题。检查以下代码中使用的文件夹路径是否存在:
strPath = Environ$("temp") & "\"
strPath = Environ$("temp") & "\"
If it does not exist, create it and try again. You will probably find that it then works fine.
如果它不存在,请创建它并重试。您可能会发现它可以正常工作。
回答by user2216667
Check your path, take it out and see what it does or play around with it. I put a spreadsheet on a new machine and it failed when trying to place it in the user\documents folder
检查你的路径,把它拿出来看看它做了什么或玩弄它。我在一台新机器上放了一个电子表格,但在尝试将其放入 user\documents 文件夹时失败了