reduce pdf file size / compress pdf file size through Macro VBA

prati

New Member
Joined
Jan 25, 2021
Messages
48
Office Version
  1. 2019
Platform
  1. Windows
Hey,

I wonder if there is a way to reduce pdf file size through Macro VBA.
I don't have Adobe Acrobat Professional, so I need a free way to achieve this goal.
I know two software's that can reduce pdf file size for free.

First software - Free PDF Compressor

Below are the files of the program - the program has main dll files gsdll32.dll and gswin32c that make the compression

1629108901798.png


Below is the main window of the program

1629108975843.png




Second software - Compress PDF - Reduce pdf size easily - Free PDF Compressor

Below are the files of the program - the program has several dll files

1629109144834.png


Below is the main window of the program

1629109119224.png


The question is how can I use one to the above programs through VBA In order to reduce pdf file size and not through the graphical / user-friendly interface.

Any alternative free way to reduce pdf file size only through VBA plus a free software would be perfect as well
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,125
The 4dots compressor can be called from the command line:

In VBA use the Shell function or WScript.Shell object to run the command line string.
 

prati

New Member
Joined
Jan 25, 2021
Messages
48
Office Version
  1. 2019
Platform
  1. Windows
The 4dots compressor can be called from the command line:

In VBA use the Shell function or WScript.Shell object to run the command line string.
Thanks

I created the macro below but nothing happened it seems i failed to run it from command

Public Sub CompressPdf_4dots()

Dim Wsh As Object 'WshShell

Set Wsh = CreateObject("WScript.Shell") 'New WshShell

Wsh.Run ("cmd /c 4dotsFreePDFCompress " & "C:\Temp\Example.pdf"""), 0, True

End Sub

Of course I put a pdf file named Example at C:\Temp
and it's the only file in the folder
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,125
Looks like insufficient quotes. Please use VBA code tags -
VBA Code:
Wsh.Run "cmd /c 4dotsFreePDFCompress.exe " & """C:\Temp\Example.pdf""", 0, True
You might need the full path to the .exe if it isn't in the Path environment variable.
 
Solution

prati

New Member
Joined
Jan 25, 2021
Messages
48
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Looks like insufficient quotes. Please use VBA code tags -
VBA Code:
Wsh.Run "cmd /c 4dotsFreePDFCompress.exe " & """C:\Temp\Example.pdf""", 0, True
You might need the full path to the .exe if it isn't in the Path environment variable.
Hey
I use the full path
Wsh.Run "cmd /c\PdfPrograms\4dotsFreePDFCompress.exe " & """C:\Temp""", 0, True

The code just load all the files located in C:\Temp....

1629139893143.png


I thought that the code can work "behind the scenes" like PDFtk server......In another words, I thought It could compress all the files without showing the graphical interface.
 

prati

New Member
Joined
Jan 25, 2021
Messages
48
Office Version
  1. 2019
Platform
  1. Windows
Hey
I use the full path
Wsh.Run "cmd /c\PdfPrograms\4dotsFreePDFCompress.exe " & """C:\Temp""", 0, True

The code just load all the files located in C:\Temp....

View attachment 44974

I thought that the code can work "behind the scenes" like PDFtk server......In another words, I thought It could compress all the files without showing the graphical interface.
I changed the command as follows

Wsh.Run "cmd /c\PdfPrograms\4dotsFreePDFCompress.exe " & """C:\Temp""" & " " & ", 0, True

Now it doesn't load the program
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,125

ADVERTISEMENT

Try the command from a command prompt, as shown in the examples.
 

prati

New Member
Joined
Jan 25, 2021
Messages
48
Office Version
  1. 2019
Platform
  1. Windows
Looks like insufficient quotes. Please use VBA code tags -
VBA Code:
Wsh.Run "cmd /c 4dotsFreePDFCompress.exe " & """C:\Temp\Example.pdf""", 0, True
You might need the full path to the .exe if it isn't in the Path environment variable.
Hey,
As usual you are a Genius.
Since it is not in the enviroment i replaced the word cmd with the full path. Now it work's


Public Sub Compress_Pdf_4dots()
Dim s as String
Dim Wsh As Object 'WshShell
Set Wsh = CreateObject("WScript.Shell") 'New WshShell

s = "c:\PdfPrograms\4dotsFreePDFCompress.exe " & "C:\Temp" & " /overwrite"
Wsh.Run s, 0, True
End Sub

Could you explain what the 0 does?
What the True does?

I just copy the Wsh.Run s, 0, True in every macro without knowing what it actully does.

Anyway - it works perfect - compress all the files in C:\Temp folder and also overwrite the existing files.
 

prati

New Member
Joined
Jan 25, 2021
Messages
48
Office Version
  1. 2019
Platform
  1. Windows
The VBA code works great, however 4dots pdf compressor sometimes destroy the pdf files, sometimes fill the page with black background.
I should search for alternative way to compress pdf through vba.

The code itself indeed works great.
The compressor/the application is bad. Even if the pdf size become smaller, the downside is that several pages in the pdf files have an error.
I should search for alternative command line solution
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,151,842
Messages
5,766,738
Members
425,376
Latest member
Bradleyckx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top