What is there, you ask, because Microsoft OLE Automation technology is described quite widely and has been used for more than 10 years. And what is surprising here is that when the DO_WORD function was moved to another project, this led to an error, Word simply did not want to start. This is despite the fact that the project is written in C ++, but, unfortunately, does not use MFC technology. Why does nothing work, because the initialization of OLE takes place as indicated in all the manuals? It turns out that this alone is not enough and you need to further initialize the MFC library. How to do it?#include "stdafx.h" #include "msword9.h" void DO_WORD(void) { AfxOleInit(); AfxEnableControlContainer(); //start word COleVariant covTrue((short)TRUE), covFALSE((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); _Application app; Documents Docs; _Document Doc; Selection Select; _Font Font; Find myFind; if (!app.CreateDispatch("Word.Application")) { AfxMessageBox ("Couldn't start Word"); return ;//FALSE; } app.SetVisible(TRUE); CString fileName = "C:/MyDocFile.doc"; Docs = app.GetDocuments(); Doc = Docs.Add(covOptional,covOptional,covOptional,covOptional); Select = app.GetSelection(); //Get Current font object and change it Font = Select.GetFont(); Font.SetBold(TRUE); Font.SetItalic(TRUE); Font.SetSize(18); Select.TypeText("Hello this is a Word doc.\n"); /* Doc.SaveAs(COleVariant(fileName), covOptional,covOptional,covOptional, covOptional,covOptional,covFALSE,covOptional, covOptional,covOptional,covOptional); //Doc.Close(covFALSE, covOptional, covOptional); app.Quit(covFALSE,covOptional,covOptional); */ AfxOleTerm(); } extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // AFX_MODULE_STATE* pModuleState = AfxGetModuleState(); pModuleState->m_bDLL = 0; CWinApp *theApp = new CWinApp; DO_WORD(); delete theApp; return 0; } rem Visual Studio 2010 call "C:\Program Files\Microsoft Visual Studio 10.0\VC\BIN\vcvars32.bat" rem Visual Studio 6.0 rem call "C:\Program Files\Microsoft Visual Studio\VC98\BIN\vcvars32.bat" mkdir Release cl /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fo".\Release\\" /Fd".\Release\\" /FD /c "wordmain.cpp" "msword9.cpp" link /nologo /subsystem:windows /incremental:no /machine:I386 /out:".\Release\wordmain.exe" ".\Release\wordmain.OBJ" ".\Release\msword9.obj" Source: https://habr.com/ru/post/132585/
All Articles