[Solved] Excel VBA to convert all Word files in a specific folder to PDF [closed]


I’ve finally found the correct VBA I was looking for:

'In your VBA window go to tools then references and add a reference to 
'Microsoft Word

Sub Converter()
  Dim cnt As Integer, currfile As String
  Dim TrimFile As String, Path As String, FilesInPath As String _
, MyFiles() As String, Fnum As Long
  Dim CalcMode As Long, LPosition As Long
  Dim StartTime As Date, EndTime As Date

  Dim objWord As Word.Application
  Dim objDoc As Word.Document


  ThisWorkbook.Activate
  currfile = ActiveWorkbook.Name

  Windows(currfile).Activate
  Sheets("Sheet1").Activate

  StartTime = Timer
  Path = Range("C3").Text & "\"

  FilesInPath = Dir(Path & "*.doc*")
  If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
  End If

  Fnum = 0
  Do While FilesInPath <> ""
    Fnum = Fnum + 1
    ReDim Preserve MyFiles(1 To Fnum)
    MyFiles(Fnum) = FilesInPath
    FilesInPath = Dir()
  Loop

  With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
  End With

  If Fnum > 0 Then
    For Fnum = LBound(MyFiles) To UBound(MyFiles)
      Set objWord = CreateObject("Word.Application")
      'objWord.Visible = True

      On Error Resume Next

      Set objDoc = Word.Documents.Open(Path & MyFiles(Fnum))

      On Error GoTo 0

      If Not objDoc Is Nothing Then

        LPosition = InStr(1, objDoc.Name, ".") - 1
        TrimFile = Left(objDoc.Name, LPosition)

        On Error Resume Next

        objDoc.ExportAsFixedFormat OutputFileName:=objDoc.Path & "\" & TrimFile & ".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



      End If

      objDoc.Close

   Next Fnum
  End If


  With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = CalcMode
  End With

  objWord.Quit

  Set objDoc = Nothing
  Set objWord = Nothing

  EndTime = Timer
  MsgBox " Task succesfully completed in " & Format(EndTime - StartTime, "0.00") & " 
seconds"
End Sub

0

solved Excel VBA to convert all Word files in a specific folder to PDF [closed]