En vbs, est-il possible de modifier le contenu d'un fichier Word

Résolu
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 5 avril 2006 à 21:22
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 6 avril 2006 à 19:12
Bonsoir à tous....

J'ai crée un fichier (trame) .doc dans lequel j'ai remplacé certains mots par "[PROD-total-C]", [PROD-libre-C]...
Je copie ce fichier sous un nouveau nom (.rtf).

FicInput = "d:\ControleRICOH\Trame RICOH - Controle journalier ORACLE.doc"
FicOutput = Replace(Replace(FicInput, "Trame ",""), "ORACLE.doc", "ORACLE du " & Right(Date,4) &_
"-" & Mid(Date, 4, 2) & "-" & Left(Date, 2) & ".rtf")
fso.CopyFile FicInput, FicOutput

Mon souhait serait d'ouvrir ce nouveau fichier (.doc ou .rtf) et
et remplacer les valeurs "[PROD-total-C]", [PROD-libre-C]...
par le contenu de variables issues de mon script vbs.
Celui-ci récupère l'espace disk (total, libre..) de plusieurs disks et lecteurs réseaux.

J'ai parcouru ce forum, exalead et MSDN.
Rien ne m'a inspiré.
Si quelqu'un d'entre-vous a une idée ou une orientation.
Merci.
jean-marc

J'utilise vbs pour ouvrir des .mdb, .txt ou .xls,mais là, c'est la première fois
que je suis confronté à word.
=======================================
mauvaise synthaxe:
Dim oWordApp
Set oWordApp = CreateObject("Word.Application")


Set oDoc = oWordApp.Documents.Open(FicOutput, 1)
'un process winword est ouvert, mais pas mon fichier
=======================================

Do While Not ObjTextStream.AtEndOfStream
strtmp = Split(objTextStream.ReadLine,".")
For i = 0 To UBound(strtmp)
strtmp = Replace(strtmp, "[PROD-total-C]", [PROD-total-C])
strtmp = Replace(strtmp, "[PROD-libre-C]", [PROD-libre-C])
strtmp = Replace(strtmp, "[PROD-total-D]", [PROD-total-D])
strtmp = Replace(strtmp, "[PROD-libre-D]", [PROD-libre-D])
Next
Loop

6 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
6 avril 2006 à 18:33
il suffit de faire :

Dim Word
Dim Doc

Set Word = CreateObject("Word.Application")
Word.DisplayAlerts = wdAlertsNone
Set Doc = Word.Documents.Open("i:\a.doc", True)
If Not Doc Is Nothing Then
Doc.Tables(1).Cell(2, 2).Range.Text = "=Prod-total-C"
Doc.Tables(1).Cell(2, 3).Range.Text = "=Prod-libre-C"
Doc.Tables(1).Cell(3, 2).Range.Text = "=Prod-total-D"
Doc.Tables(1).Cell(3, 3).Range.Text = "=Prod-libre-D"
Doc.Tables(1).Cell(4, 2).Range.Text = "=Prod-total-E"
Doc.Tables(1).Cell(4, 3).Range.Text = "=Prod-libre-E"
Doc.Tables(1).Cell(5, 2).Range.Text = "=Prod-total-F"
Doc.Tables(1).Cell(5, 3).Range.Text = "=Prod-libre-F"
Doc.Tables(1).Cell(6, 2).Range.Text = "=Prod-total-G"
Doc.Tables(1).Cell(6, 3).Range.Text = "=Prod-libre-G"
Doc.Save
MsgBox "Terminé", vbInformation
End If
Word.Quit

pour le mettre au point, a moins d'avoir VB6 sous la main, utilise l'environnement VBA (Alt + F11 dans Word)
en tatonnant un peu , tu arriveras a tes fins

Renfield
Admin CodeS-SourceS - MVP Visual Basic
3
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
6 avril 2006 à 09:50
pourquoi joues tu avec un TextStream... ?

Renfield
Admin CodeS-SourceS - MVP Visual Basic
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
6 avril 2006 à 09:59
il suffit de faire :

Dim Word
Dim Doc

Set Word = CreateObject("Word.Application")
Word.DisplayAlerts = wdAlertsNone

Set Doc = Word.Documents.Open("i:\a.doc", True)
If Not Doc Is Nothing Then
Doc.Content = Replace(Doc.Content, "[PROD-total-C]", TotalC)
Doc.Save
MsgBox "Terminé", vbInformation
End If
Word.Quit

Renfield
Admin CodeS-SourceS - MVP Visual Basic
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
6 avril 2006 à 17:12
Bonjour à tous....

Merci Renfield,
Je vais testé. Je sais que TextStream n'est pas la bonne synthaxe,
mais je ne connais pas les fonctions VBA de Word.
Je vais regarder l'help de Word.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
6 avril 2006 à 18:02
Bonjour,

Renfield, avec ta synthaxe, les zones en variables ont bien été remplacées,
mais j'ai perdu la présentation de mon document !!!

Fichier avant modif:

ESPACE LIBRE SIBL-DB-PROD<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />


parcourrrir


PARTITION

,
ESPACE TOTAL

,
ESPACE DISPO

,
COMMENTAIRES

,
----

C : (SYSTEM)

,
[PROD-total-C]

,
[PROD-libre-C]

,


,
----

D : (SWAP)

,
[PROD-total-D]

,
[PROD-libre-D]

,
,
----

E : (ARCHIVELOG)

,
[PROD-total-E]

,
[PROD-libre-E]

,
Signaler si espace libre < 500Mo (archives)

Le vendredi : signaler si < 650Mo (archives)

,
----

F: (DATA)

,
[PROD-total-F]

,
[PROD-libre-F]

,

,
----

G: (INDEX)

,
[PROD-total-G]

,
[PROD-libre-G]



fichier après modif:





ESPACE LIBRE SIBL-DB-PROD


parcourrrir


PARTITION


ESPACE TOTAL


ESPACE DISPO


COMMENTAIRES





C : (SYSTEM)


10 Go ' c'est bien le contenu de ma variable PROD_total_C


8 Go ' c'est bien le contenu de ma variable PROD_libre_C








D : (SWAP)


27 Go


10 Go








E : (ARCHIVELOG)


[PROD-total-E]


[PROD-libre-E]


Signaler si espace libre < 500Mo (archives)


Le vendredi : signaler si < 650Mo (archives)





F: (DATA)



Donc, la solution finale ne doit pas être trop eloignée.
Je suis optimiste.

jean-marc
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
6 avril 2006 à 19:12
Re,

Merci Renfield.
Je n'ai plus qu'à regarder l'aide dans la doc,car j'ai plusieurs tableaux à renseigner
ainsi qu'à remplacer des zones texte.

Résultat obtenu:

<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />





ESPACE LIBRE SIBL-DB-PROD


parcourrrir


PARTITION

,
ESPACE TOTAL

,
ESPACE DISPO

,
COMMENTAIRES

,
----

C : (SYSTEM)

,
9,76 Go

,
2,19 Go

,


,
----

D : (SWAP)

,
27,48 Go

,
10,59 Go
0
Rejoignez-nous