1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
   | Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.IO
Imports System.Web
Imports IWord = Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices.Marshal
 
Namespace SDEA
    Namespace Interop
        Public Class Word
            Implements IDisposable
#Region "Membres privés"
            Private _word As IWord.Application
            Private _doc As IWord.Document
            Private _docPath As String
#End Region
 
#Region "Propriétés publiques"
            Public ReadOnly Property WordObject() As IWord.Application
                Get
                    Return _word
                End Get
            End Property
 
            Public ReadOnly Property DocObject() As IWord.Document
                Get
                    Return _doc
                End Get
            End Property
 
            Public ReadOnly Property NB_Tables() As Integer
                Get
                    Return _doc.Tables.Count
                End Get
            End Property
 
            Public ReadOnly Property Tables() As IWord.Tables
                Get
                    Return _doc.Tables
                End Get
            End Property
#End Region
 
#Region "Constructeurs/Destructeurs"
            Sub New(ByVal wordModel As String, Optional ByVal showWordInstance As Boolean = True)
                Try
                    _word = New IWord.Application
 
                    _word.Visible = showWordInstance
                    _doc = _word.Documents.Add(wordModel)
 
                    _docPath = wordModel
                Catch ex As Exception
                    Throw ex
                End Try
            End Sub
#End Region
 
#Region "Procédures et fonctions publiques"
            Public Sub InsertImage(ByVal bookmark As String, ByVal filename As String)
                Try
                    _word.ActiveDocument.Bookmarks.Item(bookmark).Range.InlineShapes.AddPicture(filename)
                Catch ex As Exception
                    Throw ex
                End Try
            End Sub
 
            Public Sub InsertText(ByVal bookmark As String, ByVal text As String)
                Try
                    _word.ActiveDocument.Bookmarks.Item(bookmark).Range.Text = text
                Catch ex As Exception
                    Throw ex
                End Try
            End Sub
 
#Region "Procédures de gestion des yableaux"
            Public Sub InsererTableau(ByVal bookmark As String,
                                      Optional ByVal nbRows As Integer = 1,
                                      Optional ByVal nbCols As Integer = 1,
                                      Optional ByVal style As String = "")
 
                Dim oTable As IWord.Table = _doc.Tables.Add(_doc.Bookmarks.Item(bookmark).Range, nbRows, nbCols)
 
                If Not String.IsNullOrEmpty(style) Then
                    oTable.Style = style
                End If
            End Sub
 
            Public Sub InsererTableau(ByVal bookmark As String,
                                      ByRef tableSource As System.Web.UI.WebControls.Table,
                                      Optional ByVal style As String = "")
 
                Dim oTable As IWord.Table = _doc.Tables.Add(_doc.Bookmarks.Item(bookmark).Range, tableSource.Rows.Count, tableSource.Rows(0).Cells.Count)
 
                For r As Integer = 0 To tableSource.Rows.Count - 1
                    For c As Integer = 0 To tableSource.Rows(r).Cells.Count - 1
                        oTable.Cell(r + 1, c + 1).Range.Text = tableSource.Rows(r).Cells(c).Text
                    Next
                Next
 
                If Not String.IsNullOrEmpty(style) Then
                    oTable.Style = style
                End If
            End Sub
 
            Public Sub AjouterLigneTableau(ByRef table As IWord.Table)
                table.Rows.Add()
 
                For i As Integer = 1 To table.Rows(0).Cells.Count
                    table.Rows(table.Rows.Count).Cells.Add()
                Next
            End Sub
 
            Public Sub AjouterLigneTableau(ByRef table As IWord.Table, ByVal nbCells As Integer)
                table.Rows.Add()
 
                For i As Integer = 1 To nbCells
                    table.Rows(table.Rows.Count).Cells.Add()
                Next
            End Sub
#End Region
#End Region
 
#Region "IDisposable Support"
            Private disposedValue As Boolean
 
            Protected Overridable Sub Dispose(disposing As Boolean)
                If Not Me.disposedValue Then
                    If disposing Then
                        ' TODO: supprimez l'état managé (objets managés).
                    End If
 
                    ' TODO: libérez les ressources non managées (objets non managés) et substituez la méthode Finalize() ci-dessous.
                    ReleaseComObject(_doc)
                    ReleaseComObject(_word)
 
                    GC.Collect()
                    GC.WaitForPendingFinalizers()
 
                    ' TODO: définissez les champs volumineux à null.
                End If
                Me.disposedValue = True
            End Sub
 
            ' TODO: substituez Finalize() uniquement si Dispose(ByVal disposing As Boolean) ci-dessus comporte du code permettant de libérer des ressources non managées.
            Protected Overrides Sub Finalize()
                ' Ne modifiez pas ce code. Ajoutez du code de nettoyage dans Dispose(ByVal disposing As Boolean) ci-dessus.
                Dispose(False)
                MyBase.Finalize()
            End Sub
 
            Public Sub Dispose() Implements IDisposable.Dispose
                Dispose(True)
                GC.SuppressFinalize(Me)
            End Sub
#End Region
        End Class
    End Namespace
End Namespace | 
Partager