Solved

Convert MS word file to text file

Posted on 2011-09-08
6
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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