Wednesday, June 24, 2026

VBA Word - Convert Multiple HTML pages into Markdown, MD files

Run the following VBA Excel program. The program opens chat.html file from source folder in default browser. Then it selects all its content and paste into a text file. The text file is named the folder name with file extension .md and saved into the parent folder. This process is repeated for all subfolders of the parent folder.


Private Sub ConvertChatHtmlToMarkdown_NoWarnings(sourceFolder As String)
    Dim fso As Object
    Dim chatFilePath As String
    Dim parentFolder As String
    Dim folderName As String
    Dim markdownFilePath As String
    Dim htmlContent As String
    Dim ts As Object
    Dim shellCommand As String

    ' Ensure sourceFolder ends without trailing backslash
    If Right(sourceFolder, 1) = "\" Then
        sourceFolder = Left(sourceFolder, Len(sourceFolder) - 1)
    End If

    chatFilePath = sourceFolder & "\chat.html"

    ' Check if chat.html exists
    If Dir(chatFilePath) = "" Then
        Debug.Print "chat.html not found in: " & sourceFolder
        Exit Sub
    End If

    ' Open chat.html in default browser silently without FollowHyperlink
    shellCommand = "cmd /c start """" """ & chatFilePath & """"
    Shell shellCommand, vbHide

    ' Create FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")

    ' Read HTML content
    Set ts = fso.OpenTextFile(chatFilePath, 1, False, -2)
    htmlContent = ts.ReadAll
    ts.Close

    ' Get folder and parent folder names
    folderName = fso.GetFolder(sourceFolder).Name
    parentFolder = fso.GetFolder(sourceFolder).parentFolder.Path
    markdownFilePath = parentFolder & "\" & folderName & ".md"

    ' Write to .md file
    Set ts = fso.CreateTextFile(markdownFilePath, True, True)
    ts.Write htmlContent
    ts.Close

    Debug.Print "Converted: " & folderName & " -> " & markdownFilePath
End Sub

Sub ProcessAllChats()
    Dim fso As Object
    Dim sourceFolderPath As String
    Dim sourceFolder As Object
    Dim subfolder As Object

    Set fso = CreateObject("Scripting.FileSystemObject")

    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Root Folder Containing Chat Folders"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub ' User cancelled
        sourceFolderPath = .SelectedItems.Item(1)
    End With

    Set sourceFolder = fso.GetFolder(sourceFolderPath)

    For Each subfolder In sourceFolder.SubFolders
        ConvertChatHtmlToMarkdown_NoWarnings subfolder.Path
    Next

    MsgBox "All chat.html files processed.", vbInformation
End Sub

No comments:

Post a Comment

Hot Topics