Page 1 sur 1
[R.] Word 2007 - Insérer tableau objet COM
Posté : mer. 06 mai 2015 17:01
par jcaspar
Bonjour à tous
A présent que je sais comment modifier les styles je souhaiterais pouvoir insérer un tableau dans Word
j'ai donc suivi vos précédents conseils et créer une macro puis j'ai récupéré le code ...
mais je rame pour parvenir à la conversion autoit ....
Je vous remercie d'avance pour vos idées !
Jean-Marc
Code : Tout sélectionner
#include <Word.au3>
#include <MsgBoxConstants.au3>
local $oDoc
local $nom = "Le nom d'utilisateur est " & @username
$word = ObjCreate("Word.Application")
$word.visible = True
$word.Selection.WholeStory
$word.Documents.Add
$word.Selection.TypeText("test" & $nom)
$word.Selection.WholeStory
$word.Selection.Style = "Titre"
$word.Selection.insert
$word.Selection.TypeText($nom)
$word.Selection.Style = "Titre 1"
$word.Selection.TypeParagraph
$word.Selection.TypeText("Bonjour")
$word.Selection.Style = "Titre 2"
$word.Selection.TypeParagraph
$word.Selection.TypeText("Au revoir")
$word.Selection.Style = "Titre 3"
$word.Selection.TypeParagraph
$word.Selection.TypeText("fin du sujet")
$word.Selection.Style = "Titre 4"
$word.Selection.TypeParagraph
[b]$word.ActiveDocument.Tables.Add ($word.Selection.Range, 2,5, "wdWord9TableBehavior", "wdAutoFitFixed")
With $word.Selection.Tables(1)
.Columns.PreferredWidth = $word.CentimetersToPoints(1.8)
If .Style <> "Grille du tableau" Then
.Style = "Grille du tableau"
EndIf
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
EndWith
$word.Selection.MoveDown ("wdLine", 3)[/b]
Re: [..] Word 2007 - Insérer tableau objet COM
Posté : mer. 06 mai 2015 18:55
par walkson
Vous ne pouvez pas écrire les constants comme ceci:
jcaspar a écrit :$word.Selection.MoveDown ("wdLine", 3)
Ou vous donnez la valeur de la constants ou vous déclarez la constant. En générale, elles s'écrivent par ex:
Global Const $wdLine = ... Donc toutes les constants commencent par $
Je vous renvoie à une précédente réponse
http://autoitscript.fr/forum/viewtopic. ... 422#p93854 ou vous trouverez pas mal de manière pour créer et configurer un tableau Word et un lien vers un UDF de constants.
Si vous l'utilisez, ne mettez pas #include<Word.au3> car vous aurez des déclarations en double, donc bug ! (Word.au3 n'est pas utile dans votre cas car vous passez par $word = ObjCreate("Word.Application") )
Re: [..] Word 2007 - Insérer tableau objet COM
Posté : jeu. 07 mai 2015 11:41
par jcaspar
M

