Click here to Skip to main content
15,886,001 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to read all the lines in a Microsoft Office doc file by C++.

I dont care about the size font of headings or something else, I just want to read all the text lines in the Microsoft Office doc file.

Please help.
Posted
Updated 12-Jul-19 19:28pm
v3

You can use automation for that. Have a look at the links:
MS Office OLE Automation Using C++[^]

Adding automation to MFC applications[^]

Good luck!
 
Share this answer
 
Did you check this article?
Read Document Text Directly from Microsoft Word File[^]

It is in C#, but shouldn't be hard to translate into C++.
 
Share this answer
 
v3
Okey so a bit late, but here is my solution:

1) Use the MSWORD.OLB file to generate the wrappers for:
_Application
_Document
Documents
Range
Words

2) Comment out the - > #import "C:\\Program Files\\Microsoft Office\\Office16\\MSWORD.OLB" no_namespace , in every generated wrapper .h file

3) Where you want to use it for example:
MyWordReadingClass.h - file:

#include "CApplication.h"
#include "CDocuments.h"
#include "CDocument0.h"

... so on, those above that were generated

class MyWordReadingClass : public CDialogEx {
DECLARE_DYNAMIC(MyWordReadingClass)
public:
... rest of your code ...
private:
// Word application interface
CApplication m_iAppInterface;
// Documents interface
CDocuments m_iDocuments;
// Active Document interface
CDocument0 m_iActiveDocument;
// Content of the document - Range
CRange m_iDocumentRange;
// The class representing the words
CWords m_oWords;
// Total number of words in the Doc
long m_lTotalNumberOfWords;
// summarized text
std::vector<cstring> m_vText;
};

MyWordReadingClass.cpp - file:

void MyWordReadingClass::OnBnLoadDoc()
{
CFileDialog fdlgFileChooser(true, _T("*.*"), 0, 4 | 2, _T("Microsoft Word Files|*.*"));

if (fdlgFileChooser.DoModal() == IDCANCEL)
{
return;
}

if (PathFileExists(fdlgFileChooser.GetPathName()))
{
if (!m_iAppInterface.CreateDispatch(_T("Word.Application")))
{
AfxMessageBox(_T("Cannot open the Word"));
return;
}

// Set visibility, show / hide (no need to show the document)
m_iAppInterface.put_Visible(FALSE);

COleVariant ovTrue((short)TRUE);
COleVariant ovFalse((short)FALSE);
COleVariant ovOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

// Get Documents
m_iDocuments = m_iAppInterface.get_Documents();

// Open a Document
m_iActiveDocument = m_iDocuments.Open(COleVariant(fdlgFileChooser.GetPathName()),
ovOptional, ovOptional, ovOptional, ovOptional, ovOptional, ovOptional,
ovOptional, ovOptional, ovOptional, ovOptional, ovOptional, ovOptional,
ovOptional, ovOptional, ovOptional);
m_oWords = m_iActiveDocument.get_Words();
m_lTotalNumberOfWords = m_oWords.get_Count();

ULONG index = 1;
while (index <= m_lTotalNumberOfWords)
{
CRange oRange = m_oWords.Item(index);
index++;
CString word = oRange.get_Text();
word.TrimRight();
m_vText.emplace_back(word);
}

// m_vText all the words are here do some op...

COleVariant ovNoPrompt((short)FALSE);
m_iDocuments.Close(ovOptional, ovOptional, ovOptional);
m_iAppInterface.ReleaseDispatch();
}
}

Hope this helps :)
 
Share this answer
 
v2
Comments
Richard MacCutchan 13-Jul-19 3:50am    
I appreciate your response, but after 8 years it is most unlikely that the questioner has any interest in this issue. Try to work on some of the newer and more active questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900