void Tmainform::RunVBAScript(String procName, String vbScript)
{
_CodePanes *codePanes;
_CodePanePtr codePane;
CodeModule *codeModule;
try
{
codePanes = ExcelApplication1->VBE->CodePanes;
codePane = codePanes->Item(1);
codeModule= codePane->CodeModule;
codeModule->DeleteLines(1,codeModule->CountOfLines);
codeModule->AddFromString(WideString(vbScript));
ExcelApplication1->Run(procName,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, //10
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, //20
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ); //30
codeModule->DeleteLines(1,codeModule->CountOfLines);
}
catch(Exception &e)
{
ShowMessage(e.Message);
}
}
위의 코드가 C++ Builder로 바꾼건데요...
codePane = codePanes->Item(1); 를 호출할때 에러 메시지가 뜨고...
"개체가 서버에 연결되지 않았습니다"라고 나옵니다.
그리고 RunVBAScript()를 실행하기 전에 하는 일은...
폼에 ExcelApplication1,ExcelWorkbook1,ExcelWorksheet1 을 올려놓은 상태에서...
아래와 같습니다...
ExcelApplication1->Set_DisplayAlerts(0,false);
ExcelApplication1->Set_Visible(0,true);
ExcelWorkbook1->ConnectTo(ExcelApplication1->Workbooks->Add(xlWBATWorksheet,lcid));
ExcelWorksheet1->ConnectTo(ExcelApplication1->Worksheets->get_Item(1));
ExcelWorksheet1->Set_Name(WideString("보고서"));
ExcelWorksheet1->Activate(lcid);
ExcelWorksheet1->Cells->set__Default(1,1,"보고서");
RangePtr titleRow = ExcelWorksheet1->Rows->get__Default(1);
titleRow->Font->set_Bold(true);
titleRow->Font->set_Size(25);
int i;
for (i=10;i<=20;i++)
{
ExcelWorksheet1->Cells->set__Default(i,1,i);
}
ExcelWorksheet1->Cells->set__Default(i,1,"=SUM(A10:A20)");
그리고 이 코드는 정상작동이 되구요...
결과는 excel이 실행되고, book생성, sheet에 값을 넣는것까지 됩니다...
그런데 그 담엔 RunVBAScript를 실행하면 안되는거구요...
메시지는 "개체가 서버에 연결되지 않았습니다"입니다...
혹시 RunVBAScript()가 실행되시는 분은 도움 좀 주세요...^^;;;
|