您不需要第三方组件。检查这些样本
使用Range带有Text属性的函数
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;
//缺点会闪现word文档界面
function ExtractTextFromWordFile3(const FileName:string):string;
var
WordShablon : Variant;
begin
WordShablon := CreateOleObject('Word.Application');
WordShablon.Visible := false;
WordShablon.Documents.Open(FileName);
Result:=WordShablon.ActiveDocument.Content.Text;
WordShablon.Documents.Close;
WordShablon.Quit;
WordShablon := Unassigned;
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;
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.05431604385376 seconds