Improve company productivity with a Business Account.Sign Up

x
?
Solved

Using MS Word from Delphi

Posted on 2006-07-15
6
Medium Priority
?
774 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 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…

584 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