【问题标题】:dumping word document ( *.doc) to Text?将 word 文档 (*.doc) 转储到文本?
【发布时间】:2010-11-27 02:40:31
【问题描述】:

我正在寻求将 word 文档 (*.doc) 转储到 Text 的帮助?我正在使用 Delphi 2010。

如果解决方案是组件或库,它应该是免费或开源的组件或代码库。

【问题讨论】:

    标签: delphi ms-word delphi-2010


    【解决方案1】:

    您不需要第三方组件。检查这些样本

    使用带有Text 属性的Range 函数

    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检索数据

    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;
    

    【讨论】:

    • 有效!非常感谢!
    猜你喜欢
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多