?
Solved

Modify a office document

Posted on 2009-05-20
11
Medium Priority
?
552 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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