Solved

Extracting text from a Word doc, but every line ends with CR/CR

Posted on 2015-02-21
4
137 Views
Last Modified: 2015-02-24
Hi, I'm using this code to extract text from a Word .doc file:

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;

Open in new window


It works good except for one thing - every line of text it returns is terminated by a CR/CR (ie. #13#13), instead of a CR/LF (ie. #13#10). Is there a way to have the lines of the extracted text terminated my CR/LF?

Thanks!
    Shawn
0
Comment
Question by:shawn857
  • 2
  • 2
4 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 40623914
Seems like when copying text by range, it loses the CR&LF.
Better let's use Clipboard:

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;

Open in new window

0
 

Author Comment

by:shawn857
ID: 40624690
Thanks Jimy, but the clipboard method runs so much slower than copying text by range. So nothing can be done in the original method to replace CRCR to CRLF?

Thanks
    Shawn
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 40625030
> "So nothing can be done in the original method to replace CRCR to CRLF?"

It is possible by using StringReplace. But sounds unsafe to replace every occurrence of #13#13. You better test it carefully.

Result:= StringReplace(CopiedText, CRCR, CRLF, [rfReplaceAll]);

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
    Result:=StringReplace(Result, #13#13, #13#10, [rfReplaceAll]);
    WordApp.documents.item(1).Close;
  finally
   WordApp.Quit;
  end;
end;

Open in new window

0
 

Author Closing Comment

by:shawn857
ID: 40629301
Thanks Jimy!

Cheers
    Shawn
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now