vc6读写excel时GetWorkbooks()函数出错,提示:找不到成员

woshihuzi 2016-04-25 01:55:18
我用vc6读写excel文件,机器为win7+office2003,能正常执行。
但把我的代码转移到另外一台机器上编译没问题,但运行的时候出错,提示:“ 找不到成员”
用单步调试的办法,错误定位到了GetWorkbooks()函数这里,相关代码如下:
// 定义相关的变量
_Application oApp; //Excel进程对象
Workbooks oWorkbooks; //工作簿集合
_Workbook oWorkbook; //工作簿
Worksheets oWorksheets; //工作表集合
_Worksheet oWorksheet; //工作表
Range oRange; //代表某一单元格、某一行、某一列、某一选定区域
COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //表示一个为optional的值

// 创建一个Excel进程对象
if (!oApp.CreateDispatch("Excel.Application", NULL))
{
AfxMessageBox("创建Excel服务失败!");
exit(1);
}

// 获取工作簿集合
oWorkbooks = oApp.GetWorkbooks(); // 换台机器,执行这里的语句就出错了

这段代码怎么看都没问题,而且在我的机器上编译运行无误,换台机器(VMWare虚拟机,安装XP+office2003+vc6)居然就有问题。
这个问题可能老了一点,现在还用VC6的可能多了,但问题总要解决,那就请曾经玩过这个技术的高手们来指点一下迷津吧。

...全文
2389 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshihuzi 2017-09-19
  • 打赏
  • 举报
回复
谢谢各位,尤其是二楼的“zgl7903”兄。最终还是没搞明白,我就用那个能用的环境勉强完成了任务。
oyljerry 2016-04-29
  • 打赏
  • 举报
回复
两台机器的excel等是否版本一致。VC版本是否一致
赵4老师 2016-04-25
  • 打赏
  • 举报
回复
向2楼学习。
zgl7903 2016-04-25
  • 打赏
  • 举报
回复
MSDN 的例子 Import 的方式 试试看

// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Visual C++ Language  Reference and related
// electronic documentation provided with Microsoft Visual C++.
// See these sources for detailed information regarding the
// Microsoft Visual C++ product.

// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths
//#pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb.  Mso97.dll will usually be in c:\\\"Program Files\"\\\"Microsoft Office\"\\Office, and vbeext1.olb will be in c:\\\"Program Files\"\\\"Common Files\"\\\"Microsoft Shared\"\\VBA")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office11\\mso.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")   
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\VBA6\\VBE6EXT.OLB" no_namespace   
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

#pragma warning (disable:4192)

#include <stdio.h>
#include <tchar.h>

void dump_com_error(_com_error &e)
{
    _tprintf(_T("Oops - hit an error!\n"));
    _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
    _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());
    _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
    _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}

// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called.  If any reference
// count is non-zero, a protection fault will occur.
struct StartOle 
{
    StartOle() { CoInitialize(NULL); }
    ~StartOle() { CoUninitialize(); }
} _inst_StartOle;

void main()
{
    using namespace Excel;

    _ApplicationPtr pXL;

    try 
    {
      pXL.CreateInstance(L"Excel.Application");

      pXL->Visible = VARIANT_TRUE;

      WorkbooksPtr pBooks = pXL->Workbooks;
      _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
      _WorksheetPtr pSheet = pXL->ActiveSheet;

      RangePtr pRange; 
      pRange = pSheet->Range["A21"];
      pRange->Value2 = 75L;
      //pRange->NumberFormatLocal = "@";

      _CommandBarsPtr pCmdbars = pXL->CommandBars;
      int iCmdbars = pCmdbars->GetCount();

      Sleep(1000);
      pRange = pSheet->Range["20:20"];
      pRange->Insert( (long)Excel::xlDown );
      pRange->Merge();
    
      Sleep(1000);

      pBook->Saved = VARIANT_TRUE;
      pXL->Quit();
    } 
    catch(_com_error &e) 
    {
      dump_com_error(e);
      
      pXL->Quit();
    }
}

笨笨仔 2016-04-25
  • 打赏
  • 举报
回复
运行环境变化引发的问题,可检查一下系统环境,比如Office的安装路径等

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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