0

I need an script that searches in a folder for word documents (.rtf) and change the resolution of the images. The problem is that I have a lot of .rtf files that are taking a lot of space because they have high resolution images. If I change the resolution of the images the file reduces it space about 97%. Please help me.

Thank you.

PJramos
  • 1
  • 1
  • What do you mean by 'Resize'? By how much, with what tool? in what image format? You could record your steps in word, generate a macro, and add the macro code to your question as a much better reference point. – Cpt.Whale Aug 26 '21 at 15:57
  • If the rtf files are still being generated, this answer should greatly reduce the size of any new files as they're created. Word creates two copies of each image by default, but can be set to only use 1 via a registry setting: https://stackoverflow.com/a/2088172/7411885 – Cpt.Whale Aug 26 '21 at 16:00
  • It is a word document. I Select the image (logo of the company) in the header and go to Format > Compress Pictures > Select E-mail (96 ppi) and uncheck Apply only to this picture. – PJramos Aug 27 '21 at 16:15

1 Answers1

0

Unfortunately, there is no programmatic way to do "Select Image > Picture Format > Compress Pictures". It might be worth setting up an AutoHotKey script to run through your files.


If your rtf files were originally created in word, they likely saved two copies of each image (original file and huge uncompressed version). You can change this behavior by setting ExportPictureWithMetafile=0 in the registry, then re-saving each file. This can be done with a script, for example:

# Set registry key: (use the correct version number, mine is 16.0)
Try { Get-ItemProperty HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options\ -Name ExportPictureWithMetafile -ea Stop} 
Catch { New-ItemProperty HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options\ -Name ExportPictureWithMetafile -Value "0" | Out-Null }

# Get the list of files
$folder =  Get-ChildItem  "c:\temp\*.rtf" -File

# Set up save-as-filetype
$WdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' -Passthru
$RtfFormat  = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatRTF

# Start Word
$word = New-Object -ComObject word.application
$word.Visible = $False

ForEach ($rtf in $folder) {
  # save as new name temporarily (otherwise word skips the shrink process)
  $doc = $word.documents.open($rtf.FullName)
  $TempName=($rtf.Fullname).replace('.rtf','-temp.rtf')
  $doc.saveas($TempName, $RtfFormat)
  
  # check for success, then delete original file
  # re-save to original name
  # check for success again, then clean up temp file
  if (Test-Path $TempName) { Remove-Item $rtf.FullName }
  $doc.saveas($rtf.FullName, $RtfFormat)
  if (Test-Path $rtf.FullName) { Remove-Item $TempName }
  
  # close the document
  $doc.SaveAs()
  $doc.close()
}

$word.quit()

I made some default word files with a 2mb image, saved as rtf (without the registry change), and saw the rtf files were a ridiculous 19mb! I ran the script above, and it shrunk them down to 5mb.

Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16