End Sub
Private Sub Setup()
Cells.Clear
Let Range("A1") = "Path"
Let Range("B1") = "Size (KB)"
Let Range("D1") = "PDF Path"
Let Range("E1") = "PDF Size (KB)"
Let Range("E:E").Font.Color = xlNone
Let Range("B:B", "E:E").NumberFormat = "0.0"
With Range("A1:E1")
Let .Interior.Color = RGB(102, 153, 255)
Let .Borders.LineStyle = xlContinuous
End With
End Sub
Private Sub SelectFilesToConvert()
Dim i As Long
Dim r As Range
Set r = Range("A2")
With Application.FileDialog(msoFileDialogOpen)
Let .AllowMultiSelect = True
Let .InitialFileName = "initial path" Let .InitialView = msoFileDialogViewList
.Filters.Clear
.Filters.Add "Word Documents", "*.docx"
.Show
' Create hyperlinks to the files and show their size in KB
For i = 1 To .SelectedItems.Count
r.Worksheet.Hyperlinks.Add Anchor:=r, Address:=.SelectedItems(i), TextToDisplay:=.SelectedItems(i)
r.Offset(0, 1) = FileLen(r) / 1000
' Open each Word file
OpenWordFile CStr(r)
Set r = r.Offset(1, 0)
Next i
End With
End Sub
Private Sub OpenWordFile(filePath As String)
On Error GoTo ErrCleanUp
Dim wordApp As Word.Application
Set wordApp = New Word.Application
Let wordApp.DisplayAlerts = wdAlertsNone
Let wordApp.Visible = False
Dim wordDoc As Document
Set wordDoc = wordApp.Documents.Open(filePath)
SaveAsMinimizedPDF wordDoc
Let wordDoc.Saved = True
wordDoc.Close
wordApp.Quit
Exit Sub
ErrCleanUp:
Let wordDoc.Saved = True
wordDoc.Close
wordApp.Quit
End Sub
Private Sub SaveAsMinimizedPDF(ByRef doc As Document)
doc.ExportAsFixedFormat OutputFileName:= _
Split(doc.FullName, ".")(0) & ".pdf", ExportFormat:=wdExportFormatPDF _
, OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForOnScreen, Range _
:=wdExportAllDocument, From:=1, to:=1, Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=False, BitmapMissingFonts:= _
False, UseISO19005_1:=False
End Sub
Private Sub UpdateConverted()
Dim i As Long
Dim r As Range
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
Set r = Range("A" & i)
r.Offset(0, 3).Worksheet.Hyperlinks.Add _
Anchor:=r.Offset(0, 3), Address:=Split(r, ".")(0) & ".pdf", _
TextToDisplay:=Split(r, ".")(0) & ".pdf"
r.Offset(0, 4) = FileLen(r.Offset(0, 3)) / 1000
' validate
r.Offset(0, 4).Font.Color = IIf(r.Offset(0, 4) > 100, RGB(255, 0, 0), RGB(0, 255, 0))
Next i
End Sub