Solved

Using MS Word from Delphi

Posted on 2006-07-15
6
652 Views
Last Modified: 2010-04-05
Hi To All,

     I am trying to use MS Word from delphi. I can open, get text and close  the MS Word from Delphi Application. My only need is to get everything in MS Word that has been written. For example when i copy something to MS Word after opening the MS Word from Delphi  (Like pictures, formatted text etc.) and get the content of MS Word from delphi i lose the formats of the contents of the MS Word. The reason i need contents of the MS Word is to store this contents of the MS Word  in a database and then later i might load them into MS Word.
     How to owercome this issue?
    Thanks in advance.
0
Comment
Question by:mhanefitel
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:calinutz
ID: 17114537
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17115088

Have you also though about using the TOleContainer control to allow your users to perform the editing of the word document?

It exposes savetofile/stream and loadfromfile/stream methods that allow you to persist and load the OLE stream (containing the word document), and makes loading/saveing the OLE data into a database much easier, as a blob field can be used to load/save  the OLE content stream.

Just a thought,
Russell
0
 

Author Comment

by:mhanefitel
ID: 17120032
Hi To All,

     Below is the code that i have tried and is not working. I need working code.
    Thanks in advance.
var T : String;
WorApplication, WordDOcument : OLEVariant;

procedure tform1.Button1Click(Sender : TObject);
begin
    try
       WordApplication := CreateOleObject('Word.Application') ;
       WordDocument := WordApplication.Documents.Add;
       WordApplication.Visible := True;
    except
    end;
end;

procedure tform1.Button2Click(Sender : TObject);
begin
try
        WordApplication.ActiveDocument.Range.Copy;
        T := WordApplication.ActiveDocument.Range.PASTE;
except
end;
end;
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mhanefitel
ID: 17127688
I increased the point to 500.
0
 

Author Comment

by:mhanefitel
ID: 17143749
Hi rllibby,

     Could you give me an example how to load/save worddocument from olecontainer and then save the word document  to MS SQL Server Database.
     Thanks in advance.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17148921

Yes, I can show you how the load / save streams work, but I am keeping the example to the basics so you will be understand how it works. I want to provide a simple working example that you can test / run, and don't want to complicate it with database stuff that would require extra code and not really have anything to do with the real problem.

But I will tell you this; integrating the code into a DB will require only minor work, as BLOB fields expose a LoadFromStream/SaveToStream, which would be the replacement for the FStreams array in my example.

Let me know if you question / problems,
Russell


Now back to the example...

-- source --
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComObj, ActiveX, OleCtnrs, StdCtrls, ExtCtrls;

type
  TForm1            = class(TForm)
     oxWord:        TOleContainer;
     Button1:       TButton;
     Button2:       TButton;
     procedure      FormCreate(Sender: TObject);
     procedure      FormDestroy(Sender: TObject);
     procedure      Button2Click(Sender: TObject);
     procedure      Button1Click(Sender: TObject);
  private
     // Private declarations
     FStreams:      Array [0..5] of TMemoryStream;
     FIndex:        Integer;
  public
     // Public declarations
     procedure      UpdateState;
  end;

var
  Form1:            TForm1;

implementation
{$R *.DFM}

procedure TForm1.UpdateState;
begin

  // Set caption
  Caption:=Format('DOC %d', [FIndex]);

  // Update the button states as well as the OLE doc stream
  Button1.Enabled:=(FIndex > 0);
  Button2.Enabled:=(FIndex < High(FStreams));

  // Resource protection
  try
     // Check stream
     if (FStreams[FIndex].Size = 0) then
     begin
        // Create new object
        oxWord.CreateObject('Word.Document', True);
        // Set paper size to letter
        oxWord.OleObject.PageSetup.PaperSize:=2;
     end
     else
     begin
        FStreams[FIndex].Position:=0;
        oxWord.LoadFromStream(FStreams[FIndex]);
     end;
  finally
     // Do activation
     oxWord.DoVerb(ovPrimary);
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
var  dwIndex:       Integer;
begin

  // Set index
  FIndex:=0;

  // Create memory streams for example
  for dwIndex:=0 to High(FStreams) do FStreams[dwIndex]:=TMemoryStream.Create;

  // Update state of application
  UpdateState;

end;

procedure TForm1.FormDestroy(Sender: TObject);
var  dwIndex:       Integer;
begin

  // Free memory streams
  for dwIndex:=0 to High(FStreams) do FStreams[dwIndex].Free;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin

  // Clear current stream
  FStreams[FIndex].Clear;
  try
     // Save to stream
     oxWord.SaveToStream(FStreams[FIndex]);
     // Increment index
     Inc(FIndex);
  finally
     // Update state
     UpdateState;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  // Clear current stream
  FStreams[FIndex].Clear;
  try
     // Save to stream
     oxWord.SaveToStream(FStreams[FIndex]);
     // Increment index
     Dec(FIndex);
  finally
     // Update state
     UpdateState;
  end;

end;

end.

-- dfm --
object Form1: TForm1
  Left = 412
  Top = 321
  Width = 186
  Height = 248
  Caption = 'My Default Form'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object oxWord: TOleContainer
    Left = 8
    Top = 40
    Width = 157
    Height = 165
    AllowInPlace = False
    AllowActiveDoc = False
    Caption = 'oxWord'
    SizeMode = smScale
    TabOrder = 0
  end
  object Button1: TButton
    Left = 8
    Top = 8
    Width = 77
    Height = 25
    Caption = 'Previous'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 88
    Top = 8
    Width = 77
    Height = 25
    Caption = 'Next'
    TabOrder = 2
    OnClick = Button2Click
  end
end
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now