Solved

Convert MS word file to text file

Posted on 2011-09-08
6
339 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
Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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