0

I have over 900 MS Word files that I need to export the images from. I have exported a few files to HTML, but that is a slow/long process for 900+ files.

I found a Powershell script on here from a while back, but it isn't working. I have Windows 10 and Office 365 so I'm not sure if the script needs updated.

Here is the Powershell script convertdoc.ps1

param([string]$docpath,[string]$htmlpath = $docpath)

$srcfiles = Get-ChildItem $docPath -filter "*.doc"
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
$word = new-object -comobject word.application
$word.Visible = $False

function saveas-filteredhtml
    {
        $opendoc = $word.documents.open($doc.FullName);
        $opendoc.saveas([ref]"$htmlpath\$doc.fullname.html", [ref]$saveFormat);
        $opendoc.close();
    }

ForEach ($doc in $srcfiles)
    {
        Write-Host "Processing :" $doc.FullName
        saveas-filteredhtml
        $doc = $null
    }

$word.quit();

I'm calling with the following from a command prompt:

powershell -ExecutionPolicy RemoteSigned -File "c:\convertdoc.ps1" "c:\1" "c:\1-output"

Is there a better method, or what? The files are in 60 or so folders, so ideally I could point to the top folder and it would create the output folder and sub folders and recursively export.

2
  • I would do this on VBA. Iterate documents by FileSystemObject, open, and iterate by its images saving them separately with the name of document plus image num in a doc.
    – Akina
    Aug 15, 2018 at 5:57
  • 2
    Maybe consider the approach here: gallery.technet.microsoft.com/office/… i.e. copy Word doc to a temp zip, extract images to \word\media\ and move them to a destination. Aug 15, 2018 at 7:19

1 Answer 1

1

The question regarding your current code is unclear though Powershell is a helpful tool for this task.

As the comments suggest, if your files are .docx (not .doc), you can rename the file extensions to .zip. When you open the zip, you'll find a folder of images that are used within the document. We can use Powershell to rename the documents to .zip, extract the each zip, then get all of the image files.

The below solution involves creating/deleting temp directories, which is not ideal, but is a nice simple solution. Be sure to make a backup of your files before using this. The -WhatIf parameter is included as a safety measure, in case someone incorrectly uses this code. Remove this at your discretion.

# put your documents here
$documents = "C:\documents\"
# your images will be stored here
$images = "C:\images\"

Set-Location $documents

# rename all docx files to zip files, then extract the zips to directories
Get-ChildItem $documents *.docx | % { 
    Rename-Item $_ ($_.BaseName + ".zip")
    Expand-Archive ($_.BaseName + ".zip")
}

# get the images from the directories, then delete each directory
Get-ChildItem -Directory | ForEach-Object {
    Copy-Item "$documents$_\word\media\*" $images
    Remove-Item $documents$_ -Recurse -WhatIf
}

# restore the docx files
Get-ChildItem $documents *.zip | % { 
    Rename-Item $_ ($_.Basename + ".docx")
}
1
  • I ended up creating a quick and dirty batch file to pretty much do what you suggested for the first batch of files. I'll try your method on the next.
    – Dizzy49
    Aug 15, 2018 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .