将Word文档(* .doc)转储为文本? [英] dumping word document ( *.doc) to Text?

查看:97
本文介绍了将Word文档(* .doc)转储为文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Word文档(* .doc)转储到Text方面寻求帮助吗?我正在使用Delphi 2010.

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 component or code library.

推荐答案

您不需要第三方组件.检查这些样品

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

使用Range函数,该函数带有Text属性

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;

或使用剪贴板,您必须选择所有文档内容,复制到剪贴板并使用 Clipboard.AsText

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;

这篇关于将Word文档(* .doc)转储为文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