📜 ⬆️ ⬇️

Three lines of shamanism

I learned how to do on one great website how to run WinWord and insert my own lines into the document: http://jmvidal.cse.sc.edu/csce790/PS4/

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?

As a result, we have 2 programs, in the first one, which performs the role of our project and is presented in the form of source texts below, the call to the DO_WORD function comes directly from _tWinMain (). In the second program, which was obtained from the link above, first a dialogue is created via MFC, and only then DO_WORD is called. As expected, the first program refuses to work, the second one works fine.

Then the second program was subjected to indescribable shamanism in the debugger. As a result, the first one was finalized and we got the code offered below for your attention. Only three lines of shamanism lead to a normal start of WinWord and full-fledged work.
')
File wordmain.cpp:
#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; } 


The compile.bat file is suitable for both Visual Studio 2010 and the old Visual Studio 6.0. In it, we compile two files, the wordmain.cpp above, as well as msword9.cpp which can be obtained by following the link instructions , namely pressing Ctrl-W and then on the Automation tab click the Add New Class button and then select the file “ C: \ Program Files \ Microsoft Office \ Office \ msword9.olb ”and select all classes by clicking first on the first and holding down the Shift key on the last element of the corresponding list of classes.

Compile file compile.bat:
 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" 


UPD: I posted the sources from this post, including msword9.cpp, in the statja.rar file .

Source: https://habr.com/ru/post/132585/


All Articles