XL 2019 Figer une colonne en HTML venant d'un fichier Excel

  • Initiateur de la discussion Initiateur de la discussion Titof06
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

Titof06

XLDnaute Occasionnel
Bonjour,

Je reviens vers vous pour un autre problème, en tous cas un pour moi.

Je génère 2 fichiers "htm".
La macro sur trouve dans le module "Affichage_HTML"

Un pour un suivi sur 9 jours et l'autre pour un suivi sur 30 jours.
Celui de 9 jours entre parfaitement dans mon écran.
Par contre celui de 30 jours, bien sur, déborde sur l'écran.

J'aimerai, si cela est possible, figer la colonne de gauche (A sur le fichier Excel) mais dans la page "htm", afin de garder la visibilté des chauffeurs sur la gauche.

Je vous remercie et vous souahite une agréable journée.

Titof06
 

Pièces jointes

Solution
Bonjour,
dans Sub Genere_Fichier_HTML(), remplacer le code de publication des 30 jours glissants par :
VB:
'   30 JOURS GLISSANTS - OK le 10/04/2026
    Htm30 = rep & "\CALENDRIER LIVRAISON_30 Jours Glissants.htm"
    ActiveSheet.Range("A1:AE22").Select
    With ActiveWorkbook.PublishObjects.Add(xlSourceRange, Htm30, _
        "Calendrier Livraison (2)", "A1:AE22", xlHtmlStatic, _
        "TEST_Calendrier Livraison_19002", "")
        .Publish (True)
        .AutoRepublish = False
        '.AutoRepublish = True
    End With
    F = FreeFile
    Open Htm30 For Input As #F
        Data = Replace(Input$(LOF(F), #F), "<!--table", "<!--table" & vbLf & "td:nth-child(1) {position:fixed;width:50pt;}" & vbLf)
    Close #F
    Open Htm30 For...
Attention comme je le dis dans la video le mode stiky c'est vraiment figé la colone avec scroll mais ca enleve le style des td c'est pour ca que dans la video je mobtre bien que le style background doit rester inline et ca l'export MS ne le fait

allez Kado
met ça dans un module appelé supergenialpatricklemeilleur(😄😁😂🤣 LOL
VB:
'*****************************************************************************************************
'    ___     _     _______  __      _   ____  _   _  _______  ___     _   _   _    ___     _     _.
'   //  \\  /\\      //    // \\   //  //    //  //    //    //  \\  //  //  //   //  \\  //|   //
'  //___// //__\    //    //__//  //  //    //__//    //    //   // //  //  //   //   // // |  //
' //      //   \\  //    //  \\  //  //    //  \\    //    //   // //  //  //   //   // //  | //
'//      //    // //    //   // //  //___ //    \\  //     \\__// //__//  //___ \\__// //   |//
'****************************************************************************************************
'               Collection Utilitaires VBA /HTM Range to html simplifié pour titof06
'Auteur patricktoulon
' cette fonction est issue et adaptée du module VBAhtml_GENERATOR de patricktoulon

Function CreatehtmlTable(rng As Range, Optional GridLine As Boolean = False, Optional CssOutLine As Boolean = False) As String
    Dim dhtml As Object, dico As Object, Table, TbodY, cel, lig, col, crit, TR, TD, bw, bws, bwt, bwts, bwr, bwrs, bbwr, bbwrs, classe, css, elem, IdX, w
    
If CssOutLine Then Set dico = CreateObject("Scripting.Dictionary")

Set dhtml = CreateObject("htmlfile")
    dhtml.body.innerHTML = "<table><TBODY></TBODY></table>"
    Set Table = dhtml.getElementsByTagName("table")(0)
    Set TbodY = dhtml.getElementsByTagName("TBODY")(0)
    
    For lig = 1 To rng.Rows.Count
        If Application.WorksheetFunction.CountA(rng.Cells(lig, 1).Resize(1, rng.Columns.Count)) > 0 Then
            Set TR = TbodY.appendChild(dhtml.createElement("TR"))
            For col = 1 To rng.Columns.Count
                Set cel = rng.Cells(lig, col).MergeArea
                IdX = cel.Address(0, 0)
                If dhtml.getElementById(IdX) Is Nothing Then
                    Set TD = TR.appendChild(dhtml.createElement("TD"))
                    TD.setAttribute "id", IdX
                    TD.colSpan = cel.Columns.Count
                    TD.rowSpan = cel.Rows.Count
                    If cel.Cells(1).Value <> "" Then
                        With cel.Cells(1): crit = IsNull(.Font.Color) Or IsNull(.Font.Bold) Or IsNull(.Font.Italic): End With
                        If crit Then
                            TD.innerHTML = htmltexte(cel.Cells(1))
                        Else
                            TD.innerHTML = cel.Cells(1).Text
                        End If
                        
                    End If
                    With TD.Style
                        If lig = 2 Then w = w + Round((cel.width + 1) / 0.75)
                        
                        .width = Round((cel.width + 1) / 0.75) & "px"
                        .Height = Round((cel.Height + 1) / 0.75) & "px"
                        .backgroundColor = coul_XL_to_coul_HTMLX(cel.DisplayFormat.Interior.Color)
                        If Not IsNull(cel.Cells(1).Font.Color) And cel.Cells(1).Font.Color <> vbBlack Then .Color = coul_XL_to_coul_HTMLX(cel.Cells(1).Font.Color)
                        Set bw = cel.Borders(xlEdgeLeft)
                        If cel.Borders(xlEdgeLeft).LineStyle <> xlNone Then
                            bws = bw.LineStyle
                            .borderLeftWidth = Switch(bw.Weight = xlThick, "3px", bw.Weight = xlMedium, "2px", bw.Weight = xlThin, "1px", bw.Weight = xlHairline, "0.5pt")
                            .borderLeftColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeLeft).Color)
                            .borderLeftStyle = Switch(bws = xlContinuous, "solid", bws = xlDash, "dashed", bws = xlDot, "dotted", bws = xlDashDot, "dashed", bws = xlDashDotDot, "dotted", bws = xlDouble, "double", bws = xlSlantDashDot, "dashed")
                        Else
                            If GridLine Then
                                .borderLeftWidth = "0.5pt"
                                .borderLeftColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderLeftStyle = "solid"
                            End If
                        End If
                        
                        Set bwt = cel.Borders(xlEdgeTop)
                        If cel.Borders(xlEdgeTop).LineStyle <> xlNone Then
                            bwts = bwt.LineStyle
                            .borderTopWidth = Switch(bwt.Weight = xlThick, "3px", bwt.Weight = xlMedium, "2px", bwt.Weight = xlThin, "1px", bwt.Weight = xlHairline, "0.5pt")
                            .borderTopColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeTop).Color)
                            .borderTopStyle = Switch(bwts = xlContinuous, "solid", bwts = xlDash, "dashed", bwts = xlDot, "dotted", bwts = xlDashDot, "dashed", bwts = xlDashDotDot, "dotted", bwts = xlDouble, "double", bwts = xlSlantDashDot, "dashed")
                            .textAlign = "center"
                        Else
                            If GridLine Then
                                .borderTopWidth = "0.5pt"
                                .borderTopColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderTopStyle = "solid"
                            End If
                        End If
                        
                        If col = rng.Columns.Count Then
                            Set bwr = cel.Borders(xlEdgeRight)
                            If cel.Borders(xlEdgeRight).LineStyle <> xlNone Then
                                bwrs = bwr.LineStyle
                                .borderRightWidth = Switch(bwr.Weight = xlThick, "3px", bwr.Weight = xlMedium, "2px", bwr.Weight = xlThin, "1px", bwr.Weight = xlHairline, "0.5pt")
                                .borderRightColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeRight).Color)
                                .borderRightStyle = Switch(bwrs = xlContinuous, "solid", bwrs = xlDash, "dashed", bwrs = xlDot, "dotted", bwrs = xlDashDot, "dashed", bwrs = xlDashDotDot, "dotted", bwrs = xlDouble, "double", bwrs = xlSlantDashDot, "dashed")
                                
                            Else
                                If GridLine Then
                                End If
                                .borderRightWidth = "0.5pt"
                                .borderRightColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderRightStyle = "solid"
                            End If
                            
                        End If
                        
                        If lig = rng.Rows.Count Then
                            Set bbwr = cel.Borders(xlEdgeBottom)
                            If cel.Borders(xlEdgeBottom).LineStyle <> xlNone Then
                                bbwrs = bbwr.LineStyle
                                .borderBottomWidth = Switch(bbwr.Weight = xlThick, "3px", bbwr.Weight = xlMedium, "2px", bbwr.Weight = xlThin, "1px", bbwr.Weight = xlHairline, "0.5pt")
                                .borderBottomColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeBottom).Color)
                                .borderBottomStyle = Switch(bbwrs = xlContinuous, "solid", bbwrs = xlDash, "dashed", bbwrs = xlDot, "dotted", bbwrs = xlDashDot, "dashed", bbwrs = xlDashDotDot, "dotted", bbwrs = xlDouble, "double", bbwrs = xlSlantDashDot, "dashed")
                            Else
                                If GridLine Then
                                    .borderBottomWidth = "0.5pt"
                                    .borderBottomColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                    .borderBottomStyle = "solid"
                                End If
                                
                                
                            End If
                        End If
                    End With
                End If
                'on sort le CSS du htmlSI cSSiNLINE+FALSE
                If CssOutLine Then
                     If Not dico.exists("{" & TD.Style.cssText & ";}") Then
                        classe = "cla" & dico.Count
                        dico("{" & TD.Style.cssText & ";}") = classe
                        TD.className = classe
                    Else
                        TD.className = dico("{" & TD.Style.cssText & ";}")
                    End If
                    TD.removeAttribute ("style")
                End If
            Next
        End If
    Next
    If CssOutLine Then
        css = "<style>" & vbCrLf
        For Each elem In dico
            css = css & vbCrLf & "." & dico(elem) & elem & vbCrLf
        Next
        '----------------------------------------
        'le css pour figer la colonne  mode "fixed"pour les premier TD de chaque TR
        'css = css & "td:first-child {  position: fixed;  left: 0;  width: 83px; z-index: 10;border-right :2px double red;}/* IMPORTANT = largeur réelle */  " & vbCrLf
        'css = css & "table {  margin-left: " & Round(rng.Cells(1).width / 0.75) & "px;}" & vbCrLf
        '----------------------------------------
        'le css pour figer la colonne  mode "fixed"pour les premier TD de chaque TR
      css = css & "tr td:first-child,tr th:first-child {  position: sticky;  left: 0;  z-index: 5;border-rigth:2px double red;}" & vbCrLf
        
        Set TRs = dhtml.getElementsByTagName("TR")
       Dim i&
       For i = 0 To TRs.Length - 1
       TRs(i).FirstChild.Style.backgroundColor = coul_XL_to_coul_HTMLX(rng.Cells(i + 1, 1).DisplayFormat.Interior.Color)
       TRs(i).FirstChild.Style.borderRight = "3px double red"
       Next
        
        
        css = css & "</style>" & vbCrLf
    End If
    
    With Table.Style
        .borderCollapse = "collapse"
        .width = Int(w) + 1 & "px"
    End With
    
    CreatehtmlTable = "<!DOCTYPE HTML><head>" & css & "</head><body>" & Table.outerHTML & "</body></html>"
