Attribute VB_Name = "WordModule"
Option Explicit

Public Sub PrintStockBalance(ByVal sDateBegin As Date, ByVal sDateEnd As Date, ByVal sFileName As String, Optional ByVal Hwnd As Long = 0)
    Dim rs As New ADODB.Recordset
    Dim MyWordDoc As Word.Application
    
    Dim lRecordCnt As Long
    Dim lTmpVal As Long
    lTmpVal = 0
    
    Set MyWordDoc = CreateObject("word.application")
    
    EnableWindow Hwnd, 0
    PrintForm.Show
    
    rs.Open "Stock_Balance ORDER BY Catégorie, Désignation", cn, 1, 2
    lRecordCnt = rs.RecordCount
    If rs.EOF Then GoTo endsub
    
    MyWordDoc.Documents.Open FILE_TEMPLATE_STOCKBALANCE
    With MyWordDoc
        .Selection.GoTo , , , "siDateDébut"
        .Selection.TypeText cFormatDate(sDateBegin, ML_FRANCAIS)
        .Selection.GoTo , , , "siDateFin"
        .Selection.TypeText cFormatDate(sDateEnd, ML_FRANCAIS)
        .Selection.GoTo , , , "siCatégorie"
        .Selection.InsertRowsBelow lRecordCnt - 1
        .Selection.GoTo , , , "siCatégorie"
        .Selection.TypeText rs!Catégorie
        .Selection.GoTo , , , "siDésignation"
        .Selection.TypeText rs!Désignation
        .Selection.GoTo , , , "siQuantité"
        .Selection.TypeText rs!Restant
        .Selection.GoTo , , , "siValeurAchat"
        .Selection.TypeText rs!ValeurAchat
        .Selection.GoTo , , , "siValeurVente"
        .Selection.TypeText rs!ValeurVente
        .Selection.GoTo , , , "siBénéfice"
        .Selection.TypeText rs!Bénéfice
        rs.MoveNext
        Do While Not rs.EOF
            lTmpVal = lTmpVal + 1
            .Selection.MoveDown wdLine, lTmpVal
            .Selection.GoTo , , , "siCatégorie"
            .Selection.MoveDown wdLine, lTmpVal
            .Selection.TypeText rs!Catégorie
            .Selection.GoTo , , , "siDésignation"
            .Selection.MoveDown wdLine, lTmpVal
            .Selection.TypeText rs!Désignation
            .Selection.GoTo , , , "siQuantité"
            .Selection.MoveDown wdLine, lTmpVal
            .Selection.TypeText rs!Restant
            .Selection.GoTo , , , "siValeurAchat"
            .Selection.MoveDown wdLine, lTmpVal
            .Selection.TypeText rs!ValeurAchat
            .Selection.GoTo , , , "siValeurVente"
            .Selection.MoveDown wdLine, lTmpVal
            .Selection.TypeText rs!ValeurVente
            .Selection.GoTo , , , "siBénéfice"
            .Selection.MoveDown wdLine, lTmpVal
            .Selection.TypeText rs!Bénéfice
            rs.MoveNext
        Loop
    End With
    MyWordDoc.ActiveDocument.SaveAs sFileName
    MyWordDoc.Quit 0
    
endsub:
    rs.Close
    Unload PrintForm
    EnableWindow Hwnd, 1
End Sub
