Solved

Modify a office document

Posted on 2009-05-20
11
545 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:cypherbad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 24434926
did you know OLE for micro soft office ?  did you see the active X on that issue?  New technology  for you ?
0
 
LVL 1

Author Comment

by:cypherbad
ID: 24435097
Please tell me how to work with it, any examples?
0
 
LVL 8

Expert Comment

by:BdLm
ID: 24436569
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:BdLm
ID: 24436618
http://delphiku.blogspot.com/2007/03/excel-in-delphi-ole.html    for excel , many many more pages with google
0
 
LVL 1

Author Comment

by:cypherbad
ID: 24438852
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.
0
 
LVL 8

Accepted Solution

by:
BdLm earned 500 total points
ID: 24439060
Ok, today we have a holiday, can you wait until sunday, I#ll try for you ....

0
 
LVL 1

Author Closing Comment

by:cypherbad
ID: 31583463
i found the solution myself.
0
 
LVL 8

Expert Comment

by:BdLm
ID: 24444366
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.Application');
    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).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.SaveWordDoc;
begin
    V.Documents.Item(1).SaveAs('c:foo.doc');
end;



procedure TForm1.MailDocument;
begin
  V.Documents.Item(1).SaveAs('c:foo.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;
 


end.


0
 
LVL 8

Expert Comment

by:BdLm
ID: 24444485
pls. readme:


This is just a draft version 0, need a bit more time to answer your request
0
 
LVL 8

Expert Comment

by:BdLm
ID: 24445527
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.

Open in new window

Image2.ee-ole.jpg
0
 
LVL 1

Author Comment

by:cypherbad
ID: 24449709
I managed to get my needs by using this code:

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.

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 90
delphi parse string to params 3 151
drawing animated level bar based on numbers 3 111
Firemonkey DbLookupComboBox equivalent ? 2 66
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question