End Function

Function coul_XL_to_coul_HTMLX(couleur)
    'collection fonction perso
    'fonction Color XL to HTMLCOLOR ---> By Patricktoulon (2016)
    Dim str0 As String, strf As String
    str0 = Right("000000" & Hex(couleur), 6): strf = Right(str0, 2) & Mid(str0, 3, 2) & left(str0, 2)
    coul_XL_to_coul_HTMLX = "#" & strf & ""
End Function

Function htmltexte(cel As Range)
    'Fonction de récupération du code html du texte formaté dans la cellule
    'Patricktoulon (2016)
    Dim cde$, elem, Dc As New HTMLDocument
    cde = Replace(Replace(Replace(cel.Value(11), "ss:", ""), "Data", "Div"), "html:", "")
    cde = Replace(cde, " ", vbCrLf)
    With Dc
        .body.innerHTML = cde
        Debug.Print .body.innerHTML
        For Each elem In .all
            If elem.getAttribute("size") <> "" Then elem.Style.FontSize = elem.getAttribute("size") & "pt"
            elem.removeAttribute ("size")
        Next
        htmltexte = .getElementsByTagName("Div")(0).innerHTML
    End With
End Function
Et voici comment je fais pour l'appeler
Code:
Sub test()
    Dim CodeHtmL$, htmlFicH, x&
    CodeHtmL = CreatehtmlTable(Sheets("Calendrier Livraison").Range("A1:AE22"), False, True)
    htmlFicH = ThisWorkbook.Path & "\mapage.html"
    x = FreeFile
    Open htmlFicH For Output As #x: Print #x, CodeHtmL: Close #x
    DoEvents
    Shell "cmd /c start msedge """ & htmlFicH & """", vbHide
End Sub

Patrick
noyeux joel
 
Attention comme je le dis dans la video le mode stiky c'est vraiment figé la colone avec scroll mais ca enleve le style des td c'est pour ca que dans la video je mobtre bien que le style background doit rester inline et ca l'export MS ne le fait

allez Kado
met ça dans un module appelé supergenialpatricklemeilleur(😄😁😂🤣 LOL
VB:
'*****************************************************************************************************
'    ___     _     _______  __      _   ____  _   _  _______  ___     _   _   _    ___     _     _.
'   //  \\  /\\      //    // \\   //  //    //  //    //    //  \\  //  //  //   //  \\  //|   //
'  //___// //__\    //    //__//  //  //    //__//    //    //   // //  //  //   //   // // |  //
' //      //   \\  //    //  \\  //  //    //  \\    //    //   // //  //  //   //   // //  | //
'//      //    // //    //   // //  //___ //    \\  //     \\__// //__//  //___ \\__// //   |//
'****************************************************************************************************
'               Collection Utilitaires VBA /HTM Range to html simplifié pour titof06
'Auteur patricktoulon
' cette fonction est issue et adaptée du module VBAhtml_GENERATOR de patricktoulon

Function CreatehtmlTable(rng As Range, Optional GridLine As Boolean = False, Optional CssOutLine As Boolean = False) As String
    Dim dhtml As Object, dico As Object, Table, TbodY, cel, lig, col, crit, TR, TD, bw, bws, bwt, bwts, bwr, bwrs, bbwr, bbwrs, classe, css, elem, IdX, w
   
If CssOutLine Then Set dico = CreateObject("Scripting.Dictionary")

Set dhtml = CreateObject("htmlfile")
    dhtml.body.innerHTML = "<table><TBODY></TBODY></table>"
    Set Table = dhtml.getElementsByTagName("table")(0)
    Set TbodY = dhtml.getElementsByTagName("TBODY")(0)
   
    For lig = 1 To rng.Rows.Count
        If Application.WorksheetFunction.CountA(rng.Cells(lig, 1).Resize(1, rng.Columns.Count)) > 0 Then
            Set TR = TbodY.appendChild(dhtml.createElement("TR"))
            For col = 1 To rng.Columns.Count
                Set cel = rng.Cells(lig, col).MergeArea
                IdX = cel.Address(0, 0)
                If dhtml.getElementById(IdX) Is Nothing Then
                    Set TD = TR.appendChild(dhtml.createElement("TD"))
                    TD.setAttribute "id", IdX
                    TD.colSpan = cel.Columns.Count
                    TD.rowSpan = cel.Rows.Count
                    If cel.Cells(1).Value <> "" Then
                        With cel.Cells(1): crit = IsNull(.Font.Color) Or IsNull(.Font.Bold) Or IsNull(.Font.Italic): End With
                        If crit Then
                            TD.innerHTML = htmltexte(cel.Cells(1))
                        Else
                            TD.innerHTML = cel.Cells(1).Text
                        End If
                       
                    End If
                    With TD.Style
                        If lig = 2 Then w = w + Round((cel.width + 1) / 0.75)
                       
                        .width = Round((cel.width + 1) / 0.75) & "px"
                        .Height = Round((cel.Height + 1) / 0.75) & "px"
                        .backgroundColor = coul_XL_to_coul_HTMLX(cel.DisplayFormat.Interior.Color)
                        If Not IsNull(cel.Cells(1).Font.Color) And cel.Cells(1).Font.Color <> vbBlack Then .Color = coul_XL_to_coul_HTMLX(cel.Cells(1).Font.Color)
                        Set bw = cel.Borders(xlEdgeLeft)
                        If cel.Borders(xlEdgeLeft).LineStyle <> xlNone Then
                            bws = bw.LineStyle
                            .borderLeftWidth = Switch(bw.Weight = xlThick, "3px", bw.Weight = xlMedium, "2px", bw.Weight = xlThin, "1px", bw.Weight = xlHairline, "0.5pt")
                            .borderLeftColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeLeft).Color)
                            .borderLeftStyle = Switch(bws = xlContinuous, "solid", bws = xlDash, "dashed", bws = xlDot, "dotted", bws = xlDashDot, "dashed", bws = xlDashDotDot, "dotted", bws = xlDouble, "double", bws = xlSlantDashDot, "dashed")
                        Else
                            If GridLine Then
                                .borderLeftWidth = "0.5pt"
                                .borderLeftColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderLeftStyle = "solid"
                            End If
                        End If
                       
                        Set bwt = cel.Borders(xlEdgeTop)
                        If cel.Borders(xlEdgeTop).LineStyle <> xlNone Then
                            bwts = bwt.LineStyle
                            .borderTopWidth = Switch(bwt.Weight = xlThick, "3px", bwt.Weight = xlMedium, "2px", bwt.Weight = xlThin, "1px", bwt.Weight = xlHairline, "0.5pt")
                            .borderTopColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeTop).Color)
                            .borderTopStyle = Switch(bwts = xlContinuous, "solid", bwts = xlDash, "dashed", bwts = xlDot, "dotted", bwts = xlDashDot, "dashed", bwts = xlDashDotDot, "dotted", bwts = xlDouble, "double", bwts = xlSlantDashDot, "dashed")
                            .textAlign = "center"
                        Else
                            If GridLine Then
                                .borderTopWidth = "0.5pt"
                                .borderTopColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderTopStyle = "solid"
                            End If
                        End If
                       
                        If col = rng.Columns.Count Then
                            Set bwr = cel.Borders(xlEdgeRight)
                            If cel.Borders(xlEdgeRight).LineStyle <> xlNone Then
                                bwrs = bwr.LineStyle
                                .borderRightWidth = Switch(bwr.Weight = xlThick, "3px", bwr.Weight = xlMedium, "2px", bwr.Weight = xlThin, "1px", bwr.Weight = xlHairline, "0.5pt")
                                .borderRightColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeRight).Color)
                                .borderRightStyle = Switch(bwrs = xlContinuous, "solid", bwrs = xlDash, "dashed", bwrs = xlDot, "dotted", bwrs = xlDashDot, "dashed", bwrs = xlDashDotDot, "dotted", bwrs = xlDouble, "double", bwrs = xlSlantDashDot, "dashed")
                               
                            Else
                                If GridLine Then
                                End If
                                .borderRightWidth = "0.5pt"
                                .borderRightColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderRightStyle = "solid"
                            End If
                           
                        End If
                       
                        If lig = rng.Rows.Count Then
                            Set bbwr = cel.Borders(xlEdgeBottom)
                            If cel.Borders(xlEdgeBottom).LineStyle <> xlNone Then
                                bbwrs = bbwr.LineStyle
                                .borderBottomWidth = Switch(bbwr.Weight = xlThick, "3px", bbwr.Weight = xlMedium, "2px", bbwr.Weight = xlThin, "1px", bbwr.Weight = xlHairline, "0.5pt")
                                .borderBottomColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeBottom).Color)
                                .borderBottomStyle = Switch(bbwrs = xlContinuous, "solid", bbwrs = xlDash, "dashed", bbwrs = xlDot, "dotted", bbwrs = xlDashDot, "dashed", bbwrs = xlDashDotDot, "dotted", bbwrs = xlDouble, "double", bbwrs = xlSlantDashDot, "dashed")
                            Else
                                If GridLine Then
                                    .borderBottomWidth = "0.5pt"
                                    .borderBottomColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                    .borderBottomStyle = "solid"
                                End If
                               
                               
                            End If
                        End If
                    End With
                End If
                'on sort le CSS du htmlSI cSSiNLINE+FALSE
                If CssOutLine Then
                     If Not dico.exists("{" & TD.Style.cssText & ";}") Then
                        classe = "cla" & dico.Count
                        dico("{" & TD.Style.cssText & ";}") = classe
                        TD.className = classe
                    Else
                        TD.className = dico("{" & TD.Style.cssText & ";}")
                    End If
                    TD.removeAttribute ("style")
                End If
            Next
        End If
    Next
    If CssOutLine Then
        css = "<style>" & vbCrLf
        For Each elem In dico
            css = css & vbCrLf & "." & dico(elem) & elem & vbCrLf
        Next
        '----------------------------------------
        'le css pour figer la colonne  mode "fixed"pour les premier TD de chaque TR
        'css = css & "td:first-child {  position: fixed;  left: 0;  width: 83px; z-index: 10;border-right :2px double red;}/* IMPORTANT = largeur réelle */  " & vbCrLf
        'css = css & "table {  margin-left: " & Round(rng.Cells(1).width / 0.75) & "px;}" & vbCrLf
        '----------------------------------------
        'le css pour figer la colonne  mode "fixed"pour les premier TD de chaque TR
      css = css & "tr td:first-child,tr th:first-child {  position: sticky;  left: 0;  z-index: 5;border-rigth:2px double red;}" & vbCrLf
       
        Set TRs = dhtml.getElementsByTagName("TR")
       Dim i&
       For i = 0 To TRs.Length - 1
       TRs(i).FirstChild.Style.backgroundColor = coul_XL_to_coul_HTMLX(rng.Cells(i + 1, 1).DisplayFormat.Interior.Color)
       TRs(i).FirstChild.Style.borderRight = "3px double red"
       Next
       
       
        css = css & "</style>" & vbCrLf
    End If
   
    With Table.Style
        .borderCollapse = "collapse"
        .width = Int(w) + 1 & "px"
    End With
   
    CreatehtmlTable = "<!DOCTYPE HTML><head>" & css & "</head><body>" & Table.outerHTML & "</body></html>"
End Function

Function coul_XL_to_coul_HTMLX(couleur)
    'collection fonction perso
    'fonction Color XL to HTMLCOLOR ---> By Patricktoulon (2016)
    Dim str0 As String, strf As String
    str0 = Right("000000" & Hex(couleur), 6): strf = Right(str0, 2) & Mid(str0, 3, 2) & left(str0, 2)
    coul_XL_to_coul_HTMLX = "#" & strf & ""
End Function

Function htmltexte(cel As Range)
    'Fonction de récupération du code html du texte formaté dans la cellule
    'Patricktoulon (2016)
    Dim cde$, elem, Dc As New HTMLDocument
    cde = Replace(Replace(Replace(cel.Value(11), "ss:", ""), "Data", "Div"), "html:", "")
    cde = Replace(cde, " ", vbCrLf)
    With Dc
        .body.innerHTML = cde
        Debug.Print .body.innerHTML
        For Each elem In .all
            If elem.getAttribute("size") <> "" Then elem.Style.FontSize = elem.getAttribute("size") & "pt"
            elem.removeAttribute ("size")
        Next
        htmltexte = .getElementsByTagName("Div")(0).innerHTML
    End With
End Function
Et voici comment je fais pour l'appeler
Code:
Sub test()
    Dim CodeHtmL$, htmlFicH, x&
    CodeHtmL = CreatehtmlTable(Sheets("Calendrier Livraison").Range("A1:AE22"), False, True)
    htmlFicH = ThisWorkbook.Path & "\mapage.html"
    x = FreeFile
    Open htmlFicH For Output As #x: Print #x, CodeHtmL: Close #x
    DoEvents
    Shell "cmd /c start msedge """ & htmlFicH & """", vbHide
End Sub

Patrick
noyeux joel

Bonjour patricktoulon,

Oui, je vais le mettre avec "PATRICK EST LE MEILLEUR" 👍 !

Merci pour ce beau cadeau de Noël, bien en avance 🎁
Merci également pour les explications dans le code.

Je ne sais pas si votre pseudo vient que vous résidez à Toulon, mais j'ai des amis à St Cyr Les Lecques où j'y vais souvent l'été pour les voir.

Pour ma part je suis Cannois d'origine et vivant à Nice...tout le monde n'est pas parfait.

Encore Mille Mercis à vous pour le temps passé à m'aider 👌

Je vous souhaite une agréable journée, 😊

Titof06
 
Attention comme je le dis dans la video le mode stiky c'est vraiment figé la colone avec scroll mais ca enleve le style des td c'est pour ca que dans la video je mobtre bien que le style background doit rester inline et ca l'export MS ne le fait

allez Kado
met ça dans un module appelé supergenialpatricklemeilleur(😄😁😂🤣 LOL
VB:
'*****************************************************************************************************
'    ___     _     _______  __      _   ____  _   _  _______  ___     _   _   _    ___     _     _.
'   //  \\  /\\      //    // \\   //  //    //  //    //    //  \\  //  //  //   //  \\  //|   //
'  //___// //__\    //    //__//  //  //    //__//    //    //   // //  //  //   //   // // |  //
' //      //   \\  //    //  \\  //  //    //  \\    //    //   // //  //  //   //   // //  | //
'//      //    // //    //   // //  //___ //    \\  //     \\__// //__//  //___ \\__// //   |//
'****************************************************************************************************
'               Collection Utilitaires VBA /HTM Range to html simplifié pour titof06
'Auteur patricktoulon
' cette fonction est issue et adaptée du module VBAhtml_GENERATOR de patricktoulon

Function CreatehtmlTable(rng As Range, Optional GridLine As Boolean = False, Optional CssOutLine As Boolean = False) As String
    Dim dhtml As Object, dico As Object, Table, TbodY, cel, lig, col, crit, TR, TD, bw, bws, bwt, bwts, bwr, bwrs, bbwr, bbwrs, classe, css, elem, IdX, w
    
If CssOutLine Then Set dico = CreateObject("Scripting.Dictionary")

Set dhtml = CreateObject("htmlfile")
    dhtml.body.innerHTML = "<table><TBODY></TBODY></table>"
    Set Table = dhtml.getElementsByTagName("table")(0)
    Set TbodY = dhtml.getElementsByTagName("TBODY")(0)
    
    For lig = 1 To rng.Rows.Count
        If Application.WorksheetFunction.CountA(rng.Cells(lig, 1).Resize(1, rng.Columns.Count)) > 0 Then
            Set TR = TbodY.appendChild(dhtml.createElement("TR"))
            For col = 1 To rng.Columns.Count
                Set cel = rng.Cells(lig, col).MergeArea
                IdX = cel.Address(0, 0)
                If dhtml.getElementById(IdX) Is Nothing Then
                    Set TD = TR.appendChild(dhtml.createElement("TD"))
                    TD.setAttribute "id", IdX
                    TD.colSpan = cel.Columns.Count
                    TD.rowSpan = cel.Rows.Count
                    If cel.Cells(1).Value <> "" Then
                        With cel.Cells(1): crit = IsNull(.Font.Color) Or IsNull(.Font.Bold) Or IsNull(.Font.Italic): End With
                        If crit Then
                            TD.innerHTML = htmltexte(cel.Cells(1))
                        Else
                            TD.innerHTML = cel.Cells(1).Text
                        End If
                        
                    End If
                    With TD.Style
                        If lig = 2 Then w = w + Round((cel.width + 1) / 0.75)
                        
                        .width = Round((cel.width + 1) / 0.75) & "px"
                        .Height = Round((cel.Height + 1) / 0.75) & "px"
                        .backgroundColor = coul_XL_to_coul_HTMLX(cel.DisplayFormat.Interior.Color)
                        If Not IsNull(cel.Cells(1).Font.Color) And cel.Cells(1).Font.Color <> vbBlack Then .Color = coul_XL_to_coul_HTMLX(cel.Cells(1).Font.Color)
                        Set bw = cel.Borders(xlEdgeLeft)
                        If cel.Borders(xlEdgeLeft).LineStyle <> xlNone Then
                            bws = bw.LineStyle
                            .borderLeftWidth = Switch(bw.Weight = xlThick, "3px", bw.Weight = xlMedium, "2px", bw.Weight = xlThin, "1px", bw.Weight = xlHairline, "0.5pt")
                            .borderLeftColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeLeft).Color)
                            .borderLeftStyle = Switch(bws = xlContinuous, "solid", bws = xlDash, "dashed", bws = xlDot, "dotted", bws = xlDashDot, "dashed", bws = xlDashDotDot, "dotted", bws = xlDouble, "double", bws = xlSlantDashDot, "dashed")
                        Else
                            If GridLine Then
                                .borderLeftWidth = "0.5pt"
                                .borderLeftColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderLeftStyle = "solid"
                            End If
                        End If
                        
                        Set bwt = cel.Borders(xlEdgeTop)
                        If cel.Borders(xlEdgeTop).LineStyle <> xlNone Then
                            bwts = bwt.LineStyle
                            .borderTopWidth = Switch(bwt.Weight = xlThick, "3px", bwt.Weight = xlMedium, "2px", bwt.Weight = xlThin, "1px", bwt.Weight = xlHairline, "0.5pt")
                            .borderTopColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeTop).Color)
                            .borderTopStyle = Switch(bwts = xlContinuous, "solid", bwts = xlDash, "dashed", bwts = xlDot, "dotted", bwts = xlDashDot, "dashed", bwts = xlDashDotDot, "dotted", bwts = xlDouble, "double", bwts = xlSlantDashDot, "dashed")
                            .textAlign = "center"
                        Else
                            If GridLine Then
                                .borderTopWidth = "0.5pt"
                                .borderTopColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderTopStyle = "solid"
                            End If
                        End If
                        
                        If col = rng.Columns.Count Then
                            Set bwr = cel.Borders(xlEdgeRight)
                            If cel.Borders(xlEdgeRight).LineStyle <> xlNone Then
                                bwrs = bwr.LineStyle
                                .borderRightWidth = Switch(bwr.Weight = xlThick, "3px", bwr.Weight = xlMedium, "2px", bwr.Weight = xlThin, "1px", bwr.Weight = xlHairline, "0.5pt")
                                .borderRightColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeRight).Color)
                                .borderRightStyle = Switch(bwrs = xlContinuous, "solid", bwrs = xlDash, "dashed", bwrs = xlDot, "dotted", bwrs = xlDashDot, "dashed", bwrs = xlDashDotDot, "dotted", bwrs = xlDouble, "double", bwrs = xlSlantDashDot, "dashed")
                                
                            Else
                                If GridLine Then
                                End If
                                .borderRightWidth = "0.5pt"
                                .borderRightColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderRightStyle = "solid"
                            End If
                            
                        End If
                        
                        If lig = rng.Rows.Count Then
                            Set bbwr = cel.Borders(xlEdgeBottom)
                            If cel.Borders(xlEdgeBottom).LineStyle <> xlNone Then
                                bbwrs = bbwr.LineStyle
                                .borderBottomWidth = Switch(bbwr.Weight = xlThick, "3px", bbwr.Weight = xlMedium, "2px", bbwr.Weight = xlThin, "1px", bbwr.Weight = xlHairline, "0.5pt")
                                .borderBottomColor = coul_XL_to_coul_HTMLX(cel.Borders(xlEdgeBottom).Color)
                                .borderBottomStyle = Switch(bbwrs = xlContinuous, "solid", bbwrs = xlDash, "dashed", bbwrs = xlDot, "dotted", bbwrs = xlDashDot, "dashed", bbwrs = xlDashDotDot, "dotted", bbwrs = xlDouble, "double", bbwrs = xlSlantDashDot, "dashed")
                            Else
                                If GridLine Then
                                    .borderBottomWidth = "0.5pt"
                                    .borderBottomColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                    .borderBottomStyle = "solid"
                                End If
                                
                                
                            End If
                        End If
                    End With
                End If
                'on sort le CSS du htmlSI cSSiNLINE+FALSE
                If CssOutLine Then
                     If Not dico.exists("{" & TD.Style.cssText & ";}") Then
                        classe = "cla" & dico.Count
                        dico("{" & TD.Style.cssText & ";}") = classe
                        TD.className = classe
                    Else
                        TD.className = dico("{" & TD.Style.cssText & ";}")
                    End If
                    TD.removeAttribute ("style")
                End If
            Next
        End If
    Next
    If CssOutLine Then
        css = "<style>" & vbCrLf
        For Each elem In dico
            css = css & vbCrLf & "." & dico(elem) & elem & vbCrLf
        Next
        '----------------------------------------
        'le css pour figer la colonne  mode "fixed"pour les premier TD de chaque TR
        'css = css & "td:first-child {  position: fixed;  left: 0;  width: 83px; z-index: 10;border-right :2px double red;}/* IMPORTANT = largeur réelle */  " & vbCrLf
        'css = css & "table {  margin-left: " & Round(rng.Cells(1).width / 0.75) & "px;}" & vbCrLf
        '----------------------------------------
        'le css pour figer la colonne  mode "fixed"pour les premier TD de chaque TR
      css = css & "tr td:first-child,tr th:first-child {  position: sticky;  left: 0;  z-index: 5;border-rigth:2px double red;}" & vbCrLf
        
        Set TRs = dhtml.getElementsByTagName("TR")
       Dim i&
       For i = 0 To TRs.Length - 1
       TRs(i).FirstChild.Style.backgroundColor = coul_XL_to_coul_HTMLX(rng.Cells(i + 1, 1).DisplayFormat.Interior.Color)
       TRs(i).FirstChild.Style.borderRight = "3px double red"
       Next
        
        
        css = css & "</style>" & vbCrLf
    End If
    
    With Table.Style
        .borderCollapse = "collapse"
        .width = Int(w) + 1 & "px"
    End With
    
    CreatehtmlTable = "<!DOCTYPE HTML><head>" & css & "</head><body>" & Table.outerHTML & "</body></html>"
End Function

Function coul_XL_to_coul_HTMLX(couleur)
    'collection fonction perso
    'fonction Color XL to HTMLCOLOR ---> By Patricktoulon (2016)
    Dim str0 As String, strf As String
    str0 = Right("000000" & Hex(couleur), 6): strf = Right(str0, 2) & Mid(str0, 3, 2) & left(str0, 2)
    coul_XL_to_coul_HTMLX = "#" & strf & ""
End Function

Function htmltexte(cel As Range)
    'Fonction de récupération du code html du texte formaté dans la cellule
    'Patricktoulon (2016)
    Dim cde$, elem, Dc As New HTMLDocument
    cde = Replace(Replace(Replace(cel.Value(11), "ss:", ""), "Data", "Div"), "html:", "")
    cde = Replace(cde, " ", vbCrLf)
    With Dc
        .body.innerHTML = cde
        Debug.Print .body.innerHTML
        For Each elem In .all
            If elem.getAttribute("size") <> "" Then elem.Style.FontSize = elem.getAttribute("size") & "pt"
            elem.removeAttribute ("size")
        Next
        htmltexte = .getElementsByTagName("Div")(0).innerHTML
    End With
End Function
Et voici comment je fais pour l'appeler
Code:
Sub test()
    Dim CodeHtmL$, htmlFicH, x&
    CodeHtmL = CreatehtmlTable(Sheets("Calendrier Livraison").Range("A1:AE22"), False, True)
    htmlFicH = ThisWorkbook.Path & "\mapage.html"
    x = FreeFile
    Open htmlFicH For Output As #x: Print #x, CodeHtmL: Close #x
    DoEvents
    Shell "cmd /c start msedge """ & htmlFicH & """", vbHide
End Sub

Patrick
noyeux joel

Salut,
patricktoulon tu as essayé ton code dans le classeur de Titof06 ? Moi sous Excel 2021 64 bits j'ai une erreur sur cette ligne :
VB:
                If dhtml.getElementById(IdX) Is Nothing Then

erreur : Objet Requis
Nullosse
 
Salut,
patricktoulon tu as essayé ton code dans le classeur de Titof06 ? Moi sous Excel 2021 64 bits j'ai une erreur sur cette ligne :
VB:
                If dhtml.getElementById(IdX) Is Nothing Then

erreur : Objet Requis
Nullosse

Bonjour @nullosse
bien sur que oui
je vois pas pourquoi si ce n'est que tu n'a peut être pas la librairie pour créer le "htmlfile" c'est fréquent sur 64 bits
cette condition sert a ne pas recréer une cel html portant le id d'une mergearea deja existante (gestion de fusion)
Pour afficher ce contenu, nous aurons besoin de votre consentement pour définir des cookies tiers.
Pour plus d'informations, consultez notre page sur les cookies.
 
différence entre Excel 2013 et version supérieure pour expliquer l'erreur :
le moteur MSHTML utilisé par Excel ne dépend pas seulement de Windows, mais aussi de la version d’Excel elle‑même.
test à faire :
VB:
Sub TestMSHTML()
    Dim doc As Object
    Set doc = CreateObject("htmlfile")
    Debug.print doc.parentWindow.navigator.userAgent
End Sub
moi j'ai à la fin : Trident/7.0; rv:11.0
  • MSIE 9.0 → moteur IE9
  • MSIE 10.0 → moteur IE10
  • Trident/7.0; rv:11.0 → moteur IE11
 
ben il vous manque le com pour dom document alias "htmlfile" en late binding
moi j'ai la totale car j'ai refusé la mise à jour qui a supprimé IE de win 10
j'ai donc toute les librairies afférentes
sur win 11 il y a de fortes chances que ce soit shunté
il est possible que certaine fonction comme GetElementById a l'instar de GetElementsByClassname( qui a été supprimée de la librairie (sur 2007 a l’époque) aie été supprimée
autrement dit fortementça a fortement handicapé la manipulation basique du DOM dans un object document html en mémoire

j'ai déjà vu un cas comme ça chez un client
je lui réinstallé IE 11 (forcé) sur win 11 et réactiver en ré enregistrant certaines DLLs
je vais essayer de retrouver ce tuto qui m'avait été bien utile
 
L'I.A m'a proposé une modification du code de patricktoulon pour un moteur IE11 qui fonctionne chez moi et qui doit fonctionner avec toutes les versions d'IE :
VB:
Function CreatehtmlTable(rng As Range, Optional GridLine As Boolean = False, Optional CssOutLine As Boolean = False) As String
    Dim dhtml As Object, dico As Object, dejaVu As Object
    Dim Table, TbodY, cel, lig As Long, col As Long
    Dim crit, TR, TD, bw, bws, bwt, bwts, bwr, bwrs, bbwr, bbwrs
    Dim classe, css As String, elem, IdX As String, w As Double
    Dim TRs, i As Long

    If CssOutLine Then Set dico = CreateObject("Scripting.Dictionary")
    Set dejaVu = CreateObject("Scripting.Dictionary")

    Set dhtml = CreateObject("htmlfile")
    dhtml.body.innerHTML = "<table><tbody></tbody></table>"
    Set Table = dhtml.getElementsByTagName("table")(0)
    Set TbodY = dhtml.getElementsByTagName("tbody")(0)

    For lig = 1 To rng.Rows.Count
        If Application.WorksheetFunction.CountA(rng.Cells(lig, 1).Resize(1, rng.Columns.Count)) > 0 Then

            Set TR = TbodY.appendChild(dhtml.createElement("tr"))

            For col = 1 To rng.Columns.Count

                Set cel = rng.Cells(lig, col).MergeArea
                IdX = Replace(cel.Cells(1, 1).Address(0, 0), "$", "")

                ' *** NE PLUS UTILISER getElementById ? on passe par un dictionnaire ***
                If Not dejaVu.Exists(IdX) Then
                    dejaVu.Add IdX, True

                    Set TD = TR.appendChild(dhtml.createElement("td"))
                    TD.setAttribute "id", IdX
                    TD.colSpan = cel.Columns.Count
                    TD.rowSpan = cel.Rows.Count

                    If cel.Cells(1).value <> "" Then
                        With cel.Cells(1)
                            crit = IsNull(.Font.Color) Or IsNull(.Font.Bold) Or IsNull(.Font.Italic)
                        End With
                        If crit Then
                            TD.innerHTML = htmltexte(cel.Cells(1))
                        Else
                            TD.innerHTML = cel.Cells(1).text
                        End If
                    End If

                    With TD.Style
                        If lig = 2 Then w = w + Round((cel.width + 1) / 0.75)

                        .width = Round((cel.width + 1) / 0.75) & "px"
                        .Height = Round((cel.Height + 1) / 0.75) & "px"
                        .backgroundColor = coul_XL_to_coul_HTMLX(cel.DisplayFormat.Interior.Color)

                        If Not IsNull(cel.Cells(1).Font.Color) And cel.Cells(1).Font.Color <> vbBlack Then
                            .Color = coul_XL_to_coul_HTMLX(cel.Cells(1).Font.Color)
                        End If

                        ' Bordure gauche
                        Set bw = cel.Borders(xlEdgeLeft)
                        If bw.LineStyle <> xlNone Then
                            bws = bw.LineStyle
                            .borderLeftWidth = Switch(bw.Weight = xlThick, "3px", bw.Weight = xlMedium, "2px", bw.Weight = xlThin, "1px", bw.Weight = xlHairline, "0.5pt")
                            .borderLeftColor = coul_XL_to_coul_HTMLX(bw.Color)
                            .borderLeftStyle = Switch(bws = xlContinuous, "solid", bws = xlDash, "dashed", bws = xlDot, "dotted", bws = xlDashDot, "dashed", bws = xlDashDotDot, "dotted", bws = xlDouble, "double", bws = xlSlantDashDot, "dashed")
                        ElseIf GridLine Then
                            .borderLeftWidth = "0.5pt"
                            .borderLeftColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                            .borderLeftStyle = "solid"
                        End If

                        ' Bordure haut
                        Set bwt = cel.Borders(xlEdgeTop)
                        If bwt.LineStyle <> xlNone Then
                            bwts = bwt.LineStyle
                            .borderTopWidth = Switch(bwt.Weight = xlThick, "3px", bwt.Weight = xlMedium, "2px", bwt.Weight = xlThin, "1px", bwt.Weight = xlHairline, "0.5pt")
                            .borderTopColor = coul_XL_to_coul_HTMLX(bwt.Color)
                            .borderTopStyle = Switch(bwts = xlContinuous, "solid", bwts = xlDash, "dashed", bwts = xlDot, "dotted", bwts = xlDashDot, "dashed", bwts = xlDashDotDot, "dotted", bwts = xlDouble, "double", bwts = xlSlantDashDot, "dashed")
                            .textAlign = "center"
                        ElseIf GridLine Then
                            .borderTopWidth = "0.5pt"
                            .borderTopColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                            .borderTopStyle = "solid"
                        End If

                        ' Bordure droite (dernière colonne)
                        If col = rng.Columns.Count Then
                            Set bwr = cel.Borders(xlEdgeRight)
                            If bwr.LineStyle <> xlNone Then
                                bwrs = bwr.LineStyle
                                .borderRightWidth = Switch(bwr.Weight = xlThick, "3px", bwr.Weight = xlMedium, "2px", bwr.Weight = xlThin, "1px", bwr.Weight = xlHairline, "0.5pt")
                                .borderRightColor = coul_XL_to_coul_HTMLX(bwr.Color)
                                .borderRightStyle = Switch(bwrs = xlContinuous, "solid", bwrs = xlDash, "dashed", bwrs = xlDot, "dotted", bwrs = xlDashDot, "dashed", bwrs = xlDashDotDot, "dotted", bwrs = xlDouble, "double", bwrs = xlSlantDashDot, "dashed")
                            ElseIf GridLine Then
                                .borderRightWidth = "0.5pt"
                                .borderRightColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderRightStyle = "solid"
                            End If
                        End If

                        ' Bordure bas (dernière ligne)
                        If lig = rng.Rows.Count Then
                            Set bbwr = cel.Borders(xlEdgeBottom)
                            If bbwr.LineStyle <> xlNone Then
                                bbwrs = bbwr.LineStyle
                                .borderBottomWidth = Switch(bbwr.Weight = xlThick, "3px", bbwr.Weight = xlMedium, "2px", bbwr.Weight = xlThin, "1px", bbwr.Weight = xlHairline, "0.5pt")
                                .borderBottomColor = coul_XL_to_coul_HTMLX(bbwr.Color)
                                .borderBottomStyle = Switch(bbwrs = xlContinuous, "solid", bbwrs = xlDash, "dashed", bbwrs = xlDot, "dotted", bbwrs = xlDashDot, "dashed", bbwrs = xlDashDotDot, "dotted", bbwrs = xlDouble, "double", bbwrs = xlSlantDashDot, "dashed")
                            ElseIf GridLine Then
                                .borderBottomWidth = "0.5pt"
                                .borderBottomColor = coul_XL_to_coul_HTMLX(RGB(230, 230, 230))
                                .borderBottomStyle = "solid"
                            End If
                        End If
                    End With

                    ' Sortie CSS externe si demandé
                    If CssOutLine Then
                        If Not dico.Exists("{" & TD.Style.cssText & ";}") Then
                            classe = "cla" & dico.Count
                            dico("{" & TD.Style.cssText & ";}") = classe
                            TD.className = classe
                        Else
                            TD.className = dico("{" & TD.Style.cssText & ";}")
                        End If
                        TD.removeAttribute "style"
                    End If

                End If ' Not dejaVu.Exists

            Next col
        End If
    Next lig

    css = ""
    If CssOutLine Then
        css = "<style>" & vbCrLf
        For Each elem In dico
            css = css & "." & dico(elem) & elem & vbCrLf
        Next

        ' sticky première colonne
        css = css & "tr td:first-child,tr th:first-child { position: sticky; left: 0; z-index: 5; }" & vbCrLf

        Set TRs = dhtml.getElementsByTagName("tr")
        For i = 0 To TRs.Length - 1
            TRs(i).FirstChild.Style.backgroundColor = coul_XL_to_coul_HTMLX(rng.Cells(i + 1, 1).DisplayFormat.Interior.Color)
            TRs(i).FirstChild.Style.borderRight = "3px double red"
        Next

        css = css & "</style>" & vbCrLf
    End If

    With Table.Style
        .borderCollapse = "collapse"
        .width = Int(w) + 1 & "px"
    End With

    CreatehtmlTable = "<!DOCTYPE HTML><head>" & css & "</head><body>" & Table.outerHTML & "</body></html>"
End Function

La solution universelle (Windows 7 → 11, Excel 2010 → 365)
Ne plus utiliser getElementById pendant la construction du DOM.
À la place, utilise un dictionnaire VBA pour savoir si une cellule fusionnée a déjà été traitée.
C’est la seule méthode fiable sur toutes les versions d’Excel et Windows.
 
Re-,
Bug à ce niveau :

1776336093405.png
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Retour