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 :)
Updated 12-Jul-19 19:32pm
v2