Bulk-Convert Word .doc files to .docx

There are several reasons why bulk converting old Microsoft Office documents can be worthwhile. The new files are typically much smaller, present fewer security risks and are less likely to be blocked by default, either now or in the future.

When this macro is run, it will prompt for a FOLDER and will then convert all documents in that folder and all subfolders. For a large folder this may take some time. This code is for Microsoft Windows only.

Public Sub ConvertDocToDocx()
Dim FSO, objFolder, objSubfolder, objFile, queue As Collection
Dim fldr As FileDialog
Dim strFolder As String
Dim objWordApplication As New Word.Application
Dim objWordDocument As Word.Document
Dim strOldFileName As String

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = "C:\demo\"
    If .Show <> -1 Then Exit Sub
    strFolder = .SelectedItems(1)
End With
Set fldr = Nothing

Set FSO = CreateObject("Scripting.FileSystemObject")
' magic to loop through subfolders
Set queue = New Collection
queue.Add FSO.GetFolder(strFolder)

Do While queue.Count > 0
    Set objFolder = queue(1)
    queue.Remove 1 'dequeue
    '...insert any per-folder processing code here...
    For Each objSubfolder In objFolder.SubFolders
        queue.Add objSubfolder 'enqueue
    Next objSubfolder
    
    For Each objFile In objFolder.Files
        If LCase(Right(objFile, 3)) = "doc" Then
            strOldFileName = objFile
            'Open old file
            With objWordApplication
                Set objWordDocument = .Documents.Open(FileName:=strOldFileName, AddToRecentFiles:=False, ReadOnly:=False, Visible:=True)
                ' Save it
                With objWordDocument
                    ' Save as docx or docm, depending on whether the file contains macros.
                    ' and turn off compatibility mode
                    If .HasVBProject = False Then
                    .SaveAs2 FileName:=.FullName & "x", FileFormat:=wdFormatXMLDocument, CompatibilityMode:=15, AddToRecentFiles:=False
                    Else
                    .SaveAs2 FileName:=.FullName & "m", FileFormat:=wdFormatXMLDocumentMacroEnabled, CompatibilityMode:=15, AddToRecentFiles:=False
                    End If
                    'close the document
                    .Close False
                End With
            End With

        End If
    Next objFile
Loop

End Sub

Word CompatibilityMode Enumeration

Name Value Description
wdCurrent 65535 Compatibility mode equivalent to the latest version of Word.
wdWord2003 11 Word is put into a mode that is most compatible with Word 2003. Features new to Word are disabled in this mode.
wdWord2007 12 Word is put into a mode that is most compatible with Word 2007. Features new to Word are disabled in this mode.
wdWord2010 14 Word is put into a mode that is most compatible with Word 2010. Features new to Word are disabled in this mode.
wdWord2013 15 Default. All Word features are enabled.

Related

KB922850 - A file type was blocked by your registry policy settings.
Convert Excel .xls to .xlsx Excel Spreadsheet Macro.


 
Copyright © 1999-2025 windevcluster.com
Some rights reserved