需求

  • 把同个目录下的word文档都转换成pdf文档
  • 保存到同个目录下的同名文件

代码如下

  • Win10 + Office 2019 测试通过

    Sub Sava2PDF()
    '定义对话框变量
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    Dim BaseFileName As String
    Dim FileNameArray() As String
    
    With fd
          If .Show = -1 Then
              '定义单个文件变量
              Dim vrtSelectedItem As Variant
               
              '定义循环变量
              Dim i As Integer
              i = 1
               
              '开始文件检索
              For Each vrtSelectedItem In .SelectedItems
    
                  '打开要转换为PDF的word文件
                  Dim tempDC As Document
                  Set tempDC = Documents.Open(vrtSelectedItem)
                  FileNameArray = Split(tempDC.Name, ".")
                  BaseFileName = FileNameArray(0)
                  'MsgBox (BaseFileName)
                  
                  tempDC.ExportAsFixedFormat OutputFileName:= _
                  tempDC.Path + "\" + BaseFileName + ".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
                  tempDC.Close
          
              Next vrtSelectedItem
          End If
      End With
       
      Set fd = Nothing
    
    
    
    End Sub

成果

标签: word, office, vba

评论已关闭