【问题标题】:Saving as PDF, under year and month folder在年月文件夹下另存为 PDF
【发布时间】:2018-05-02 15:48:32
【问题描述】:

我发现此代码效果很好,但我需要将 1 张工作簿另存为 pdf。将文件格式更改为xltypepdf 时,我不断收到错误消息。有人可以帮忙吗?

Sub DateFolderSave() 
Application.DisplayAlerts = False 
' Check for year folder and create if needed If 
Len(Dir("C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date), 
vbDirectory)) = 0 Then 
MkDir "C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) 
End If 
' Check for month folder and create if needed If 
Len(Dir("C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" & 
MonthName(Month(Date), False), vbDirectory)) = 0 Then MkDir 
"C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" & 
MonthName(Month(Date), False)
 End If 
' Check for date folder and create if needed If 
Len(Dir("C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" & 
MonthName(Month(Date), False) & "\" & Format(Date, "dd.mm.yy"), 
vbDirectory)) = 0 Then 
MkDir "C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" & 
MonthName(Month(Date), False) & "\" & Format(Date, "dd.mm.yy") 
End If
 ' Save File 
ActiveWorkbook.SaveAs Filename:= _ "C:\Users\Christine\Desktop\Learner Lists 
LC\" & Year(Date) & "\" & MonthName(Month(Date), False) & "\" & Format(Date, 
"dd.mm.yy") & ".xlsm", _ FileFormat:=xlOpenXMLWorkbookMacroEnabled, 
CreateBackup:=False 
Application.DisplayAlerts = True 
' Popup Message MsgBox "File Saved As:" &   vbNewLine & 
C:\Users\Desktop\Learner Lists LC\" & Year(Date) & _ "\" & 
MonthName(Month(Date), False) & "\" & Format(Date, "mm.dd.yy") & ".xlsm"
End Sub

【问题讨论】:

  • 您不必在工作簿上SaveAs 来获取 PDF 文件,您必须在工作表上ExportAsFixedFormat Type:=xlTypePDF 才能做到这一点
  • 提示:将代码复制到问题中时,只需直接从 VBE 复制/粘贴到问题中即可。这样您就不会出现使代码无法运行的无效换行符。
  • 是的,很抱歉。先发到手机上然后塞进去
  • @YowE3K -- 我很好奇为什么有时你会“放弃”一个非常好的几乎答案作为评论而不是答案?很难不猛扑进去,重新输入你说的话并得分! (我想我可能已经不经意地从你那里剪掉了一些观点......)我认为你要么是在提示 OP 填写空白并回答他们自己的问题,要么你害怕达到 20,000 个代表会让你“老”还是什么? :) 在你的答案坐在那里和 54% 的“路径”代码之间(但谁在数呢),这真的很难控制自己。
  • @ashleedawg 通常是因为我在睡觉前看问题,我不想再熬夜查看文档(我自己从未真正使用过ExportAsFixedFormat ,所以在我发布答案之前需要阅读它)但我可以提供一个快速评论,这可能会给 OP 提供足够的线索,让他们自己做。但是,如果您从我这里看到任何此类 cmets,如果您将其充实为完整的答案,我会非常高兴。之后联系我,我会支持你的答案(如果我不完全同意,我会提出修复建议)。

标签: vba excel pdf directory


【解决方案1】:

我做的第一件事(在弄清楚代码开始和结束的位置之后)是搜索和替换,使用 1 个变量和 1 个常量(而不是重复 8 次相同的文件夹名称)。

我的侦探技能告诉我,您是 VBA 的新手:使用常量和变量是第一课,因为它们是“所有编码和开发”的基础。

但不用担心,每个人都必须从某个地方开始。我重写了你的子程序:

Option Explicit

Sub ExportSheetToPDF()
'exports ActiveWorksheet to dated PDF

    'Set openPDFwhenDone to TRUE to auto-open the PDF when finished
    Const openPDFwhenDone = True

    'Set constant basePath to the file save path
    Const basePath = "C:\Users\Christine\Desktop\Learner Lists LC\"

    'Declare variable pdfPath which for the complete path & filename
    Dim pdfPath As String

    'get the name of the "year" folder
    pdfPath = basePath & Year(Date)

    'if the "year" folder doesn't exist then create it
    If Dir(pdfPath, vbDirectory) = "" Then MkDir pdfPath

    'get the name of the "month" folder
    pdfPath = pdfPath & "\" & Format(Month(Date), "00")

    'if the "month" folder doesn't exist then create it
    If Dir(pdfPath, vbDirectory) = "" Then MkDir pdfPath

    'get the complete pdf filename
    pdfPath = pdfPath & "\" & Format(Date, "yyyy-mm-dd") & ".pdf"

    'export active worksheet as PDF to pdfPath
    ActiveSheet.ExportAsFixedFormat xlTypePDF, pdfPath,,,,,, openPDFwhenDone

    'make sure the pdf file was created
    If Dir(pdfPath) = "" Then
        'file not found
        MsgBox "Something went wrong. (PDF wasn't created.)"
    Else
        'Success!  Show success message (unless PDF was set to auto-open)
        If Not openPDFwhenDoneThen MsgBox "File Saved As:" & vbLf & pdfPath
    End If

End Sub

在我开始挑选之前,我认为“所有这些代码”所做的不仅仅是找出文件名和导出!请注意您的代码的哪些部分被变量pdfPath 替换。大小可能无关紧要,但效率和易读性才是重要的!

我在上面的代码中添加了很多 cmets,以便更容易理解,但作为示例,下面的代码几乎相同,只是进一步压缩。实际上创建PDF文件只是一行代码。

Sub ExportSheetToPDF_smaller()
    Const basePath = "C:\Users\Christine\Desktop\Learner Lists LC\"
    If Dir(basePath & Year(Date), vbDirectory) = "" Then MkDir basePath & Year(Date)
    If Dir(basePath & Format(Date, "yyyy\\mm"), vbDirectory) = "" Then MkDir basePath & Format(Date, "yyyy\\mm")
    ActiveSheet.ExportAsFixedFormat xlTypePDF, Format(Date, "yyyy\\mm\\yyyy-mm-dd.p\df"), , , , , , True
    MsgBox "File Saved As:" & vbLf & Dir(basePath & Format(Date, "yyyy\\mm\\yyyy-mm-dd.p\df"))
End Sub

因此,作为免费代码的回报,您可以获得家庭作业。 说真的。请花一些时间浏览下面的链接,将这些命令与上面的代码示例进行比较,因为它们涵盖了使用的每个命令,并且是一个很好的起点。还有很多其他资源(甚至 YouTube 也是一个隐藏的教程库)...

VBA 作业:

如果您在导出 PDF 时遇到任何问题,请告诉我。

祝你好运!

【讨论】:

  • 我希望我能为每个“家庭作业”投票一次! :D
  • 非常感谢您的帮助。是的,很新,还只是迈出了第一步。感谢您的指导。将检查链接。
  • 不客气。请这样做,它并不像看起来那么令人生畏,请记住,Google 是程序员最好的朋友!
猜你喜欢
  • 2019-07-01
  • 2021-06-04
  • 2013-07-23
  • 2019-05-12
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多