2010-11-27 107 views

回答

5

你不需要第三方组件。检查这些样品

使用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; 

或使用剪贴板,你必须选择所有的文档内容,复制到剪贴板,并使用检索数据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

That Works!非常感谢! – IElite 2010-11-27 13:19:47