2010-11-27 112 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