Solved

Modify a office document

Posted on 2009-05-20
11
541 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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