Wednesday, June 24, 2026

VBA Word - Convert each Markdown (MD file into HTML

Install Pandoc (free, command-line tool) to run this VBA code to convert each Markdown (MD file into HTML.
Sub ConvertAllMdToHtml()
    Dim fso As Object
    Dim folderPath As String
    Dim file As Object
    Dim mdFilePath As String
    Dim htmlFilePath As String
    Dim command As String

    Set fso = CreateObject("Scripting.FileSystemObject")

    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Folder Containing .md Files"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub
        folderPath = .SelectedItems.Item(1)
    End With

    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

    For Each file In fso.GetFolder(folderPath).Files
        If LCase(fso.GetExtensionName(file.Name)) = "md" Then
            mdFilePath = file.Path
            htmlFilePath = fso.GetParentFolderName(mdFilePath) & "\" & fso.GetBaseName(mdFilePath) & ".html"
            ' Build Pandoc command
            command = "cmd /c pandoc """ & mdFilePath & """ -f markdown -t html -s -o """ & htmlFilePath & """"
            Shell command, vbHide
        End If
    Next

    MsgBox "All .md files converted to HTML!", vbInformation
End Sub

No comments:

Post a Comment

Hot Topics