导入office的类型库的方法步骤

导入office的类型库的方法步骤

关键词:
Office,COM,OLB

我们要操作office那么第一个想到的就是该怎么把它的库加入到我们项目中呢?
最近对office研究了一番,ms的office可以说完全利用com的一个杰作,我们没有
比它用的更好,以前看到过国外有几家公司也用com写的开发包,例如ArcGis,
不能不佩服,看来还需要不断练内功!
最近去csdn论坛发了个关于offfice清理问题的帖子,冷清的让我心寒!这些大牛
都去写blog了?

进入正题,下面我列出office当前所有版本的类型库,office2007以后再说!哈哈!
  office版本号              类型库
  --------------------------------------------------

  Microsoft Access 97          Msacc8.olb
  Microsoft Jet Database 3.5      DAO350.dll
  Microsoft Binder 97          Msbdr8.olb
  Microsoft Excel 97          Excel8.olb
  Microsoft Graph 97          Graph8.olb
  Microsoft Office 97          Mso97.dll
  Microsoft Outlook 97         Msoutl97.olb
  Microsoft PowerPoint 97        Msppt8.olb

  Microsoft Word 97           Msword8.olb
  Microsoft Access 2000         Msacc9.olb
  Microsoft Jet Database 3.51      DAO360.dll
  Microsoft Binder 2000         Msbdr9.olb
  Microsoft Excel 2000         Excel9.olb
  Microsoft Graph 2000         Graph9.olb
  Microsoft Office 2000         Mso9.dll
  Microsoft Outlook 2000        Msoutl9.olb
  Microsoft PowerPoint 2000       Msppt9.olb
  Microsoft Word 2000          Msword9.olb 

  Microsoft Access 2002         Msacc.olb
  Microsoft Excel 2002         Excel.exe
  Microsoft Graph 2002         Graph.exe 
  Microsoft Office 2002         MSO.dll
  Microsoft Outlook 2002        MSOutl.olb
  Microsoft PowerPoint 2002       MSPpt.olb
  Microsoft Word 2002          MSWord.olb

  Microsoft Office Access 2003     Msacc.olb
  Microsoft Office Excel 2003      Excel.exe
  Microsoft Graph 2003         Graph.exe 
  Microsoft Office 2003         MSO.dll
  Microsoft Office Outlook 2003     MSOutl.olb
  Microsoft Office PowerPoint 2003   MSPpt.olb
  Microsoft Office Word 2003      MSWord.olb

上面你可能注意到了,office2002和office2003怎么有类型库在exe中?
我也不知道ms为啥把它放进去,不过用import一样的导入。

下面我以office2003为例写的一个例子:

#import "C://Program Files//Common Files//system//ado//msado21.tlb" no_namespace /
rename ("EOF", "adoEOF") 

#import "C://Program Files//Common Files//Microsoft Shared//DAO//DAO360.DLL" rename("EOF","EndOfFile") /
rename("BOF","BegOfFile")

#import "C:/Program Files//Common Files//Microsoft Shared//Office11//MSO.DLL" rename_namespace("Office")
using namespace Office;

#import "C://Program Files//Common Files//Microsoft Shared//VBA//VBA6//VBE6EXT.olb" rename_namespace("VBE6")
using namespace VBE6;

#import "C:/Program Files/Microsoft Office/Office11/MSWORD.olb" rename("ExitWindows","ExitWindowsEx"),named_guids,rename_namespace("MSWord")
using namespace MSWord;

#import "C:/Program Files/Microsoft Office/Office11/MSACC.olb" implementation_only,rename_namespace("MSACC")
using namespace MSACC;

#import "C:/Program Files/Microsoft Office/Office11/MSPPT.olb" implementation_only,rename_namespace("MSPPT")
using namespace MSPPT;

#import "C://Program Files//Microsoft Office//Office11//Excel.exe" rename("RGB","RGBEx"),rename("DialogBox","DialogBoxEx"),rename_namespace("MSExcel")
using namespace MSExcel;

说明:
1、前四个的路径都是默认的,后四个根据版本不同更改即可!
2、ado和dao是为了支持Access的,如果你不是操作Access那么可以去掉他们!
3、office和VBE6是必须的,我们每次都会用到!

下面给一个简单例子:
int _tmain(int argc, _TCHAR* argv[])
{
//Initialize the COM libraries
::CoInitialize(NULL);

std::cout<<"Begin clear!"<<std::endl;

// Create an instance of the Word application and obtain the 
// pointer to the application's IDispatch interface.
CLSID clsidWord;
CLSIDFromProgID(L"Word.Application", &clsidWord); 

IUnknown* pUnk;

HRESULT hr = GetActiveObject(clsidWord, NULL, (IUnknown**)&pUnk); //得到当前打开的word的接口指针
    //你也可以用CoCreateInstance(...)去创建!

    MSWord::_ApplicationPtr pApplication = pUnk;
//...
::CoUninitialize();
std::cout<<"Finish!---Thanks!"<<std::endl;
system("PAUSE");
return 0;
}

作者:wangweixing2000
转贴请注明出处!谢谢!