【问题标题】:Saving and renaming multiple files as PDF without asking for location at the same time将多个文件保存和重命名为 PDF,而无需同时询问位置
【发布时间】:2018-01-31 15:32:34
【问题描述】:

我有三个单词的文档文件,我想用Now() 日期重命名它们。三个文件分别为

1. EMEA.doc --rename-> EMEA 083117.doc -convert-> EMEA 082317.PDF

2. CEEMEA.doc --rename-> CEEMEA 083117.doc -convert-> CEEMEA 082317.PDF

3. LATAM.doc --rename-> LATAM 083117.doc -convert-> LATAM 082317.PDF

我需要ExportAsFixedFormat(PDF) 这些.Doc 文件。以下代码仅适用于 ActiveDocument。我想将文件保存在特定位置而不需要 VBA 询问位置。

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    "C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\LATAM.pdf", ExportFormat _
    :=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
    wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
    Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
    CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
    BitmapMissingFonts:=True, UseISO19005_1:=False

【问题讨论】:

  • 我不确定我是否理解,您为什么不在代码中将它们一一打开并为所有它们做同样的事情?还是使用循环?
  • @vacip 打开和打印需要时间,我想保存它。由于有问题的文件只是示例,因此有很多文件需要重命名和转换。
  • 你为什么不做EMEA.doc -convert-> EMEA 082317.PDF
  • @jsotola 实际上我需要日期为EMEA 082317.docEMEA 082317.pdf 的两个文件,而没有日期EMEA.doc 的文件对我来说没用。

标签: vba pdf-generation ms-word file-rename export-to-pdf


【解决方案1】:

为三个文件创建UserForm,并将代码(如下所示)复制到PDF_Click

代码将一一打开这三个文件并使用Now() 日期执行SAVEAS 并使用For loop 为选中标记的文件创建PDF

Private Sub PDF_Click()

Dim d As String

Finalize.hide
Dim C As MSForms.Control
For Each C In Me.Controls
    If TypeName(C) = "CheckBox" Then
    If C.Value = True Then
    If C.Caption = "Select All" Then
    Else

VD = "C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\"

Documents.Open FileName:=VD & C.Caption & ".doc"

d = Format(Now(), "mmddyy")
f = VD & Ctl.Caption & " " & d
ActiveDocument.SaveAs2 f & ".doc"

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        f & ".pdf", ExportFormat _
        :=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, TO:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False

ActiveDocument.Close

    End If
    End If
    End If

Next
End Sub

此答案对批评者开放,以改进。

【讨论】:

    【解决方案2】:

    大方向:

    1. 您需要告诉程序您要操作哪些文件(例如“all word files in folder x”)
    2. 您似乎已经有了重命名规则(文件名&“”&日期),只需遍历所有有问题的文件即可; batch file 可能是 vba 重命名文件的一个很好的替代解决方案
    3. Open and save each document to PDF(后台即可,无需实际打开文档)

    注意:您从未真正“转换”过 word 文件,只保存了一个附加 (PDF) 文件。为了使它看起来像“转换”,您需要将 PDF 保存在同一位置并删除原始 word 文件。

    如果您在程序中遇到特定问题需要帮助,请使用相关代码行更新您的帖子,并说明错误/错误行为、所需行为以及您迄今为止尝试解决的问题。

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 2011-07-20
      • 2017-06-23
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      相关资源
      最近更新 更多