Editer document Word

PhilLu Messages postés 251 Date d'inscription lundi 9 novembre 2009 Statut Membre Dernière intervention 11 mai 2021 - 10 nov. 2015 à 09:31
 NABIL74 - 11 nov. 2015 à 20:35
Bonjour,
Je souhaite éditer un document word automatiquement.
Cela marche très bien, mais après modification, j'obtient un nouveau document Word 'document1', j'aimerais sauver automatiquement ce document en lui attribuant un nom.

procedure TForm1.Button11Click(Sender: TObject);
var
Fword,FDocument,FFindObject:OleVariant;
Filename:String;

begin
Filename := 'C:\temp\A_Editer.doc';
Fword := CreateOleObject('Word.Application');
FDocument := Fword.Documents.Add(Filename);
FFindObject := FDocument.ActiveWindow.Selection.Find;
Fword.visible := true;
FFindObject.ClearFormatting;
FFindObject.Replacement.ClearFormatting;
FFindObject.Text := 'xxx2'; //balise qui sera remplacée par le texte du 'test.rtf'
FFindObject.Forward := True;
FFindObject.Replacement.Text := '';
FFindObject.Wrap := 1;
FFindObject.MatchCase := False;
FFindObject.MatchWholeWord := False;
FFindObject.MatchWildcards := False;
FFindObject.MatchSoundsLike := False;
FFindObject.MatchAllWordForms := False;

if FFindObject.Execute() then Fword.selection.InsertFile('C:\temp\test.rtf');
end;


Merci pour votre aide ;)
PhilLu

1 réponse

Le code suivant recherche un String dans un document Word et le remplace par un autre String:
    Type
TWordReplaceFlags = set of (wrfReplaceAll, wrfMatchCase, wrfMatchWildcards);

Function Word_StringReplace(ADocument: TFileName; SearchString, ReplaceString: string; Flags: TWordReplaceFlags): Boolean;
const
wdFindContinue = 1;
wdReplaceOne = 1;
wdReplaceAll = 2;
wdDoNotSaveChanges = 0;
var
WordApp: OLEVariant;
begin
Result := False;


if not FileExists(ADocument) then
begin
ShowMessage('Le fichier spécifié est introuvable.');
Exit;
end;

try
WordApp := CreateOLEObject('Word.Application');
except
on E: Exception do
begin
E.Message := 'Word n''est pas disponible.';
raise;
end;
end;

try

WordApp.Visible := true;

WordApp.Documents.Open(ADocument);

WordApp.Selection.Find.ClearFormatting;
WordApp.Selection.Find.Text := SearchString;
WordApp.Selection.Find.Replacement.Text := ReplaceString;
WordApp.Selection.Find.Forward := True;
WordApp.Selection.Find.Wrap := wdFindContinue;
WordApp.Selection.Find.Format := False;
WordApp.Selection.Find.MatchCase := wrfMatchCase in Flags;
WordApp.Selection.Find.MatchWholeWord := False;
WordApp.Selection.Find.MatchWildcards := wrfMatchWildcards in Flags;
WordApp.Selection.Find.MatchSoundsLike := False;
WordApp.Selection.Find.MatchAllWordForms := False;

if wrfReplaceAll in Flags then
WordApp.Selection.Find.Execute(Replace := wdReplaceAll)
else
WordApp.Selection.Find.Execute(Replace := wdReplaceOne);

WordApp.ActiveDocument.SaveAs(ADocument); // Sauvegarde du document

Result := True;

WordApp.ActiveDocument.Close(wdDoNotSaveChanges);
finally

WordApp.Quit; / Quitter Word
WordApp := Unassigned;
end;
end;

//----------------------------------------------------------------------------
Utilisation :

procedure TForm1.LMDButton1Click(Sender: TObject);
begin
Word_StringReplace('C:\Test.doc' , 'xxx2' , 'Nouveau mot' , [wrfReplaceAll]); // Test.doc ou Test.Rtf
end;
0
Rejoignez-nous