erci beaucoup pour ces conseils !
Cela reviendrait à créer du code sous la forme suivante si je comprends bien .... bon j'ai encore du travail sur la planche pour tout comprendre
mais grâce à vos conseils j'avance petit à petit...
Jean-Marc
Code : Tout sélectionner
local $nom = "Le nom d'utilisateur est " & @username
local $ip = "L'adresse IP du PC est :" & @IPAddress1
local $domaine = "Vous êtes connecté au domaine:" & @LogonDomain
local $serveur = "Vous êtes connecté au serveur:" & @LogonServer
local $os = "Votre système d'exploitation est le suivant :" & @OSVersion & @LF & " La langue est " & @OSlang & @LF & "Le service Pack est: " & @OSServicePack
global Const $vbTab = 1
global Const $wdLine = 3
global const $NumRows = 6
global const $NumColumns = 10
global const $Range = "MyRange"
global const $DefaultTableBehavior = "wdWord9TableBehavior"
global const $AutoFitBehavior = " wdAutoFitFixed"
global const $Unit = "wdLine"
global const $Count = 5
$word = ObjCreate("Word.Application")
$word.visible = True
$word.Selection.WholeStory
$word.Documents.Add
$word.Selection.TypeText($nom)
$word.Selection.WholeStory
$word.Selection.Style = "Titre"
$word.Selection.insert
$word.Selection.TypeText($ip)
$word.Selection.Style = "Titre 1"
$word.Selection.TypeParagraph
$word.Selection.TypeText($nom)
$word.Selection.Style = "Titre 2"
$word.Selection.TypeParagraph
$word.Selection.TypeText($domaine)
$word.Selection.Style = "Titre 3"
$word.Selection.TypeParagraph
$word.Selection.TypeText($serveur)
$word.Selection.Style = "Titre 4"
$word.Selection.TypeParagraph
$word.Selection.TypeText($os)
$word.Selection.Style = "Titre 5"
$word.Selection.TypeParagraph
$word.Selection.TypeText($vbTab)
$word.Selection.EndOf(2, 0)
$word.Selection.MoveDown ($wdline)
$word.ActiveDocument.Tables.Add($word.Selection.Range, $NumRows, $NumColumns, $DefaultTableBehavior, $AutoFitBehavior)
With $word.Selection.Tables(1)
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
EndWith
$word.Selection.MoveDown($Unit, $Count)
$word.Selection.TypeParagraph
$word.ActiveDocument.Tables.Add($Range, $NumRows, $NumColumns)
Re: [..] Word 2007 - Insérer tableau objet COM
Posté : jeu. 07 mai 2015 19:47
par walkson
Bonsoir,
Vous mélangez variable et constant.
► Afficher le texte
Code : Tout sélectionner
local $nom = "Le nom d'utilisateur est " & @username
local $ip = "L'adresse IP du PC est :" & @IPAddress1
local $domaine = "Vous êtes connecté au domaine:" & @LogonDomain
local $serveur = "Vous êtes connecté au serveur:" & @LogonServer
local $os = "Votre système d'exploitation est le suivant :" & @OSVersion & @LF & " La langue est " & @OSlang & @LF & "Le service Pack est: " & @OSServicePack
Local $vbTab = 1
global Const $wdLine = 3 ;est une constant qui se réfère à l'unité utilisée
global Const $wdRow = 10
Local $NumRows = 6 ;est une variable comme $nom = "toto"
Local $NumColumns = 10
Global Const $wdMove = 0
global Const $wdWord9TableBehavior = 1
global Const $wdAutoFitFixed = 0
Local $Unit = 2
Local $Count = 5
;global const $Range = "MyRange" => Range se réfère à une sélection => $Range = $word.Selection.Range
$word = ObjCreate("Word.Application")
$word.visible = True
$word.Selection.WholeStory
$word.Documents.Add
$word.Selection.TypeText($nom)
$word.Selection.WholeStory
$word.Selection.Style = "Titre"
$word.Selection.insert
$word.Selection.TypeText($ip)
$word.Selection.Style = "Titre 1"
$word.Selection.TypeParagraph
$word.Selection.TypeText($nom)
$word.Selection.Style = "Titre 2"
$word.Selection.TypeParagraph
$word.Selection.TypeText($domaine)
$word.Selection.Style = "Titre 3"
$word.Selection.TypeParagraph
$word.Selection.TypeText($serveur)
$word.Selection.Style = "Titre 4"
$word.Selection.TypeParagraph
$word.Selection.TypeText($os)
$word.Selection.Style = "Titre 5"
$word.Selection.TypeParagraph
$word.Selection.TypeText($vbTab)
$word.Selection.EndOf(2, 0)
$word.Selection.MoveDown ($wdline)
;expression.Add(Étendue, NumLignes, NumColonnes, ComportementTableauParDéfaut, ComportementAjustAuto)
;expression.Add(Sélection, variable, variable, 2 contantes, constant WdAutoFitBehavior.)voir aide VBA "Tables.Add"
$word.ActiveDocument.Tables.Add($word.Selection.Range, $NumRows, $NumColumns, $wdWord9TableBehavior , Default)
With $word.Selection.Tables(1)
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
EndWith
$word.ActiveDocument.Content.InsertParagraphAfter
;$word.Selection.MoveDown($wdline, 6, $wdMove); pas réussi à faire fonctionner
$word.Selection.Move($wdline, 61) ;chaque cell est comme une ligne. Y a surement une meilleure méthode
$word.ActiveDocument.Tables.Add($word.Selection.Range, $NumRows, $NumColumns)
Word n'est pas évident pour se déplacer dans un texte ou simplement de savoir où on en est (la sélection)
Si ca peut vous consoler, moi aussi je rame des fois....

