Converting Word Documents

by Mar 8, 2021

There are still floating around numerous Microsoft Office documents in old file formats (.doc instead of .docx).

Here is a simple PowerShell function that takes an old .doc Word document, converts it and saves the converted document in .docx format. Provided the old Word document isn’t locked, this process is completely invisible and can run unattended:

function Convert-Doc2Docx
{
  param
  (
    [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
    [string]
    [Alias('FullName')]
    $Path,
    
    [string]
    $DestinationFolder
    
  )
  begin
  {
    $word = New-Object -ComObject Word.Application
  }

  process
  {

    
    $pathOut = [System.IO.Path]::ChangeExtension($Path, '.docx')
    if ($PSBoundParameters.ContainsKey('DestinationFolder'))
    {
      $exists = Test-Path -Path $DestinationFolder -PathType Container
      if (!$exists)
      {
        throw "Folder not found: $DestinationFolder"
      }
      $name = Split-Path -Path $pathOut -Leaf
      $pathOut = Join-Path -Path $DestinationFolder -ChildPath $name
    }
    $doc = $word.Documents.Open($Path)
    $name = Split-Path -Path $Path -Leaf
    Write-Progress -Activity 'Converting' -Status $name
    $doc.Convert()
    $doc.SaveAs([ref]([string]$PathOut),[ref]16)
    $word.ActiveDocument.Close()

  }
  end
  {
    $word.Quit()
  }
} 


Twitter This Tip! ReTweet this Tip!