IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Problème avec mon Word (conversion de 'Word.Application' en 'System.Windows.Forms.Application)


Sujet :

Windows Forms

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut Problème avec mon Word (conversion de 'Word.Application' en 'System.Windows.Forms.Application)
    Bonjour,

    je voudrais transmettre les contenues de mes variables à un fichier word.

    J'ai donc ajouté des références(com) dans mon projet:

    Microsoft Word 11.0 et Microsoft Word 12.0 .

    Ensuite dans mon projet:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    '//Pour la classe Registry
    Imports Microsoft.Win32
    Public class FrmInfoCand
     
    Private Sub bou_tool_generer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bou_tool_generer.Click
     
    objWord = New Word.Application
    objWord.Visible = True
     
    end sub
     
    End class
    Mon problème se situe sur la ligne objWord = New Word.Application, j'ai un message d'erreur :
    Des erreurs d'exécution peuvent se produire lors de la conversion de 'Word.Application' en 'System.Windows.Forms.Application'.
    Que faire ???

    D'avance merci

  2. #2
    Membre chevronné
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    1 048
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2009
    Messages : 1 048
    Points : 2 201
    Points
    2 201
    Par défaut
    Je te conseille en premier lieu de te concentrer avec la référence Microsoft Word 12 (qui est pour office 2003) ou la 11 selon la version utilisée dans l'environnement de déploiement (si ce dernier n'est pas connu je te conseille d'éviter le pilotage d'office tout court).

    Ensuite je te conseille de spécifier entièrement l'espace de nom pour le pilotage de word, plus particulièrement pour l'objet Application. En effet ce dernier prête parfois à confusion avec d'autre objet application.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Dim WrdApp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
    J'ai deja eu des situations ou la simple modification d'une ligne a suffit pour le compilateur perd s'embrouille. Bref soit le plus précis possible surtout sur les thème "générique" tels que application.

    Pour Office 2007, Office 2010 et le format docx, certains diront qu'il est peut être plus propre de travailler directement sur le format XML du fichier.

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    Ok merci je vais continuer a chercher après une solution. J'avais trouvé une source pour piloter Word mais 2003 avec les signets .

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
     
    'Pour la classe Registry
    Imports Microsoft.Win32
     
    Public Class Form1
        Inherits System.Windows.Forms.Form
     
    #Region " Code généré par le Concepteur Windows Form "
     
        Public Sub New()
            MyBase.New()
     
            'Cet appel est requis par le Concepteur Windows Form.
            InitializeComponent()
     
            'Ajoutez une initialisation quelconque après l'appel InitializeComponent()
     
        End Sub
     
        'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
     
        'Requis par le Concepteur Windows Form
        Private components As System.ComponentModel.IContainer
     
        'REMARQUE*: la procédure suivante est requise par le Concepteur Windows Form
        'Elle peut être modifiée en utilisant le Concepteur Windows Form.  
        'Ne la modifiez pas en utilisant l'éditeur de code.
        Friend WithEvents btnLancerWord As System.Windows.Forms.Button
        Friend WithEvents txtNom As System.Windows.Forms.TextBox
        Friend WithEvents txtAdresse As System.Windows.Forms.TextBox
        Friend WithEvents txtCP As System.Windows.Forms.TextBox
        Friend WithEvents txtVille As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents Label3 As System.Windows.Forms.Label
        Friend WithEvents Label4 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.btnLancerWord = New System.Windows.Forms.Button()
    Me.txtNom = New System.Windows.Forms.TextBox()
    Me.txtAdresse = New System.Windows.Forms.TextBox()
    Me.txtCP = New System.Windows.Forms.TextBox()
    Me.txtVille = New System.Windows.Forms.TextBox()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.SuspendLayout()
    '
    'btnLancerWord
    '
    Me.btnLancerWord.Location = New System.Drawing.Point(144, 176)
    Me.btnLancerWord.Name = "btnLancerWord"
    Me.btnLancerWord.Size = New System.Drawing.Size(112, 24)
    Me.btnLancerWord.TabIndex = 0
    Me.btnLancerWord.Text = "Lancer Word"
    '
    'txtNom
    '
    Me.txtNom.Location = New System.Drawing.Point(144, 40)
    Me.txtNom.Name = "txtNom"
    Me.txtNom.Size = New System.Drawing.Size(248, 20)
    Me.txtNom.TabIndex = 1
    Me.txtNom.Text = ""
    '
    'txtAdresse
    '
    Me.txtAdresse.Location = New System.Drawing.Point(144, 72)
    Me.txtAdresse.Name = "txtAdresse"
    Me.txtAdresse.Size = New System.Drawing.Size(248, 20)
    Me.txtAdresse.TabIndex = 2
    Me.txtAdresse.Text = ""
    '
    'txtCP
    '
    Me.txtCP.Location = New System.Drawing.Point(144, 104)
    Me.txtCP.Name = "txtCP"
    Me.txtCP.Size = New System.Drawing.Size(72, 20)
    Me.txtCP.TabIndex = 3
    Me.txtCP.Text = ""
    '
    'txtVille
    '
    Me.txtVille.Location = New System.Drawing.Point(144, 136)
    Me.txtVille.Name = "txtVille"
    Me.txtVille.Size = New System.Drawing.Size(144, 20)
    Me.txtVille.TabIndex = 4
    Me.txtVille.Text = ""
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(32, 40)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(80, 16)
    Me.Label1.TabIndex = 5
    Me.Label1.Text = "Nom"
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(32, 80)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(88, 16)
    Me.Label2.TabIndex = 6
    Me.Label2.Text = "Adresse"
    '
    'Label3
    '
    Me.Label3.Location = New System.Drawing.Point(32, 112)
    Me.Label3.Name = "Label3"
    Me.Label3.Size = New System.Drawing.Size(96, 16)
    Me.Label3.TabIndex = 7
    Me.Label3.Text = "Code postal"
    '
    'Label4
    '
    Me.Label4.Location = New System.Drawing.Point(32, 144)
    Me.Label4.Name = "Label4"
    Me.Label4.Size = New System.Drawing.Size(104, 16)
    Me.Label4.TabIndex = 8
    Me.Label4.Text = "Ville"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(440, 238)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label4, Me.Label3, Me.Label2, Me.Label1, Me.txtVille, Me.txtCP, Me.txtAdresse, Me.txtNom, Me.btnLancerWord})
    Me.Name = "Form1"
    Me.Text = "Automation Office"
    Me.ResumeLayout(False)
     
        End Sub
     
    #End Region
     
        Private objWord As Word.Application
        Private objDoc As Word.Document
     
        Private Sub btnLancerWord_Click( _
                ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnLancerWord.Click
            Dim sDoc As String
     
            If VerifierWord() = False Then
                MsgBox("Word n'est pas installé sur ce poste")
                Exit Sub
            End If
     
            objWord = New Word.Application()
            objWord.Visible = True
     
            ' Les classes Path et Application doivent être qualifiées
            ' car elles existent dans l'espace de noms Word.
            sDoc = System.IO.Path.GetDirectoryName( _
                    System.Windows.Forms.Application.ExecutablePath())
            sDoc &= "\AutomationWord.doc"
            objDoc = objWord.Documents.Open(sDoc)
     
            RenseignerSignet(txtNom, "Nom")
            RenseignerSignet(txtAdresse, "Adresse")
            RenseignerSignet(txtCP, "CP")
            RenseignerSignet(txtVille, "Ville")
        End Sub
     
        Private Sub RenseignerSignet(ByVal Controle As TextBox, ByVal NomSignet As String)
            Dim objRange As Word.Range
     
            objRange = objDoc.Bookmarks.Item(NomSignet).Range
            With objRange
                .Text = Controle.Text
                .Select()
            End With
            objDoc.Bookmarks.Add(NomSignet, objWord.Selection.Range)
        End Sub
     
        Private Function VerifierWord() As Boolean
            Dim rkCR As RegistryKey = Registry.ClassesRoot
            Dim rkWD As RegistryKey = rkCR.OpenSubKey("Word.Application")
     
            Return Not (rkWD Is Nothing)
            rkWD.Close()
        End Function
    End Class
    Dans le Projet il a juste ajouter comme référence Word.dll ça fonctionne plustot bien sauf que quand j'utilise son code dans mon projet ça ne fonctionne pas !!!

  4. #4
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    quand j'utilise son code dans mon projet ça ne fonctionne pas !!!
    Quel est le message d'erreur ?
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    Le message d'erreur est celui du premier post.

    J'ai trouvé:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    Imports Microsoft.VisualBasic
    Imports Microsoft.Win32
     
    Public Class FrmInfoCand
     
        Dim WordApp As Object
        Dim WordDoc As Object
        Dim WordRng As Object
     
        Private Sub bou_tool_generer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bou_tool_generer.Click
            Dim str_nom As String = String.Empty
            Dim str_prenom As String = String.Empty
            Dim str_requete As String = String.Empty
     
            If FONC_VERIFIER_WORD() = False Then
                MsgBox("Word n'est pas installé sur ce poste !!")
                Exit Sub
            End If
     
            WordApp = CreateObject("Word.Application")
            WordDoc = WordApp.Documents.Open(Application.StartupPath & "\Statut.dotx")
            WordApp.Visible = True
     
     
                str_nom = "test"
                str_prenom = "test1"
     
            Select Case (bou_tool_combo.Text)
                Case "Réserve de recrutement" '//En reserve pour 6 mois 
                    RenseignerSignet("Réserve de recrutement", "Titre")
                    RenseignerSignet(str_nom, "Nom")
                    RenseignerSignet(str_prenom, "Prenom")
                Case "Négatif dans réserve"
                    RenseignerSignet("Négatif dans réserve", "Titre")
                    RenseignerSignet(str_nom, "Nom")
                    RenseignerSignet(str_prenom, "Prenom")
                Case "Entretien"
                    RenseignerSignet("Entretien", "Titre")
                    RenseignerSignet(str_nom, "Nom")
                    RenseignerSignet(str_prenom, "Prenom")
                Case "Négociation"
                    RenseignerSignet("Négociation", "Titre")
                    RenseignerSignet(str_nom, "Nom")
                    RenseignerSignet(str_prenom, "Prenom")
                Case "Engagé"
                    RenseignerSignet("Engagé", "Titre")
                    RenseignerSignet(str_nom, "Nom")
                    RenseignerSignet(str_prenom, "Prenom")
            End Select
     
        End Sub
        ''' <summary>
        ''' FONCTION POUR VERIFIER SI WORD EST INSTALLE
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function FONC_VERIFIER_WORD() As Boolean
            Dim rkCR As RegistryKey = Registry.ClassesRoot
            Dim rkWD As RegistryKey = rkCR.OpenSubKey("Word.Application")
     
            Return Not (rkWD Is Nothing)
            rkWD.Close()
        End Function
        ''' <summary>
        ''' PROCEDURE: ENVOIE LES INFOS AU DOCUMENT
        ''' </summary>
        ''' <param name="Controle"></param>
        ''' <param name="NomSignet"></param>
        ''' <remarks></remarks>
        Private Sub RenseignerSignet(ByVal Controle As Object, ByVal NomSignet As String)
     
            Dim objRange As Object
     
            objRange = WordDoc.Bookmarks.Item(NomSignet).Range
            With objRange
                .Text = Controle
                .Select()
            End With
            'WordDoc.Bookmarks.Add(NomSignet, WordApp.Selection.Range)
        End Sub
    End Class
    Maintenant je voudrais bien trouver comment sauvegarder automatiquement le document sans l'afficher ???

    D'avance merci

  6. #6
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    ça fonctionne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    Dim WordApp As Word.Application
            Dim wordDoc As Word.Document
     
            Dim now As DateTime = DateTime.Now
            Dim nouv_rep_heure As String = now.ToString("HHmmss")
     
     
     
            WordApp = CreateObject("Word.Application")
            wordDoc = WordApp.Documents.Open(Application.StartupPath & "\Statut.docx")
     
            '//Je rentre le texte pour chaque signet.
            wordDoc.Bookmarks.Item("Titre").Range.Text = "Titre ok"
            wordDoc.Bookmarks.Item("Nom").Range.Text = "Nom ok"
            wordDoc.Bookmarks.Item("Prenom").Range.Text = "Prenom ok"
     
            '//Je sauvegarde le document.
            MsgBox(Application.StartupPath & "\" & now.ToString("m") & nouv_rep_heure & ".docx")
            wordDoc.SaveAs(Application.StartupPath & "\" & now.ToString("m") & nouv_rep_heure & ".docx")
            wordDoc.Close()
            wordDoc = Nothing
     
            WordApp.Quit()
            WordApp = Nothing
    Import:

    Microsoft.office.interop et microsoft 12.0.objet library

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Toutes versions] Problème avec macro Excel pour ouvrir Word
    Par lenul78570 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 30/03/2010, 16h25
  2. probléme avec mon GUI Application
    Par lisco dans le forum Qt
    Réponses: 8
    Dernier message: 26/06/2009, 16h19
  3. [MySQL] problème avec mon application de recherche php/mysql
    Par cuisto44000 dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 05/06/2008, 03h01
  4. [Dates] Problème avec mon application
    Par gtraxx dans le forum Langage
    Réponses: 16
    Dernier message: 21/08/2006, 20h59
  5. Problème avec un ansistring - écriture dans Word
    Par scorpion37 dans le forum C++Builder
    Réponses: 11
    Dernier message: 02/05/2006, 15h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo