Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-10-2022, 04:14 AM
Franci Franci is offline Converting .doc to .docx Windows 10 Converting .doc to .docx Office 2016
Novice
Converting .doc to .docx
 
Join Date: May 2016
Posts: 12
Franci is on a distinguished road
Default Converting .doc to .docx

Hi!




I need to convert multiple .doc files to .docx.


I found this code on

How to batch convert .doc format files to .docx in Word?




Code:
Sub ConvertDocToDocx()
'Updated by ExtendOffice 20181128
    Dim xDlg As FileDialog
    Dim xFolder As Variant
    Dim xFileName As String
    Application.ScreenUpdating = False
    Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
    If xDlg.Show <> -1 Then Exit Sub
    xFolder = xDlg.SelectedItems(1) + "\"
    xFileName = Dir(xFolder & "*.doc", vbNormal)
    While xFileName <> ""
        Documents.Open FileName:=xFolder & xFileName, _
            ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
            PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
            WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
            wdOpenFormatAuto, XMLTransform:=""
        ActiveDocument.SaveAs xFolder & Replace(xFileName, "doc", "docx"), wdFormatDocumentDefault
        ActiveDocument.Close
        xFileName = Dir()
    Wend
    Application.ScreenUpdating = True
End Sub


It does convert .doc files to .docx, but the problem is it does not stop when all .doc files in a folder are converted. Instead, it keeps running and converting .docx to .docxx and .docxx to .docxxx...


I'd like to know why this is happening and what is wrong with the code.
Reply With Quote
  #2  
Old 02-10-2022, 05:12 AM
gmayor's Avatar
gmayor gmayor is offline Converting .doc to .docx Windows 10 Converting .doc to .docx Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

This type of process needs a touch of error handling e.g.
Code:
Sub ConvertDocToDocx()
Dim oDlg As FileDialog
Dim sFolder As String
Dim sName As String, sFullName As String
Dim oDoc As Document
Dim i As Integer: i = 0
    Application.ScreenUpdating = False
    Set oDlg = Application.FileDialog(msoFileDialogFolderPicker)
    If oDlg.Show <> -1 Then Exit Sub
    sFolder = oDlg.SelectedItems(1) + "\"
    sName = Dir(sFolder & "*.doc", vbNormal)
    While sName <> ""
        If Right(LCase(sName), 3) = "doc" Then
            i = i + 1
            Set oDoc = Documents.Open(FileName:=sFolder & sName, _
                                      ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
                                      PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
                                      WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
                                      wdOpenFormatAuto, XMLTransform:="")
            sFullName = Left(oDoc.FullName, InStrRev(oDoc.FullName, Chr(46)))
            If ActiveDocument.HasVBProject = True Then
                ActiveDocument.SaveAs sFullName & "docm", wdFormatXMLDocumentMacroEnabled
            Else
                ActiveDocument.SaveAs sFullName & "docx", wdFormatXMLDocument
            End If
            ActiveDocument.Close
        End If
        sName = Dir()
    Wend
    Application.ScreenUpdating = True
    If i = 1 Then
        MsgBox i & " document converted"
    Else
        MsgBox i & " documents converted"
    End If
    Set oDoc = Nothing
    Set oDlg = Nothing
End Sub
Or you could use Document Batch Processes which has even more error handling and avoids the overwriting of existing files with the same new names.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-10-2022, 05:45 AM
Franci Franci is offline Converting .doc to .docx Windows 10 Converting .doc to .docx Office 2016
Novice
Converting .doc to .docx
 
Join Date: May 2016
Posts: 12
Franci is on a distinguished road
Default

Thank you, your code works fine.
Reply With Quote
Reply

Tags
convert doc to docx



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting DOCX files to plain text preserving (hanging) indents jsampson45 Word 1 01-14-2022 11:20 PM
Merge a .docx into a table in existing .docx MarkAtTheBeach Word Tables 3 10-26-2018 12:55 PM
Converting .doc to .docx converting docx files to office xp jimdevo17 Word 1 01-14-2011 08:17 PM
Converting .doc to .docx .docx ding dong Word 3 11-01-2009 12:43 PM
Converting a Docx file to a Dictionary file geoffatmhg Word 0 02-08-2009 04:00 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft