开发者

dumping word document ( *.doc) to Text?

开发者 https://www.devze.com 2023-01-27 17:12 出处:网络
I\'m looking for help in dumpingword document ( *.doc) to Text? I am using Delphi 2010. If the solution is a component or library, it should be a free or opensource开发者_C百科 component or code libr

I'm looking for help in dumping word document ( *.doc) to Text? I am using Delphi 2010.

If the solution is a component or library, it should be a free or opensource开发者_C百科 component or code library.


you don't need a third party component. check these samples

Using the Range function wich comes with a Text property

uses
ComObj;


function ExtractTextFromWordFile(const FileName:string):string;
var
  WordApp    : Variant;
  CharsCount : integer;
begin
  WordApp := CreateOleObject('Word.Application');
  try
    WordApp.Visible := False;
    WordApp.Documents.open(FileName);
    CharsCount:=Wordapp.Documents.item(1).Characters.Count;//get the number of chars to select
    Result:=WordApp.Documents.item(1).Range(0, CharsCount).Text;//Select the text and retrieve the selection
    WordApp.documents.item(1).Close;
  finally
   WordApp.Quit;
  end;
end;

or using the clipboard, you must select all the doc content, copy to the clipboard and retrieve the data using Clipboard.AsText

uses
ClipBrd,
ComObj;

function ExtractTextFromWordFile(const FileName:string):string;
var
  WordApp    : Variant;
  CharsCount : integer;
begin
  WordApp := CreateOleObject('Word.Application');
  try
    WordApp.Visible := False;
    WordApp.Documents.open(FileName);
    CharsCount:=Wordapp.Documents.item(1).Characters.Count; //get the number of chars to select
    WordApp.Selection.SetRange(0, CharsCount); //make the selection
    WordApp.Selection.Copy;//copy to the clipboard
    Result:=Clipboard.AsText;//get the text from the clipboard
    WordApp.documents.item(1).Close;
  finally
   WordApp.Quit;
  end;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消