为什么调用OleFunction("Save")或者OleFunction("SaveAs",strFileName)会报错????

rocsoar 2005-04-04 11:26:51
错误提示为:“未知命令”
...全文
417 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
arraden 2005-04-18
  • 打赏
  • 举报
回复
对,具体的去查看VBA的帮助
僵哥 2005-04-15
  • 打赏
  • 举报
回复
Save或者SaveAs只有WorkBook对象有,所以楼主起初使用Word.Application不能实现,而后使用的是WorkBooks亦不能实现,正确的应该是WorkBook,可以使用Word.Application.ActiveWorkBook来调用Save或者SaveAs。
arraden 2005-04-05
  • 打赏
  • 举报
回复
没有遇到过,帮你顶
mli0080 2005-04-04
  • 打赏
  • 举报
回复
我这里一份关于EXCEL写入的文件,这是以前写的东西用来把ListView中的数据保存成Excel文件,你参考一下吧!
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UseExcelByOle.h"
//---------------------------------------------------------------------------
TExcelOle::TExcelOle()
{
//FFilePath = "";
FInitialized = false;
FIsNewFile = false; //New File Flag
FpOpenDlg = NULL;
FpSaveDlg = NULL;
}
//---------------------------------------------------------------------------
TExcelOle::~TExcelOle()
{
if(FVExcelApp.vt != VT_NULL || FVExcelApp.vt != VT_EMPTY)
FVExcelApp = Unassigned;
if(FpOpenDlg)
delete FpOpenDlg;
if(FpSaveDlg)
delete FpSaveDlg;
}
//---------------------------------------------------------------------------
String TExcelOle::GetCells(int ARow, int ACol)
{
String sResult = "";
FVExcelSheetCell = FVExcelWorkSheet.OlePropertyGet("Cells",ARow,ACol);
sResult = FVExcelSheetCell.OlePropertyGet("Value");
return sResult;
}
//---------------------------------------------------------------------------
void TExcelOle::SetCells(int ARow, int ACol, const String Value)
{
FVExcelSheetCell = FVExcelWorkSheet.OlePropertyGet("Cells",ARow,ACol);
FVExcelSheetCell.OlePropertySet("Value",Value);
}
//---------------------------------------------------------------------------
String TExcelOle::ExtendFileName(String sFilePath)
{
String Result = sFilePath;
int nTmp = sFilePath.Pos(".xls");
if(nTmp ==0)
Result = sFilePath + ".xls";
return Result;
}

