• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

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
0
cypherbad
Asked:
cypherbad
  • 7
  • 4
1 Solution
 
BdLmCommented:
did you know OLE for micro soft office ?  did you see the active X on that issue?  New technology  for you ?
0
 
cypherbadAuthor Commented:
Please tell me how to work with it, any examples?
0
 
BdLmCommented:
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
BdLmCommented:
http://delphiku.blogspot.com/2007/03/excel-in-delphi-ole.html    for excel , many many more pages with google
0
 
cypherbadAuthor Commented:
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
 
BdLmCommented:
Ok, today we have a holiday, can you wait until sunday, I#ll try for you ....

0
 
cypherbadAuthor Commented:
i found the solution myself.
0
 
BdLmCommented:
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
 
BdLmCommented:
pls. readme:


This is just a draft version 0, need a bit more time to answer your request
0
 
BdLmCommented:
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
 
cypherbadAuthor Commented:
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now