我现在碰到了一个很棘手的问题,我新建了一个控制台程序,我发现我用MFC的方式无法操纵excel了。难道控制台程序不能自动化?我觉得不可能啊如果单步调试,错误是,1008
An attempt was made to reference a token that does not exist.
如果正常执行,错误是,2
The system cannot find the file specified.
整个程序的代码如下:请大牛帮忙看看?
// testole.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include "testole.h"
#include <afxdisp.h>
#include "excel9.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
if(!AfxOleInit()) // Your addition starts here
{
AfxMessageBox("Could not initialize COM dll");
return FALSE;
}
int nRetCode = 0; // 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
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
_Application app; // app is the Excel _Application object // Start Excel and get Application object...
if(!app.CreateDispatch("Excel.Application"))
{
UINT iLastCode=GetLastError();
LPVOID lpMessageBuffer;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
iLastCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
(LPTSTR) &lpMessageBuffer,
0,
NULL );
char m_ErrorMessage[300];
wsprintf(m_ErrorMessage,"Error Code %d : ",iLastCode);
int iLen=strlen(m_ErrorMessage);
strcpy(m_ErrorMessage+iLen,(char*)lpMessageBuffer);
LocalFree( lpMessageBuffer ); AfxMessageBox(m_ErrorMessage);
}
else
{
//Make Excel Visible and display a message
app.SetVisible(TRUE);
AfxMessageBox ("Excel is Running!");
}
} return nRetCode;
}
An attempt was made to reference a token that does not exist.
如果正常执行,错误是,2
The system cannot find the file specified.
整个程序的代码如下:请大牛帮忙看看?
// testole.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include "testole.h"
#include <afxdisp.h>
#include "excel9.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
if(!AfxOleInit()) // Your addition starts here
{
AfxMessageBox("Could not initialize COM dll");
return FALSE;
}
int nRetCode = 0; // 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
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
_Application app; // app is the Excel _Application object // Start Excel and get Application object...
if(!app.CreateDispatch("Excel.Application"))
{
UINT iLastCode=GetLastError();
LPVOID lpMessageBuffer;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
iLastCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
(LPTSTR) &lpMessageBuffer,
0,
NULL );
char m_ErrorMessage[300];
wsprintf(m_ErrorMessage,"Error Code %d : ",iLastCode);
int iLen=strlen(m_ErrorMessage);
strcpy(m_ErrorMessage+iLen,(char*)lpMessageBuffer);
LocalFree( lpMessageBuffer ); AfxMessageBox(m_ErrorMessage);
}
else
{
//Make Excel Visible and display a message
app.SetVisible(TRUE);
AfxMessageBox ("Excel is Running!");
}
} return nRetCode;
}
但还没有找到方法。
我的程序是CGI程序,奇怪的是
如果在IIS中运行,报错:拒绝访问
如果在Apache中,报错:系统找不到指定的文件
#import "E:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" \
rename_namespace("VBIDE")
#import "E:\Program Files\Microsoft Office\Office10\Excel.EXE" \
rename("DialogBox", "DialogBoxXL") \
rename("RGB", "RGBXL") \
rename("ExitWindows", "ExitWindowsXL") \
rename("DocumentProperties", "DocumentPropertiesXL") \
no_auto_excludeint main(int argc, char* argv[])
{ ::CoInitialize(NULL);
{
Excel::_ApplicationPtr pExcel(L"Excel.Application"); pExcel->PutVisible(0, VARIANT_TRUE);
Sleep(1000);
pExcel->Quit(); pExcel = NULL;
}
::CoUninitialize();
return 0;
}
加上
::CoInitialize(NULL);
就好了
我都可以的
不过用的式com技术