Re: [..] Word 2007 - Insérer tableau objet COM
Posté : jeu. 07 mai 2015 22:45
par jcaspar
M
erci pour vos conseils !
voici mes avancées le code fonctionne sous Word 2007 je coince encore pour modifier la couleur des cellules du tableau.... mais bon j'avance doucement mais surement
ensuite je voudrais pouvoir insérer des formes ... ce n'est pas forcément évident mais cet apprentissage permet d'étendre les possibilités à Excel, Outlook, Powerpoint etc...
Jean-Marc
Code : Tout sélectionner
[spoiler=]
local $nom = "Le nom d'utilisateur est " & @username
local $ip = "L'adresse IP du PC est :" & @IPAddress1
local $domaine = "Vous êtes connecté au domaine:" & @LogonDomain
local $serveur = "Vous êtes connecté au serveur:" & @LogonServer
local $os = "Votre système d'exploitation est le suivant :" & @OSVersion & @LF & " La langue est " & @OSlang & @LF & "Le service Pack est: " & @OSServicePack
Global $wdColorRed
$word = ObjCreate("Word.Application")
$word.visible = True
$word.Selection.WholeStory
$word.Documents.Add
$word.Selection.TypeText($nom)
$word.Selection.WholeStory
$word.Selection.Style = "Titre"
$word.Selection.insert
$word.Selection.TypeText($ip)
$word.Selection.Style = "Titre 1"
$word.Selection.TypeParagraph
$word.Selection.TypeText($nom)
$word.Selection.Style = "Titre 2"
$word.Selection.TypeParagraph
$word.Selection.TypeText($domaine)
$word.Selection.Style = "Titre 3"
$word.Selection.TypeParagraph
$word.Selection.TypeText($serveur)
$word.Selection.Style = "Titre 4"
$word.Selection.TypeParagraph
$word.Selection.TypeText($os)
$word.Selection.Style = "Titre 5"
$word.Selection.TypeParagraph
;~ tableau automatique
$word.ActiveDocument.Tables.Add ($word.Selection.Range, 5,7)
With $word.Selection.Tables(1)
If .Style <> "Grille du tableau" Then
.Style = "Grille du tableau"
EndIf
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
EndWith
for $i=0 to 34 step 1
$j=$i+5
$word.Selection.TypeText ("La valeur de la cellule "& $i& "est de" &$j)
sleep(100)
$word.Selection.MoveRight (1)
$word.Selection.Font.Color = $wdColorRed
$word.Selection.Font.Color ($wdColorRed)
Next
$word.Selection.TypeParagraph
$word.Selection.TypeText(" ")
;~ tableau manuel
$word.ActiveDocument.Tables.Add ($word.Selection.Range, 5,7)
With $word.Selection.Tables(1)
If .Style <> "Grille du tableau" Then
.Style = "Grille du tableau"
EndIf
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
EndWith
$word.Selection.TypeText("Leo")
$word.Selection.Font.ColorIndex = 10
$word.Selection.MoveRight (1)
$word.Selection.TypeText("Firmin")
$word.Selection.Font.Color(5)
$word.Selection.MoveRight (1)
$word.Selection.Font.Color = "wdColorRed"
$word.Selection.TypeText ("Louis")
$word.Selection.MoveRight (1)
$word.Selection.TypeText("Elise")
$word.Selection.MoveRight(1)
$word.Selection.TypeText ("Lucie")
$word.Selection.MoveRight(1)
$word.Selection.TypeText ("Tom")
$word.Selection.MoveRight(1)
$word.Selection.TypeText ("Jules")
$word.Selection.MoveRight(1)
;~ $word.Selection.MoveLeft
;~ $word.Selection.MoveDown
$word.Selection.TypeText ("Lupita")
;~ $word.ActiveDocument.Tables(1).Cell(1, 1).Range.Shading.BackgroundPatternColor("wdColorAqua")
[/spoiler]
Re: [..] Word 2007 - Insérer tableau objet COM
Posté : ven. 08 mai 2015 08:06
par jguinch
@jcaspar : merci d'utiliser la balise Spoiler pour le code

Re: [..] Word 2007 - Insérer tableau objet COM
Posté : ven. 08 mai 2015 11:55
par jcaspar

oups désolé pour cet oubli voilà qui est corrigé

!