//---------------------------------------------------------------------------
void __fastcall TExcelOle::OnDlgCanClose(TObject *Sender,bool &CanClose)
{
String sTmp = "";
if(FpOpenDlg == Sender)
{
sTmp = ExtendFileName(FpOpenDlg->FileName.Trim());
CanClose = WantFileExist(sTmp,true);
}
else if(FpSaveDlg == Sender)
{
sTmp = ExtendFileName(FpSaveDlg->FileName.Trim());
CanClose = WantFileExist(sTmp,false);
}
else
return;
}
//---------------------------------------------------------------------------
bool TExcelOle::WantFileExist(String sFilePath,bool bWanted)
{
bool Result = false;
if(bWanted)
{
if(!FileExists(sFilePath))
{
String sMsg = "xls文件" + sFilePath + "不存在!";
MessageDlg(sMsg,mtError,TMsgDlgButtons()<<mbYes,0);
}
else
Result = true;
}
else
{
if(FileExists(sFilePath))
{
String sMsg = "xls文件'" + sFilePath + "'已经存在。是否替换原文件?";
int nResult = MessageDlg(sMsg,mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0);
if(nResult == mrYes)
{
Result = DeleteFile(sFilePath);
if(!Result)
{
sMsg = "xls文件'" + sFilePath + "'无法替换!可能正在使用中!";
MessageDlg(sMsg, mtError, TMsgDlgButtons()<<mbOK, 0);
}
}
}
else
Result = true;
}
return Result;
}
//---------------------------------------------------------------------------
void TExcelOle::InitOpenDlg()
{
if(!FpOpenDlg)
{
FpOpenDlg = new TOpenDialog(NULL);
FpOpenDlg->Filter = "Microsoft Excel 工作薄(*.xls)|*.xls";
FpOpenDlg->FileName = "Book1.xls";
FpOpenDlg->DefaultExt = ".xls";
FpOpenDlg->OnCanClose = OnDlgCanClose;
}
FpOpenDlg->InitialDir = GetCurrentDir();
}
//---------------------------------------------------------------------------
void TExcelOle::InitSaveDlg()
{
if(!FpSaveDlg)
{
FpSaveDlg = new TSaveDialog(NULL);
FpSaveDlg->Filter = "Microsoft Excel 工作薄(*.xls)|*.xls";
FpSaveDlg->FileName = "Book1.xls";
FpSaveDlg->DefaultExt = ".xls";
FpSaveDlg->OnCanClose = OnDlgCanClose;
}
FpSaveDlg->InitialDir = GetCurrentDir();
}
//---------------------------------------------------------------------------
//判断是否安装MS Excel
bool TExcelOle::IsInstalled()
{
bool Result = false;
HKEY Key = NULL;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Office\\Excel\\",
0,KEY_READ,&Key);
if(Key!=NULL)
Result = true;
return Result;
}
//---------------------------------------------------------------------------
//创建ExcelApp
bool TExcelOle::Initialize()
{
if(FInitialized)
return FInitialized;
if(!ExcelInstalled)
{
MessageDlg("您计算机没有安装Excel软件,或者安装不正确\n导致不能使用Excel功能!",mtError,TMsgDlgButtons()<< mbOK,0);
return FInitialized;
}
try
{
FVExcelApp = CreateOleObject("Excel.Application"); //Create Excel App OLE
}
catch(Exception &e)
{
MessageDlg("加载Excel应用程序时出错!",mtError,TMsgDlgButtons()<< mbOK,0);
FVExcelApp = Unassigned;
return FInitialized;
}
FVExcelWorkBooks = FVExcelApp.OlePropertyGet("Workbooks");
FInitialized = true;
return FInitialized;
}
//---------------------------------------------------------------------------
//新建Excel文档
void TExcelOle::NewFile()
{
if(FInitialized)
{
FVExcelWorkBook = FVExcelWorkBooks.OleFunction("Add");
FVExcelWorkSheet = FVExcelWorkBook.OlePropertyGet("ActiveSheet");
FVExcelSheetCols = FVExcelWorkSheet.OlePropertyGet("Columns");
FIsNewFile = true;
}
}
//---------------------------------------------------------------------------
//打开已有文件(直接传路径)
bool TExcelOle::OpenFile(String sFilePath)
{
bool Result = false;
if(FInitialized)
{
if(WantFileExist(sFilePath.Trim(),true))
{
FVExcelWorkBook = FVExcelWorkBooks.OleFunction("Open",sFilePath.c_str());
FVExcelWorkSheet = FVExcelWorkBook.OlePropertyGet("ActiveSheet");
FVExcelSheetCols = FVExcelWorkSheet.OlePropertyGet("Columns");
Result = true;
//FFilePath = sFilePath;
FIsNewFile = false;
}
}
return Result;
}
//---------------------------------------------------------------------------
//打开已有文件(带对话框)
bool TExcelOle::OpenFileDlg()
{
bool Result = false;
if(FInitialized)
{
InitOpenDlg();
if(FpOpenDlg->Execute() && FpOpenDlg->FileName.Trim() !="")
Result = OpenFile(FpOpenDlg->FileName.Trim());
}
return Result;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void TExcelOle::SetColAutoFize()
{
FVExcelSheetCols.OleProcedure("AutoFit");
}
//---------------------------------------------------------------------------



//---------------------------------------------------------------------------
//如果是open的,直接保存
bool TExcelOle::Save()
{
bool Result = false;
if(FInitialized)
{
FVExcelWorkBook.OleProcedure("Save");
Result = true;
}
return Result;
}
//---------------------------------------------------------------------------
bool TExcelOle::SaveAs(String sFilePath)
{
bool Result = false;
if(FInitialized)
{
if(WantFileExist(sFilePath.Trim(),false))
{
FVExcelWorkBook.OleFunction("SaveAs",sFilePath.c_str());
Result = true;
}
}
return Result;
}
//---------------------------------------------------------------------------
bool TExcelOle::SaveAsDlg()
{
bool Result = false;
if(FInitialized)
{
InitSaveDlg();
if(FpSaveDlg->Execute() && FpSaveDlg->FileName.Trim() !="")
Result = SaveAs(FpSaveDlg->FileName.Trim());
}
return Result;
}
//---------------------------------------------------------------------------
void TExcelOle::Close()
{
if(FInitialized)
{
FVExcelWorkBooks.OleProcedure("Close");
FVExcelApp.OleProcedure("Quit");
}
}
//---------------------------------------------------------------------------
#pragma package(smart_init)
mli0080 2005-04-04
  • 打赏
  • 举报
回复
那是excel控件里面的函数,你安装了excel了吗?不是BCB里面的函数!你去查有关excel的文档吧!
rocsoar 2005-04-04
  • 打赏
  • 举报
回复
不是。我在BCB中,把数据存到一个新的excel文件里,在网上照抄了一段代码,如下:
AnsiString strFullFileName = "f:\\gddh\\tttt.xls";
Variant excel_book; //book
Variant excel_sheet; //工作步
Variant excel_range;
Variant my_worksheet; //当前页
Variant my_range; //当前范围
Variant V = Variant::CreateObject("Excel.Application");
excel_book=V.OlePropertyGet("workbooks"); //创建book
V.OlePropertySet("SheetsInNewWorkbook",(Variant)1);
excel_sheet=V.OleFunction("add"); ----------------------这里报错
my_worksheet=excel_sheet.OlePropertyGet("ActiveSheet");
V.OlePropertySet("Visible",(Variant)true); //可见
V.OleProcedure("Save"); -----------------------------------这里报错
V.OleFunction("Quit"); ------------------------------------这里报错
V = Unassigned;

错误原因都是“未知命令”
mli0080 2005-04-04
  • 打赏
  • 举报
回复
OleFunction好象不是MFC中的函数吧!!是你自己写的一个函数吗?
constantine 2005-04-04
  • 打赏
  • 举报
回复
友情up
nicholas1002 2005-04-04
  • 打赏
  • 举报
回复
nichodff
rocsoar 2005-04-04
  • 打赏
  • 举报
回复
FVExcelWorkBook.OleFunction("SaveAs",sFilePath.c_str());


这一句的时候,错误提示仍为:未知命令。
void __fastcall TPhoneForm::SelectButtonClick(TObject *Sender) {   AnsiString StrDate, ExName;//存放日期用于sheet   AnsiString Datatem,phone1="拨号";//临时存放数据库的字段值   int i,j;   //查询所需的数据   PhoneADOQuery->Close();   PhoneADOQuery->Parameters->ParamByName("date1")->Value=PhoneMaskEdit1->Text;   PhoneADOQuery->Parameters->ParamByName("date2")->Value=PhoneMaskEdit2->Text;   PhoneADOQuery->Active=true;   //新建一个EXCEL   Ex = Variant::CreateObject("Excel.Application");   Ex.OlePropertyGet("workbooks").OleFunction("Add", 6);   Wb = Ex.OlePropertyGet("ActiveWorkBook");   Sh = Wb.OlePropertyGet("ActiveSheet");   Ex.OlePropertySet("Visible", true);   //给sheet以日期重命名,   StrDate=DateToStr(Date());   Sh.OlePropertySet("Name", StrDate.c_str());   //给EXCEL输入数据   for (j=0;jFieldCount;j++)   {      Datatem=PhoneADOQuery->Fields->Fields[j]->FieldName;      Sh.OlePropertyGet("Cells", 1, j+1).OlePropertySet("Value", Datatem.c_str());   }   PhoneADOQuery->First();   for (i=0; iRecordCount; i++)   {   for (j=0;jFieldCount;j++)   {      Datatem=PhoneADOQuery->Fields->Fields[j]->AsString;      Sh.OlePropertyGet("Cells", i+2, j+1).OlePropertySet("Value", Datatem.c_str());      if (phone1==PhoneADOQuery->Fields->Fields[j]->FieldName)     {Sh.OlePropertyGet("Cells", i+2, j+1).OlePropertySet("NumberFormatLocal", "0_ ");//设置单元格格式为数值格式 }   }   PhoneADOQuery->Next();   }   //保存EXCEL并退出   ExName=GetCurrentDir()+"\\"+DateToStr(Date())+".xls";   Wb.OleFunction("SaveAs", ExName.c_str());   Wb.OleFunction("Close");   Ex.OleFunction ("Quit");   Ex = Unassigned; }

703

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder ActiveX/COM/DCOM
社区管理员
  • ActiveX/COM/DCOM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