IEl*_*ite 0 delphi ms-word delphi-2010
我正在寻找帮助将word文档(*.doc)转储到Text?我正在使用Delphi 2010.
如果解决方案是组件或库,则它应该是免费或开源组件或代码库.
你不需要第三方组件.检查这些样本
使用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;
Run Code Online (Sandbox Code Playgroud)
或者使用剪贴板,您必须选择所有文档内容,复制到剪贴板并使用检索数据 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;
Run Code Online (Sandbox Code Playgroud)