convert all doc old to new docx

i saw a code in the end of the thread http://www.experts-exchange.com/questions/28714741/I-need-a-tool-to-convert-MS-Word-MS-Excel-files-to-new-2013-version.html#a40982186

which is also attached below,  i added microsoft word to the VBA reference

i run the code and it stops If Documents.Count > 0 Then in yellow debugger. although the selected folder has word documents.

grateful if someone can fix this for me.

Sub SaveAllAsDOCX()
Dim strFilename As String
Dim strDocName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Dim intPos As Integer
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
    .Title = "Select folder and click OK"
    .AllowMultiSelect = False
    .InitialView = msoFileDialogViewList
    If .Show <> -1 Then
        MsgBox "Cancelled By User", , "List Folder Contents"
        Exit Sub
    End If
    strPath = fDialog.SelectedItems.Item(1)
    If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
    Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
    strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFilename = Dir$(strPath & "*.doc")
While Len(strFilename) <> 0
    Set oDoc = Documents.Open(strPath & strFilename)
    strDocName = ActiveDocument.FullName
    intPos = InStrRev(strDocName, ".")
    strDocName = Left(strDocName, intPos - 1)
    strDocName = strDocName & ".docx"
    oDoc.SaveAs Filename:=strDocName, _
        FileFormat:=wdFormatDocumentDefault
    oDoc.Close SaveChanges:=wdDoNotSaveChanges
    strFilename = Dir$()
Wend
End Sub

Open in new window

LVL 6
FloraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kulboyCommented:
this checks the amount of open documents.
If you have a doc open it will want it to close with the save changes popup.
0
FloraAuthor Commented:
kulboy, i do not understand what are u suggesting to me.
0
kulboyCommented:
I'm pointing out that this peace of code:
If Documents.Count > 0 Then
    Documents.Close SaveChanges:=wdPromptToSaveChanges
End If

is checking if there are open documents, it is not related to the amount of documents in the folder.
Can you give me a little more info on what the error is?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

FloraAuthor Commented:
here is the error

run-time error 429

ActiveX component can't create object
0
kulboyCommented:
this probably means word in not open?
0
GrahamSkanRetiredCommented:
The code seems to have been written as a Word macro, so put it in a code module of a Word template. Use the Normal template project unless you have good reason not to.

Meanwhile I'll try to adapt it to run in Excel.
0
FloraAuthor Commented:
thanks Grahamskan
0
GrahamSkanRetiredCommented:
This can be a macro in Excel (or any other Office application). It needs a reference to the Word Object library.
Option Explicit

Sub SaveAllAsDOCX()
    Dim strFilename As String
    Dim strDocName As String
    Dim strInPath As String
    Dim strOutPath As String
    Dim fDialog As FileDialog
    Dim intPos As Integer
    Dim wdApp As Word.Application
    Dim oDoc As Word.Document
    Dim bNewWordApp As Boolean
    Dim iCount As Integer
    
    'get input folder
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select folder with '.doc' documents"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , "List Folder Contents"
            Exit Sub
        End If
        strInPath = fDialog.SelectedItems.Item(1)
    End With
    
    'get output folder
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select output folder for .docx documents"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , "List Folder Contents"
            Exit Sub
        End If
        strOutPath = fDialog.SelectedItems.Item(1)
    End With
    
    'try to avoid multiple instances of the Word application,
    'so only create the Word application if Word is not running
        On Error Resume Next 'temporarily supress error checking
        Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0 'resume error checking
    If wdApp Is Nothing Then
        Set wdApp = CreateObject(Class:="Word.Application")
        bNewWordApp = True
    End If
    
    wdApp.Visible = True
    
    'loop through the files
    strFilename = Dir$(strInPath & "\*.doc")
    Do While Len(strFilename) > 0
        Set oDoc = wdApp.Documents.Open(strInPath & "\" & strFilename)
        strDocName = oDoc.Name & "x"
        oDoc.SaveAs Filename:=strOutPath & "\" & strDocName, _
            FileFormat:=wdFormatDocumentDefault
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
        iCount = iCount + 1
        strFilename = Dir$()
    Loop
    If bNewWordApp = True Then
        wdApp.Quit
    End If
    MsgBox iCount & " .doc documents from " & strInPath & " copied to " & strOutPath & " as .docx documents"
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FloraAuthor Commented:
many thanks GrahamSkan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.