cypherbad
asked on
Modify a office document
Hello Experts,
I need to make a software that can quick modifications in a microsoft office 2007/2003 (doesn`t matter) or in any other document that supports tables. I want to add in the document at some coordinates or don`t know at the end of the line or what ever, mainly i need to know how to open a office document and edit it trough delphi code only, no manual edits needed. I need to add in the text some values from string variables.
Thank You,
cypherbad
I need to make a software that can quick modifications in a microsoft office 2007/2003 (doesn`t matter) or in any other document that supports tables. I want to add in the document at some coordinates or don`t know at the end of the line or what ever, mainly i need to know how to open a office document and edit it trough delphi code only, no manual edits needed. I need to add in the text some values from string variables.
Thank You,
cypherbad
did you know OLE for micro soft office ? did you see the active X on that issue? New technology for you ?
ASKER
Please tell me how to work with it, any examples?
pls. have a look at http://delphi.about.com/od/kbcontrolole/ss/word_automation.htm
http://delphiku.blogspot.com/2007/03/excel-in-delphi-ole.html for excel , many many more pages with google
ASKER
Ok i`ll give the points for the expert that will give me a code that will open a word document and copy/insert some text in it. Preferable table suported.
Kind Regards,
cypherbad.
Kind Regards,
cypherbad.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i found the solution myself.
unit Unit1_wotrd_ole;
{************************* *****
* http://www.delphipraxis.net/topic55769.html
*
*
************************** ****}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls;
type
TForm1 = class(TForm)
OpenOLEButton1: TButton;
CloseOLeButton: TButton;
Button1: TButton;
procedure OpenOLEButton1Click(Sender : TObject);
procedure CloseOLeButtonClick(Sender : TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
// WordApplication1 : TWordApplication;
V: Variant;
procedure SaveWordDoc;
procedure MailDocument;
procedure CopyChartToWord;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.OpenOLEButton1Click (Sender: TObject);
begin
V := CreateOleObject('Word.Appl ication');
V.Visible := True;
end;
procedure TForm1.CloseOLeButtonClick (Sender: TObject);
begin
if not VarIsEmpty(V) then
V.Quit;
end;
procedure TForm1.Button1Click(Sender : TObject);
var Range: Variant;
i: Integer;
begin
v.Documents.Add;
v.Documents.Item(1).Range;
Range := v.Documents.Item(1).Range;
Range.Text := 'This is a column from a spreadsheet: ';
for i := 1 to 3 do v.Documents.Item(1).Paragr aphs.Add;
Range :=v.Documents.Item(1).Rang e(v.Docume nts.Item(1 ).
Paragraphs.Item(3).Range.S tart);
Range.Paste;
for i := 1 to 3 do v.Documents.Item(1).Paragr aphs.Add;
end;
procedure TForm1.SaveWordDoc;
begin
V.Documents.Item(1).SaveAs ('c:foo.do c');
end;
procedure TForm1.MailDocument;
begin
V.Documents.Item(1).SaveAs ('c:foo.do c');
V.Options.SendMailAttach := True;
V.Documents.Item(1).SendMa il;
end;
procedure TForm1.CopyChartToWord;
var
Range: Variant;
i, NumPars: Integer;
begin
NumPars := V.Documents.Item(1).Paragr aphs.Count ;
Range := V.Documents.Item(1).Range(
V.Documents.Item(1).Paragr aphs.Item( NumPars).R ange.Start ,
V.Documents.Item(1).Paragr aphs.Item( NumPars).R ange.End);
Range.Text := 'This is graph: ';
for i := 1 to 3 do V.Documents.Item(1).Paragr aphs.Add;
Range := V.Documents.Item(1).Range(
V.Documents.Item(1).Paragr aphs.Item( NumPars + 2).Range.Start,
V.Documents.Item(1).Paragr aphs.Item( NumPars + 2).Range.End);
// Range.PasteSpecial(,,,,wdP asteOleObj ect);
end;
end.
{*************************
* http://www.delphipraxis.net/topic55769.html
*
*
**************************
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls;
type
TForm1 = class(TForm)
OpenOLEButton1: TButton;
CloseOLeButton: TButton;
Button1: TButton;
procedure OpenOLEButton1Click(Sender
procedure CloseOLeButtonClick(Sender
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
// WordApplication1 : TWordApplication;
V: Variant;
procedure SaveWordDoc;
procedure MailDocument;
procedure CopyChartToWord;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.OpenOLEButton1Click
begin
V := CreateOleObject('Word.Appl
V.Visible := True;
end;
procedure TForm1.CloseOLeButtonClick
begin
if not VarIsEmpty(V) then
V.Quit;
end;
procedure TForm1.Button1Click(Sender
var Range: Variant;
i: Integer;
begin
v.Documents.Add;
v.Documents.Item(1).Range;
Range := v.Documents.Item(1).Range;
Range.Text := 'This is a column from a spreadsheet: ';
for i := 1 to 3 do v.Documents.Item(1).Paragr
Range :=v.Documents.Item(1).Rang
Paragraphs.Item(3).Range.S
Range.Paste;
for i := 1 to 3 do v.Documents.Item(1).Paragr
end;
procedure TForm1.SaveWordDoc;
begin
V.Documents.Item(1).SaveAs
end;
procedure TForm1.MailDocument;
begin
V.Documents.Item(1).SaveAs
V.Options.SendMailAttach := True;
V.Documents.Item(1).SendMa
end;
procedure TForm1.CopyChartToWord;
var
Range: Variant;
i, NumPars: Integer;
begin
NumPars := V.Documents.Item(1).Paragr
Range := V.Documents.Item(1).Range(
V.Documents.Item(1).Paragr
V.Documents.Item(1).Paragr
Range.Text := 'This is graph: ';
for i := 1 to 3 do V.Documents.Item(1).Paragr
Range := V.Documents.Item(1).Range(
V.Documents.Item(1).Paragr
V.Documents.Item(1).Paragr
// Range.PasteSpecial(,,,,wdP
end;
end.
pls. readme:
This is just a draft version 0, need a bit more time to answer your request
This is just a draft version 0, need a bit more time to answer your request
the excel-copy chart to word -application did not yet work, may be solved on sunday, can you use that code now ?
unit Unit1_wotrd_ole;
{******************************
* http://www.delphipraxis.net/topic55769.html
* http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24424050.html
*
*
******************************}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls;
type
TForm1 = class(TForm)
OpenOLEButton1: TButton;
CloseOLeButton: TButton;
SaveWordButton: TButton;
MailWordButton: TButton;
OpenExcelButton: TButton;
LoadFileButton: TButton;
ExcelBook2WordButton: TButton;
CopyChart2WordButton: TButton;
procedure OpenOLEButton1Click(Sender: TObject);
procedure CloseOLeButtonClick(Sender: TObject);
procedure SaveWordButtonClick(Sender: TObject);
procedure MailWordButtonClick(Sender: TObject);
procedure OpenExcelButtonClick(Sender: TObject);
procedure LoadFileButtonClick(Sender: TObject);
procedure ExcelBook2WordButtonClick(Sender: TObject);
procedure CopyChart2WordButtonClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
// WordApplication1 : TWordApplication;
V : Variant;
ExcelApp : Variant;
procedure SaveWordDoc;
procedure MailDocument;
procedure CopyChartToWord;
procedure CopyExcelCellsToWord(Sender: TObject);
procedure CopyExcelToWord ;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.OpenOLEButton1Click(Sender: TObject);
begin
V := CreateOleObject('Word.Application');
V.Visible := True;
end;
procedure TForm1.CloseOLeButtonClick(Sender: TObject);
begin
if not VarIsEmpty(V) then V.Quit;
end;
procedure TForm1.SaveWordDoc;
begin
V.Documents.Item(1).SaveAs('c:\demo.doc');
end;
procedure TForm1.MailDocument;
begin
V.Documents.Item(1).SaveAs('c:\demo.doc');
V.Options.SendMailAttach := True;
V.Documents.Item(1).SendMail;
end;
procedure TForm1.CopyChartToWord;
var
Range: Variant;
i, NumPars: Integer;
begin
NumPars := V.Documents.Item(1).Paragraphs.Count;
Range := V.Documents.Item(1).Range(
V.Documents.Item(1).Paragraphs.Item(NumPars).Range.Start,
V.Documents.Item(1).Paragraphs.Item(NumPars).Range.End);
Range.Text := 'This is graph: ';
for i := 1 to 3 do V.Documents.Item(1).Paragraphs.Add;
Range := V.Documents.Item(1).Range(
V.Documents.Item(1).Paragraphs.Item(NumPars + 2).Range.Start,
V.Documents.Item(1).Paragraphs.Item(NumPars + 2).Range.End);
// Range.PasteSpecial(,,,,wdPasteOleObject);
end;
procedure TForm1.SaveWordButtonClick(Sender: TObject);
begin
SaveWordDoc;
end;
procedure TForm1.MailWordButtonClick(Sender: TObject);
begin
MailDocument;
end;
procedure TForm1.OpenExcelButtonClick(Sender: TObject);
begin
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.Visible := True;
end;
procedure TForm1.LoadFileButtonClick(Sender: TObject);
begin
ExcelApp.Workbooks.Open('c:\ee_test.xls');
end;
procedure TForm1.CopyExcelToWord;
var
Sheets: Variant;
begin
SetFocus;
Sheets := ExcelApp.Sheets;
Sheets.Item['Data'].Activate;
Sheets.Item['Data'].Range['A1:B7'].Select;
Sheets.Item['Data'].UsedRange.Copy;
CopyExcelCellsToWord(nil);
end;
procedure TForm1.CopyExcelCellsToWord(Sender: TObject);
var Range: Variant;
i: Integer;
begin
v.Documents.Add;
v.Documents.Item(1).Range;
Range := v.Documents.Item(1).Range;
Range.Text := 'Test text ';
for i := 1 to 3 do v.Documents.Item(1).Paragraphs.Add;
Range :=v.Documents.Item(1).Range(v.Documents.Item(1).Paragraphs.Item(3).Range.Start);
Range.Paste;
for i := 1 to 3 do v.Documents.Item(1).Paragraphs.Add;
end;
procedure TForm1.ExcelBook2WordButtonClick(Sender: TObject);
begin
CopyExcelToWord
end;
procedure TForm1.CopyChart2WordButtonClick(Sender: TObject);
var
Sheets: Variant;
begin
SetFocus;
Sheets := ExcelApp.Sheets;
Sheets.Item['Chart1'].Select;
ExcelApp.Selection.Copy;
CopyChartToWord;
end;
end.
Image2.ee-ole.jpg
ASKER
I managed to get my needs by using this code:
Thanks!
Thanks!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ComObj;
{$R *.dfm}
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;
{ Check if file exists }
if not FileExists(ADocument) then
begin
ShowMessage('Specified Document not found.');
Exit;
end;
{ Create the OLE Object }
try
WordApp := CreateOLEObject('Word.Application');
except
on E: Exception do
begin
E.Message := 'Word is not available.';
raise;
end;
end;
try
{ Hide Word }
WordApp.Visible := False;
{ Open the document }
WordApp.Documents.Open(ADocument);
{ Initialize parameters}
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;
{ Perform the search}
if wrfReplaceAll in Flags then
WordApp.Selection.Find.Execute(Replace := wdReplaceAll)
else
WordApp.Selection.Find.Execute(Replace := wdReplaceOne);
{ Save word }
WordApp.ActiveDocument.SaveAs(ADocument);
{ Assume that successful }
Result := True;
{ Close the document }
WordApp.ActiveDocument.Close(wdDoNotSaveChanges);
finally
{ Quit Word }
WordApp.Quit;
WordApp := Unassigned;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Word_StringReplace('C:\c.doc','test2','test3',[wrfReplaceAll]);
end;
end.