Solved

Convert MS word file to text file

Posted on 2011-09-08
6
334 Views
Last Modified: 2012-06-21
Hi,

I have to convert a MS-word (*.doc/*.docx) file to a text file (*.txt) using a VC++ and MFC.

The problem faced while conversion is that When I try to convert word file to text file the text file is not in the readable form. It shows the text in the form like "´•IOÃ0…ïHü‡ÈW”¸p@5åÀr„JqvIj/ò¸Û¿gÜ%j«¶)P.‘ç½÷yœ™tfºN&àQY“³ë¬Ã0ÒÊT9û¼¤w,Á L!jk  ".

Reverse conversion, I mean from text to *.doc is working fine.

I checked the font properties are also same. Even I pasted the converted garbage text to word file, but it produced the same garbage one.

Welcome if any further information required.

Thanks
0
Comment
Question by:harshvir_drish
  • 3
  • 2
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36501022
How do you perform the conversion?
0
 

Author Comment

by:harshvir_drish
ID: 36501518
I am doing this task using VC+ program.

Pl. find the code snippet , developed by me for this purpose.

         
CFile File1;
	char Buff[15024];
	File1.Open(m_SourceFile,CFile::modeRead);
	UINT Bytes = File1.Read(Buff,15024);
	CFile File2;
	File2.Open(L"c:\\MyFile.txt",CFile::modeCreate|CFile::modeWrite);
	File2.Write(Buff,Bytes);

Open in new window


Waiting for your appreciable response.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36501565
Thought so.  A word document is not text, it has other information in it.  What you are doing is effectively renaming xx.doc to xx.txt.  Hence you see garbage.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:harshvir_drish
ID: 36501656
Yes I tested the same by opening the same word file with notepad and the output is same as generated by my code.

So, Can you guide further?

Thanks for your quick and appreciable response.
0
 
LVL 4

Expert Comment

by:pgorod
ID: 36514087
1. I would do it from within Word, with a Macro, a simple "Save as..." text.

From MFC, there are ways to handle Word from there (COM automation object):
http://support.microsoft.com/kb/196776

But the basic idea is that Microsoft Word knows about it's format, you should use their code to convert.

2. A totally different approach, only for docx format (does not work for .doc!) is that you can unzip a docx and you will see text files with the document content; these are in XML and the format is documented by Microsoft.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 36515440
>>So, Can you guide further?

Two possiblities spring to mind.
Already mentioned is controlling word via automation.
The other is to send a message to word to copy the open document to the clipboard, then your app reads the clipboard and saves to .txt file.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

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

18 Experts available now in Live!

Get 1:1 Help Now