[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

Using MS Word from Delphi

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
mhanefitel
Asked:
mhanefitel
  • 3
  • 2
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:

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
 
mhanefitelAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
mhanefitelAuthor Commented:
I increased the point to 500.
0
 
mhanefitelAuthor Commented:
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
 
Russell LibbySoftware Engineer, Advisory Commented:

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

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now