CreateDispatch to start Word problems 
Author Message
 CreateDispatch to start Word problems

Hi,

I have created a win32 console application that supports MFC.  I want
to be ablel to start Word by using this little program.  I am using
the code at the end of this message.  It doesn't work, all I get is my
message box telling me that Word cannot be started.

Hope someone can help, thanks, Chris.

// AutoWord.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "AutoWord.h"
#include "msword9.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
        int nRetCode = 0;
        COleException *e = new COleException;

        // initialize MFC and print and error on failure
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(),
0))
        {
                // TODO: change error code to suit your needs
                cerr << _T("Fatal Error: MFC initialization failed") << endl;
                nRetCode = 1;
        }
        else
        {      
                _Application oApp;

                if(!oApp.CreateDispatch("Word.Application", e))
                {
                        AfxMessageBox("Unable to start Word");
                        nRetCode = 0;
                }
        }

        return nRetCode;

Quote:
}



Wed, 07 Sep 2005 17:39:27 GMT  
 CreateDispatch to start Word problems
Hi Chris,

There should be a matching pair of ::CoInitialize()
and ::CoUninitialize().
You provide a pointer to a COleException object but you
don't care what happens to it...
If your CreateDispatch fails you can call
COleException::ReportError() to show a dialog box that
explains the error.

If the error message cannot help you, post again and bring
along the error message.

BTW, don't forget to delete the exception.

Hope this helps
--
Rog

Quote:
>-----Original Message-----
>Hi,

>I have created a win32 console application that supports
MFC.  I want
>to be ablel to start Word by using this little program.  
I am using
>the code at the end of this message.  It doesn't work,
all I get is my
>message box telling me that Word cannot be started.

>Hope someone can help, thanks, Chris.

>// AutoWord.cpp : Defines the entry point for the console
application.
>//

>#include "stdafx.h"
>#include "AutoWord.h"
>#include "msword9.h"

>#ifdef _DEBUG
>#define new DEBUG_NEW
>#undef THIS_FILE
>static char THIS_FILE[] = __FILE__;
>#endif

>//////////////////////////////////////////////////////////
///////////////////
>// The one and only application object

>CWinApp theApp;

>using namespace std;

>int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
>{
>    int nRetCode = 0;
>    COleException *e = new COleException;

>    // initialize MFC and print and error on failure
>    if (!AfxWinInit(::GetModuleHandle(NULL),

NULL, ::GetCommandLine(),
Quote:
>0))
>    {
>            // TODO: change error code to suit your
needs
>            cerr << _T("Fatal Error: MFC

initialization failed") << endl;
Quote:
>            nRetCode = 1;
>    }
>    else
>    {      
>            _Application oApp;

>            if(!oApp.CreateDispatch

("Word.Application", e))

- Show quoted text -

Quote:
>            {
>                    AfxMessageBox("Unable to start
Word");
>                    nRetCode = 0;
>            }
>    }

>    return nRetCode;
>}
>.



Sat, 10 Sep 2005 09:26:01 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Automation problem with Word: Couldn't get Word object

2. Start Office-Programm like Word within an ATL Dialog

3. How to Start Word from C Application

4. Can I use a DLL to start MS Word with Automation

5. Starting Word and staying in current directory.

6. start word and run running a macro

7. How to start Notepad/Word/etc from VC++

8. start word and running a macro

9. CreateDispatch fails for COM DLL class

10. CreateDispatch giving weird error.

11. CreateDispatch Failing for Outlook.Application

12. createdispatch opens executable

 

 
Powered by phpBB® Forum Software