【问题标题】:there is a way to save a pdf file with a path folder name of the excel file+vba?有没有办法用excel文件+vba的路径文件夹名称保存pdf文件?
【发布时间】:2020-02-05 20:38:53
【问题描述】:

这是新的。我想做的是以下。我有一个保存在“X 文件夹”中的 excel 文件(总是不同),我必须从该文件中打印一个 pdf,我需要命名为“XFolder+VICPDFQuote”,我需要将此 pdf 保存在同一个文件夹中excel是。我有以下代码,但由于文件保存在不同的文件夹中,所以我遗漏了一些东西:


' Print_quote Macro

ActiveSheet.PageSetup.Orientation = xlPortrait
    Worksheets("Quote").PageSetup.PrintArea = "$H$6:$P$133"

        Sheets(Array("Cov", "Quote", "T&C VIC")).Select
    strFile = ThisWorkbook.Path & "\" & strFile


   ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
     ThisWorkbook.Path & "\" & FolderName & "VICPDFQuote", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True

 Sheets("Quote").Select

End Sub


【问题讨论】:

  • FolderName 是什么?
  • FolderName 应该反映 ThisWorkbook.Path,但作为有效文件名(没有 /\:?*" )?然后FolderName =Replace(Replace(ThisWorkbook.Path,"\","!"),":","!")using ! 作为分隔符-
  • 文件夹名称总是不同的。我在“apples 文件夹”中有一个 excel 文件名 pear 项目。 pdf文件需要命名为“Apples VICPDFQuote”
  • 所以只存储第一个父文件夹?

标签: excel vba


【解决方案1】:

要获取包含文件的文件夹名称,请使用 FileSystemObject

With CreateObject("Scripting.FileSystemObject")
    Dim NewFileName As String
    NewFileName = .GetFile(ThisWorkbook.FullName).ParentFolder.Name & " VICPDFQuote.pdf"
End With

【讨论】:

  • 嗨,谢谢,但它不起作用。将pdf命名为false
  • 显示结果和Workbook.FullName。您可以将代码中的FolderName替换为CreateObject("Scripting.FileSystemObject").GetFile(ThisWorkbook.FullName).ParentFolder.Name
  • 不客气!您错过了将我的新 variblNewFileName 分配给您的 pdf 导出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 2012-08-26
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多