Converting Word Documents from .doc to .docx (Part 1)

by Nov 8, 2019

Converting old Word documents to the new .docx format can be a lot of work. Thanks to PowerShell, you can automate the conversion:

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

    process
    {
        # determine target path
        $pathOut = [System.IO.Path]::ChangeExtension($Path, '.docx')
        
        # open the document
        $doc = $word.Documents.Open($Path)

        Write-Progress -Activity 'Converting' -Status $PathOut
        
        # important: do the actual conversion
        $doc.Convert()

        # save in the appropriate format
        $doc.SaveAs([ref]$PathOut,[ref]16)

        # close document
        $word.ActiveDocument.Close()
    }
    end
    {
        # close word
        $word.Quit()
    }
}

Twitter This Tip! ReTweet this Tip!