BlackFlag
Messages postés4Date d'inscriptionlundi 12 mai 2003StatutMembreDernière intervention19 mars 2012
-
25 sept. 2009 à 10:06
BlackFlag
Messages postés4Date d'inscriptionlundi 12 mai 2003StatutMembreDernière intervention19 mars 2012
-
25 sept. 2009 à 11:38
Bonjour à tous !
J'ai un projet en perpétuelle évolution (VB.NET 2005) et sur la dernière mouture je voulais "déplacer" ma routine d'impression dans une .dll mais là ... je bloque
j'ai essayé de mettre ma routine dans une classe de ma .dll:
Imports System
Imports System.IO
Imports Microsoft.Office.Interop
Public Class FicheTechnique_Print
Private NomDoc As String
Private CheminDoc As String
Private NomModele As String
Private CheminModele As String
Private Impression As Boolean
Private Valeurs As DataSet
Public Property DOC_NAME() As String
' On définit la propriété FileName, le fichier qui contient les îcones.
Get
Return NomDoc
End Get
Set(ByVal Value As String)
NomDoc = Value
End Set
End Property
Public Property DOC_PATH() As String
' On définit la propriété FileName, le fichier qui contient les îcones.
Get
Return CheminDoc
End Get
Set(ByVal Value As String)
CheminDoc = Value
End Set
End Property
Public Property DOT_NAME() As String
' On définit la propriété FileName, le fichier qui contient les îcones.
Get
Return NomModele
End Get
Set(ByVal Value As String)
NomModele = Value
End Set
End Property
Public Property DOT_PATH() As String
' On définit la propriété FileName, le fichier qui contient les îcones.
Get
Return CheminModele
End Get
Set(ByVal Value As String)
CheminModele = Value
End Set
End Property
Public Property BolPrint() As Boolean
' On définit la propriété FileName, le fichier qui contient les îcones.
Get
Return Impression
End Get
Set(ByVal Value As Boolean)
Impression = Value
End Set
End Property
Public Property DS_Report() As DataSet
' On définit la propriété FileName, le fichier qui contient les îcones.
Get
Return Valeurs
End Get
Set(ByVal Value As DataSet)
Valeurs = Value
End Set
End Property
Public Sub btn_Imprim()
Dim appWd As Word.Application
Dim Doc As Word.Document
Dim Nix As Object
Dim contenu As String
On Error Resume Next 'see if word is running'todo - on error must be replaced with try, catch, finally
appWd = GetObject(, "word.application")
If Err.Number <> 0 Then 'word not running
Err.Clear() ' clear err object in case error occurred.
'create a new instance of word
appWd = CreateObject("word.application")
'create an instance of word
Else
appWd.Activate()
End If
On Error GoTo btn_Imprim_Error
'On Error Resume Next
With appWd
'open dot-file as document
.Documents.Add(Template:=Chr(34) & CheminModele & NomModele & Chr(34))
'Set winword visible
'.Visible = True
'maximize WinWord
'.WindowState = Word.WdWindowState.wdWindowStateMaximize
Doc = .ActiveDocument
With Valeurs.Tables("FTN_GENERAL")
If .Rows.Count > 0 Then
With .Rows(0)
Doc.Bookmarks("fldTITRE").Select()
Doc.Selection.InsertAfter(NZStr(.Item("TITRE").ToString))
Doc.Bookmarks("fldDESCRIPTION").Select()
Doc.Selection.InsertAfter(NZStr(.Item("DESCRIPTION").ToString))
Doc.FormFields("fldREVISION_DATE").Result = NZStr(.Item("REVISION_DATE").ToString)
Doc.FormFields("fldCODE_APPEL").Result = .Item("CODE_APPEL").ToString
'DataSet.DT_FTN.AddDT_FTNRow(, .Item("LANG").ToString, "SPECIFICATIONS", "", "", .Item("SPECIFICATIONS").ToString, "")
End With
End If
End With
Select Case Impression
Case False
.ActiveDocument.SaveAs(Chr(34) & CheminDoc & NomDoc & ".doc" & Chr(34))
'Set winword visible
.Visible = True
'maximize WinWord
.WindowState = Word.WdWindowState.wdWindowStateMaximize
Case True
.ActiveDocument.SaveAs(Chr(34) & CheminDoc & NomDoc & ".doc" & Chr(34))
.ActiveDocument.PrintOut(Background:=False)
appWd.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
End Select
'save document
'if you want to write a history, take filename from here and rewrite it into access
'.ActiveDocument.SaveAs Chr(34) & DOC_PATH & DOC_NAME & ".doc" & Chr(34)
'.ActiveDocument.SaveAs Chr(34) & DOC_PATH & DOC_NAME & ".doc" & Chr(34), FileFormat:=wdFormatPDF
'You wanna print ?
'.ActiveDocument.PrintOut Background:=False
'You wanna quit WinWord ?
' .Quit
'Set Doc = .Documents.Open(DOC_PATH & DOC_NAME & ".doc", , True)
End With
btn_Imprim_Exit:
Exit Sub
btn_Imprim_Error:
If Err.Number = 5941 Then
Nix = MsgBox("Fichier Modéle erroné (Signet Manquant)" & _
vbCrLf & "Fichier Modéle : " & CheminModele, _
vbCritical, "Annulation WinWord")
appWd.Quit()
GoTo btn_Imprim_Exit
Else
Nix = MsgBox("Erreur Générale lors de l'appel de WinWord" & _
vbCrLf & "l'Erreur-No est : " & Err.Number & " " & Err.Description, _
vbCritical, "WinWord Annulé")
appWd.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
GoTo btn_Imprim_Exit
End If
End Sub
End Class
... et dans un module :
Imports System
Imports System.IO
Imports Microsoft.Office.Interop
Module Print
Public Sub Doc_Imprim(ByVal DOT_PATH As String, ByVal DOT_NAME As String, ByVal DOC_PATH As String, ByVal DOC_NAME As String, ByVal BolPrint As Boolean, ByVal DS_Report As DataSet)
Dim appWd As Word.Application
Dim Doc As Word.Document
Dim Nix As Object
Dim contenu As String
On Error Resume Next 'see if word is running'todo - on error must be replaced with try, catch, finally
appWd = GetObject(, "word.application")
If Err.Number <> 0 Then 'word not running
Err.Clear() ' clear err object in case error occurred.
'create a new instance of word
appWd = CreateObject("word.application")
'create an instance of word
Else
appWd.Activate()
End If
On Error GoTo Doc_Imprim_Error
'On Error Resume Next
With appWd
'open dot-file as document
.Documents.Add(Template:=Chr(34) & DOT_PATH & DOT_NAME & Chr(34))
'Set winword visible
'.Visible = True
'maximize WinWord
'.WindowState = Word.WdWindowState.wdWindowStateMaximize
Doc = .ActiveDocument
With DS_Report.Tables("FTN_GENERAL")
If .Rows.Count > 0 Then
With .Rows(0)
Doc.Bookmarks("fldTITRE").Select()
Doc.Selection.InsertAfter(NZStr(.Item("TITRE").ToString))
Doc.Bookmarks("fldDESCRIPTION").Select()
Doc.Selection.InsertAfter(NZStr(.Item("DESCRIPTION").ToString))
Doc.FormFields("fldREVISION_DATE").Result = NZStr(.Item("REVISION_DATE").ToString)
Doc.FormFields("fldCODE_APPEL").Result = .Item("CODE_APPEL").ToString
'DataSet.DT_FTN.AddDT_FTNRow(, .Item("LANG").ToString, "SPECIFICATIONS", "", "", .Item("SPECIFICATIONS").ToString, "")
End With
End If
End With
Select Case BolPrint
Case False
.ActiveDocument.SaveAs(Chr(34) & DOC_PATH & DOC_NAME & ".doc" & Chr(34))
'Set winword visible
.Visible = True
'maximize WinWord
.WindowState = Word.WdWindowState.wdWindowStateMaximize
Case True
.ActiveDocument.SaveAs(Chr(34) & DOC_PATH & DOC_NAME & ".doc" & Chr(34))
.ActiveDocument.PrintOut(Background:=False)
appWd.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
End Select
'save document
'if you want to write a history, take filename from here and rewrite it into access
'.ActiveDocument.SaveAs Chr(34) & DOC_PATH & DOC_NAME & ".doc" & Chr(34)
'.ActiveDocument.SaveAs Chr(34) & DOC_PATH & DOC_NAME & ".doc" & Chr(34), FileFormat:=wdFormatPDF
'You wanna print ?
'.ActiveDocument.PrintOut Background:=False
'You wanna quit WinWord ?
' .Quit
'Set Doc = .Documents.Open(DOC_PATH & DOC_NAME & ".doc", , True)
End With
Doc_Imprim_Exit:
Exit Sub
Doc_Imprim_Error:
If Err.Number = 5941 Then
Nix = MsgBox("Fichier Modéle erroné (Signet Manquant)" & _
vbCrLf & "Fichier Modéle : " & DOT_NAME, _
vbCritical, "Annulation WinWord")
appWd.Quit()
GoTo Doc_Imprim_Exit
Else
Nix = MsgBox("Erreur Générale lors de l'appel de WinWord" & _
vbCrLf & "l'Erreur-No est : " & Err.Number & " " & Err.Description, _
vbCritical, "WinWord Annulé")
appWd.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
GoTo doc_Imprim_Exit
End If
End Sub
End Module
En essayant d'appeler soit l'une ou l'autre par ces 2 lignes dans mon prog principal:
'Declaration
Declare Sub Imprim Lib "Cartha-Office_DotNet_Print.dll" Alias "btn_Imprim" (ByVal DOT_PATH As String, ByVal DOT_NAME As String, ByVal DOC_PATH As String, ByVal DOC_NAME As String, ByVal BolPrint As Boolean, ByVal DS_Report As DataSet)
'Appel
imprim(DOT_PATH, DOT_NAME, DOC_PATH, DOC_NAME, BolPrint, DS_Report)
En ayant bien sur pris soins de référencer mon projet .dll dans le principal mais j'obtiens toujours le fameux message comme quoi le point d'entrée (btn_Imprim si j'essaye pour la class, Doc_Imprim pour le module) n'est pas trouvé !
Si qq'un avait 5mins pour y jeter coup d'œil, ce serait super sympa. Merci !
BlackFlag
Messages postés4Date d'inscriptionlundi 12 mai 2003StatutMembreDernière intervention19 mars 2012 25 sept. 2009 à 11:38
C'est bon !
J'ai pour finir supprimé la déclaration :
'Declaration
Declare Sub Imprim Lib "Cartha-Office_DotNet_Print.dll" Alias "btn_Imprim" (ByVal DOT_PATH As String, ByVal DOT_NAME As String, ByVal DOC_PATH As String, ByVal DOC_NAME As String, ByVal BolPrint As Boolean, ByVal DS_Report As DataSet)
Et remplacé cette dernière par un 'import' :
Imports Cartha_Office_DotNet_Print
Et ça fonctionne avec l'appel de 'doc_imprim' qui se trouve dans le module précédemment appelé 'print' rebaptisé 'doc_print' pour éviter des conflits :