Pages

Thursday, February 18, 2021

Batch converting Microsoft Word .doc to .docx with VBA

I was recently asked to provide an way to batch process a set of Microsoft Word documents that are still saved in the older .doc format to the modern .docx format and I wasn’t able to find a script that did exactly what I wanted so ended up writing one for the client. Coding isn’t my forte given it is not something I commonly perform so I thought I’d write this blog post for anyone who may be looking for something similar as well as have it for myself to reference in the future.

The following demonstrates how to open .doc documents in a specified folder and save them as .docx.

1. Begin by launching Word, click on the Developer tab and click on Visual Basic

image

2. Click on the Insert tab and then Module:

image

3. Paste the following code into the module:

Sub UpdateOldDocToDocx()

    Application.ScreenUpdating = False

    'Disable privacy settings warning

    Application.DisplayAlerts = False

    Dim oldDocument As Document

    Dim strPassword As String, strFile As String, strFolder, strDestFolder As String

    Dim passwordRow As Integer

    strFolder = "C:\Word\"

    strFile = Dir(strFolder & "*.doc", vbNormal)

    strDestFolder = "C:\Word\Upgraded\"

    ' BEGIN LOOPING THROUGH OLD WORD DOCUMENTS

    While strFile <> ""

      Set oldDocument = Documents.Open(FileName:=strFolder & strFile)

      'OPEN WORD, SAVE AND CLOSE

      With oldDocument

        .SaveAs2 FileName:=strDestFolder & strFile & "x", FileFormat:=wdFormatXMLDocument, CompatibilityMode:=15

        .Close

      End With

      strFile = Dir()

      Wend

End Sub

6. Update the variables to desired values:

strFolder: Where the source files are (ensure that the trailing "\" is in the path)

strDestFolder: Where the destination password protected files should be

(ensure that the trailing "\" is in the path)

7. Proceed to run the module by clicking on the play button or F5:

image

8. The upgraded files should be present in the defined directory:

imageimage

No comments:

Post a Comment