Commander Word à partir d'Excel

nak

XLDnaute Occasionnel
Bonjour à tous,

Aujourd'hui j'essai de créer un document Word à partir d'Excel.
J'ai donc activé la référence MS Word et j'écris mon docx ligne par ligne. Cela fonctionne plutot bien.

Néanmoins je rencontre un problème avec le BackgroundPatternColor.
Lorsque j'écris mon titre j'applique un fond bleu avec une police blanche. Ensuite je vais à la ligne et j'applique un fond blanc pour écrire un second paragraphe.
Malheureusement le fond s'applique toujours pour mon paragraphe ET mon titre. Je n'arrive pas à différentier les deux.

Voici mon code :
Code:
Sub Passage_Excel_Word()
Dim appWord As New Word.Application
Dim docWord As New Word.Document

' Il faut créer un nouveau document Word dans l'application Word ou ouvrir un existant
With appWord
    .Visible = True
    Set docWord = .Documents.Open(ThisWorkbook.Path & "\essai.docx") 'Set docWord = .Documents.Add
    .Activate
End With

'Dans Word on ajoute une ligne de titre avec une mise en forme
With appWord.Selection
    .TypeText Text:="Chiffre d'affaire 2013"
    .HomeKey Unit:=wdLine
    .EndKey Unit:=wdLine, Extend:=wdExtend
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Font.Size = 18
    .Font.Color = wdColorWhite 'blanc
    .Shading.Texture = wdTextureNone
    .Shading.ForegroundPatternColor = wdColorAutomatic
    .Shading.BackgroundPatternColor = -553582593 'Je mets mon titre sur fond bleu
    
    .EndKey Unit:=wdLine
    .TypeParagraph

    .Font.Color = wdColorAutomatic
    .Font.Size = 12
    .TypeText Text:="essai" 'ICI, je n'arrive pas à écrire mon texte sur fond automatique (blanc)
    
    '.Shading.Texture = wdTextureNone
    '.Shading.ForegroundPatternColor = wdColorAutomatic
    '.Shading.BackgroundPatternColor = wdColorAutomatic


'Copier le tableau Excel dans le presse papier
    Range("a1:b13").Copy
    
' Coller le tableau dans Word avec liaison
    .EndKey Unit:=wdLine
    .TypeParagraph
    .PasteSpecial Link:=True, Placement:=wdInLine ', DisplayAsIcon:=False, DataType:=wdPasteOLEObject

' Remise à zéro du style
    .Shading.Texture = wdTextureNone
    .Shading.ForegroundPatternColor = wdColorAutomatic
    .Shading.BackgroundPatternColor = wdColorAutomatic

'Un autre paragraphe    
    .Font.Color = wdColorAutomatic
    .TypeText Text:="essai"

'Aller au début du document
    .HomeKey Unit:=wdStory

End With

'Réinitialiser l'objet
Set appWord = Nothing

'End With

End Sub


Avez-vous une idée pour mon problème ? Peut-être un saut de section ?

Merci

A+
 

Pièces jointes

  • Excel_Word.zip
    38.4 KB · Affichages: 27

Discussions similaires

Statistiques des forums

Discussions
312 083
Messages
2 085 188
Membres
102 809
dernier inscrit
Sandrine83