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

VBA Word Discussion :

Ajouter un paragraphe à mon doc


Sujet :

VBA Word

  1. #1
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Mai 2009
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2009
    Messages : 54
    Points : 49
    Points
    49
    Par défaut Ajouter un paragraphe à mon doc
    Bonjour à tous,

    J'ai le code suivant :
    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
    'Déclaration des variables
        Public nom As String
        Public NomDoc As String
        Public chemin As String
        Public appWD As Word.Application
        Public oDoc As Word.Document
        Public nparagraphe As Integer
        Public date_t As String
        Public debug1 As Integer
     
     
    Private Sub createline()
     
        nparagraphe = nparagraphe + 1
     
        If nparagraphe = 1 Then
            MsgBox "Debug1 / nparagraphe vaut : " & nparagraphe
            'On créer l'objet et on vérifie sont existance
            Set appWD = CreateObject("Word.Application")
            If Err.Number <> 0 Then
                Set appWD = CreateObject("Word.Application")
            End If
            Set oDoc = appWD.Documents.Add
            With oDoc
                With .Paragraphs(1).Range
                    .Font.Bold = True 'en gras
                    .Font.Italic = True 'en italique
                    .Hyperlinks.Add Anchor:=.Paragraphs(nparagraphe).Range, _
                        Address:="http://toto.com", _
                        ScreenTip:="totobulle" & nparagraphe, _
                        TextToDisplay:="totolink" & nparagraphe
                End With
                .SaveAs chemin
                .Close
            End With
     
            With appWD
                .Quit
            End With
     
        Else
            MsgBox "Debug2 / nparagraphe vaut : " & nparagraphe
            Set appWD = CreateObject("Word.Application")
            If Err.Number <> 0 Then
                Set appWD = CreateObject("Word.Application")
            End If
            Set oDoc = appWD.Documents.Open(chemin)
            With oDoc
                With .Paragraphs(1).Range
                    .Font.Bold = True 'en gras
                    .Font.Italic = True 'en italique
                    debug1 = ActiveDocument.BuiltinDocumentProperties(wdPropertyParas)
                    MsgBox "debug1 total 1 : " & debug1
                    .InsertAfter vbLf
                    debug1 = ActiveDocument.BuiltinDocumentProperties(wdPropertyParas)
                    MsgBox "debug1 total 2 : " & debug1
                    .Hyperlinks.Add Anchor:=.Paragraphs(nparagraphe).Range, _
                        Address:="http://toto1.com", _
                        ScreenTip:="totobulle" & nparagraphe, _
                        TextToDisplay:="totolink" & nparagraphe
                End With
                .SaveAs chemin
                .Close
            End With
     
            With appWD
                .Quit
            End With
     
        End If
     
    End Sub
     
    Sub Test()
     
     
     
        'Initialisation variables
        nom = ""
        NomDoc = ""
        chemin = ""
        nparagraphe = 0
        date_t = ""
     
     
        'Remplissage des variables
        date_t = Format(Now(), "dd.mm.yy_hh.mm.ss")
        nom = "ListeLiens" & "-" & date_t
        NomDoc = nom & ".doc"
        nparagraphe = 0
     
        'Vérification du nom pour etre sur qu'il n'est pas vide
        If Len(nom) = 1 Then
            MsgBox "Le nom est vide"
            Exit Sub 'si la ligne ne contient ni nom ni prénom on quitte
        End If
     
        'On indique le dossier d'enregistrement du .doc
        chemin = "P:\" & NomDoc
     
        'On appelle la fonction d'ecriture du document
        For i = 1 To 5
             createline
        Next
     
        'On reinitialise les variables
        nom = ""
        NomDoc = ""
        chemin = ""
        nparagraphe = 0
        date_t = ""
     
    End Sub
    qui me fait sans cesse le message d'erreur suivant :
    Nom : Erreur_create_word.PNG
