View Single Post
 
Old 09-27-2019, 08:36 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,179
gmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of light
Default

This relates to the Windows filing system, however it is possible by collecting the filenames, sorting them and then processing the sorted list e.g.

Code:
Sub ConvertHTMLtoDOC()
'Graham Mayor - https://www.gmayor.com - Last updated - 28 Sep 2019
Dim strFile As String
Dim strPath As String
Dim strDocName As String
Dim oDoc As Document
Dim oColl As Collection
Dim arr() As Variant
Dim i As Integer
    Set oColl = New Collection
    strPath = "C:\path\"
    strFile = Dir(strPath & "*.htm*")
    Do While strFile <> ""
        oColl.Add strPath & strFile
        strFile = Dir()
    Loop
    arr = toArray(oColl)
    For i = LBound(arr) To UBound(arr)
        Set oDoc = Documents.Open(arr(i))
        strDocName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".docx"
        oDoc.SaveAs2 FileName:=strDocName, FileFormat:=wdFormatDocumentDefault
        oDoc.Close
    Next i
    Set oDoc = Nothing
End Sub

Private Function toArray(ByVal Coll As Collection) As Variant
Dim arr() As Variant
Dim i As Long
    ReDim arr(1 To Coll.Count) As Variant
    For i = 1 To Coll.Count
        arr(i) = Coll(i)
    Next
    toArray = arr
    QuickSort arr
lbl_Exit:
    Exit Function
End Function

Private Sub QuickSort(vArray As Variant, _
                      Optional lng_Low As Long, _
                      Optional lng_High As Long)
Dim vPivot As Variant
Dim vTmp_Swap As Variant
Dim tmp_Low As Long
Dim tmp_High As Long

    If lng_High = 0 Then
        lng_Low = LBound(vArray)
        lng_High = UBound(vArray)
    End If

    tmp_Low = lng_Low
    tmp_High = lng_High
    vPivot = vArray((lng_Low + lng_High) \ 2)
    While (tmp_Low <= tmp_High)
        While (vArray(tmp_Low) < vPivot And tmp_Low < lng_High)
            tmp_Low = tmp_Low + 1
        Wend
        While (vPivot < vArray(tmp_High) And tmp_High > lng_Low)
            tmp_High = tmp_High - 1
        Wend
        If (tmp_Low <= tmp_High) Then
            vTmp_Swap = vArray(tmp_Low)
            vArray(tmp_Low) = vArray(tmp_High)
            vArray(tmp_High) = vTmp_Swap
            tmp_Low = tmp_Low + 1
            tmp_High = tmp_High - 1
        End If
    Wend
    If (lng_Low < tmp_High) Then QuickSort vArray, lng_Low, tmp_High
    If (tmp_Low < lng_High) Then QuickSort vArray, tmp_Low, lng_High
lbl_Exit:
    Exit Sub
End Sub
__________________
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