Générer fichier word depuis excel

Francky62

XLDnaute Nouveau
Bonjour a tous,

Comme le titre l'indique, j'utilise excel afin de créer et ouvrir un fichier word.

J'utilise ce code :

Code:
Sub CreerOuvrirWord()
Dim NomDoc As String
Dim Chemin As String
Dim appWD As Word.Application
Dim oDoc As Word.Document
nom = Cells(ActiveCell.Row, 3) & " " & " "
NomDoc = nom & ".doc"
If Len(nom) = 1 Then Exit Sub 'si la ligne ne contient ni nom ni prénom on quitte
Chemin = ActiveWorkbook.Path & "\Mon Fichier Clients\" & NomDoc 'à adapter ici répertoire courant
On Error Resume Next
Set appWD = CreateObject("Word.Application")
If Err.Number <> 0 Then
    Set appWD = CreateObject("Word.Application")
End If
On Error GoTo 0
If Dir(Chemin) <> "" Then
    Set oDoc = appWD.Documents.Open(Chemin)
Else
    Set oDoc = appWD.Documents.Add
    With oDoc
        With .Sections(1).Headers(wdHeaderFooterPrimary).Range 'ajout d'un en-tête contenant le nom
            .Font.Bold = True 'en gras
            .Font.Italic = True 'en italique
            .Text = "Fiche de soin de " & nom
        End With
        .SaveAs Chemin
    End With
End If
appWD.Visible = True
appWD.Activate
 
End Sub

La macro fonctionne trés bien.
J'aimerai cependant utiliser un modèle word pour la création.
J'ai donc créé un modèle en .dotx qui s'appelle "Modéle.dotx" , celui-ci se trouvant dans le dossier racine du fichier excel
Je voudrai garder les mêmes conditions de sauvegarde et d’intégration en en-tête lors de la création .

Merci pour l'aide que vous pourrez m'apporter
 
Dernière édition:

Francky62

XLDnaute Nouveau
Re : Générer fichier word depuis excel

Voici le code final, merci quand même de votre aide

Code:
Sub CreerOuvrirWord()
Dim NomDoc As String
Dim Chemin As String
Dim appWD As New Word.Application
Dim oDoc As Word.Document
Dim Modéle As String
Modéle = ThisWorkbook.Path & "\Modéles.dotx"
nom = Cells(ActiveCell.Row, 3) & " " & " "
NomDoc = nom & ".doc"
If Len(nom) = 1 Then Exit Sub 'si la ligne ne contient ni nom ni prénom on quitte
Chemin = ActiveWorkbook.Path & "\Mon Fichier Clients\" & NomDoc 'à adapter ici répertoire courant
On Error Resume Next
Set appWD = CreateObject("Word.Application")
If Err.Number <> 0 Then
    Set appWD = CreateObject("Word.Application")
End If
On Error GoTo 0
If Dir(Chemin) <> "" Then
    Set oDoc = appWD.Documents.Open(Chemin)
Else
    Set oDoc = appWD.Documents.Add(Modéle)
    With oDoc
        With .Sections(1).Headers(wdHeaderFooterPrimary).Range 'ajout d'un en-tête contenant le nom
            .Font.Bold = True 'en gras
            .Font.Italic = True 'en italique
            .Text = "Fiche de soin de " & nom
        End With
        .SaveAs Chemin
    End With
End If
appWD.Visible = True
appWD.Activate
 
End Sub
 

Discussions similaires

Statistiques des forums

Discussions
312 094
Messages
2 085 240
Membres
102 832
dernier inscrit
kirale