Affichages : 302
Taille : 6,0 Ko

    Cela est du au fait que le nombre de paragraphes ne s’incrémente pas... Ma variable debug1 le prouve... Elle reste figée à "1"...

    J'ai tenté de remplacer ".InsertAfter vbLf" par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    .Paragraphs.Add
    .InsertParagraphAfter
    .InsertAfter vbCrLf
    Mais rien à faire ce nombre ne s'incrémente pas... Pouvez-vous m'aidez s'il vous plaît ? Merci

    PS1 : Le doc word qui se créé a bien une ligne de plus pourtant...
    PS2 : C'est une macro faite dans Excel... Le but est de générer un .doc à partir de mon excel... Les .markbook ne m’intéresse pas car je ne sais pas à l'avance combien de boucle va faire ma boucle for à l'avenir.

  2. #2
    Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Mai 2009
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2009
    Messages : 54
    Points : 49
    Points
    49
    Par défaut
    En fait j'ai fini par trouver !

    J'appelais le paragraphes 3 dans le paragraphe 3 du coup cela plantait...

    Bref voici le code corrigé :
    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
    'Déclaration des variables
        Public nom As String
        Public NomDoc As String
        Public chemin As String
        Public appWD As Word.Application
        Public oDoc As Word.Document
        Public nparagraphe As Integer
        Public date_t As String
     
     
    Private Sub createline()
     
        nparagraphe = nparagraphe + 1
     
        If nparagraphe = 1 Then
            'On créer l'objet et on vérifie sont existance
            Set appWD = CreateObject("Word.Application")
            If Err.Number <> 0 Then
                Set appWD = CreateObject("Word.Application")
            End If
            Set oDoc = appWD.Documents.Add
            With oDoc
                With .Paragraphs(nparagraphe).Range
                    .Font.Bold = True 'en gras
                    .Font.Italic = True 'en italique
                    .Hyperlinks.Add Anchor:=.Paragraphs(1).Range, _
                        Address:="http://toto.com", _
                        ScreenTip:="totobulle" & nparagraphe, _
                        TextToDisplay:="totolink" & nparagraphe
                End With
                .Paragraphs.Add
                .SaveAs chemin
                .Close
            End With
     
            With appWD
                .Quit
            End With
     
        Else
            Set appWD = CreateObject("Word.Application")
            If Err.Number <> 0 Then
                Set appWD = CreateObject("Word.Application")
            End If
            Set oDoc = appWD.Documents.Open(chemin)
            With oDoc
                With .Paragraphs(nparagraphe).Range
                    .Font.Bold = True 'en gras
                    .Font.Italic = True 'en italique
                    '.Text = "bbb"
                    .Hyperlinks.Add _
                        Anchor:=.Paragraphs(1).Range, _
                        Address:="http://toto1.com", _
                        ScreenTip:="totobulle" & nparagraphe, _
                        TextToDisplay:="totolink" & nparagraphe
                End With
                .Paragraphs.Add
                .SaveAs chemin
                .Close
            End With
     
            With appWD
                .Quit
            End With
     
     
        End If
     
    End Sub
     
    Sub Test()
     
     
     
        'Initialisation variables
        nom = ""
        NomDoc = ""
        chemin = ""
        nparagraphe = 0
        date_t = ""
        nbparagraphes = 0
     
     
        'Remplissage des variables
        date_t = Format(Now(), "dd.mm.yy_hh.mm.ss")
        nom = "ListeLiens" & "-" & date_t
        NomDoc = nom & ".docx"
        nparagraphe = 0
     
        'Vérification du nom pour etre sur qu'il n'est pas vide
        If Len(nom) = 1 Then
            MsgBox "Le nom est vide"
            Exit Sub 'si la ligne ne contient ni nom ni prénom on quitte
        End If
     
        'On indique le dossier d'enregistrement du .doc
        chemin = "P:\" & NomDoc
     
            For i = 1 To 10
                createline
            Next
     
        'End If
        'appWD.Visible = True
        'appWD.Activate
     
        'On reinitialise les variables
        nom = ""
        NomDoc = ""
        chemin = ""
        nparagraphe = 0
        date_t = ""
     
    End Sub
    Merci à tous de votre aide précieuse.

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

Discussions similaires

  1. Ajouter des onglets à mon editeur de texte
    Par diden138 dans le forum Delphi
    Réponses: 18
    Dernier message: 12/11/2006, 21h02
  2. [VBA-E] Macro VBA pour personaliser mon .doc depuis mon .xls
    Par korntex5 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 22/05/2006, 16h01
  3. Comment ajouter une photo à mon profil ?
    Par ghita269 dans le forum Mode d'emploi & aide aux nouveaux
    Réponses: 5
    Dernier message: 18/11/2005, 20h36
  4. Réponses: 1
    Dernier message: 20/04/2005, 16h05
  5. [Fichier] Ajout des lignes de doc dans arraylist
    Par 3adoula dans le forum Entrée/Sortie
    Réponses: 9
    Dernier message: 29/04/2004, 22h41

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