VC6.0调用Excel XP,Word 2K

#include <ole2.h>
#include <stdio.h>
#pragma warning (disable:4146 4192 4786 4081)
#define USE_OFFICEXP 1

#ifdef USE_OFFICEXP
#import "C:\Program Files\Common Files\Microsoft Shared\Office10\MSO.DLL"
#import "c:\\Program Files\\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.olb"
#import "c:\Program Files\Microsoft Office\Office10\MSWORD.olb" rename("ExitWindows","ExitWindowsEx")
#import "c:\Program Files\Microsoft Office\Office10\excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
         rename("DocumentProperties", "DocumentPropertiesXL") exclude("IFont","IPicture")
#else
#import "c:\Program Files\Microsoft Office\Office\MSO9.dll"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_namespace
#import "c:\Program Files\Microsoft Office\Office\MSWORD9.OLB" rename("ExitWindows","_ExitWindows")
#import "c:\Program Files\Microsoft Office\Office\excel9.olb" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
         rename("DocumentProperties", "DocumentPropertiesXL") exclude("IFont","IPicture")
#endif
int main(int argc, char* argv[])
{
 VARIANT varItem;
 varItem.vt = VT_I4;
 varItem.intVal = 1;
    ::CoInitialize(NULL);
    CLSID clsid;
   
#if 0
 CLSIDFromProgID(L"Word.Application", &clsid);
 Word::_ApplicationPtr pApp;
 pApp.CreateInstance (clsid,NULL);
 pApp ->PutVisible (VARIANT_TRUE);
 pApp ->GetDocuments() ->Add();
 Word::_DocumentPtr pWordDocument =pApp-> GetActiveDocument();
 Word::SelectionPtr pSelection    = pApp ->GetSelection();
 pSelection ->TypeText("badguy");
#endif
#if 1
 CLSIDFromProgID(L"Excel.Application", &clsid);
 Excel::_ApplicationPtr pApp;
 pApp .CreateInstance (clsid,NULL);
 pApp ->PutVisible (0,VARIANT_TRUE);
 Excel::_WorkbookPtr pWorkbook = pApp ->GetWorkbooks () ->Add ();
 Excel::_WorksheetPtr    pSheet    = pWorkbook ->GetWorksheets ()  ->GetItem (varItem); 
 Excel::RangePtr     pRange    = pSheet ->GetUsedRange(0);
 int iColCnt = pRange ->GetColumns() ->GetCount();
 printf("used columns is %d\n",iColCnt);
 int iRowCnt = pRange ->GetRows() ->GetCount();
 printf("used rows  is %d\n",iRowCnt);
 pRange = pSheet ->GetRange(OLESTR("A1"));
 pRange ->Value2     = OLESTR("SOME");
 Excel::InteriorPtr  pInterior= pRange ->GetInterior ();
 varItem.intVal = 0xff0000;
 pInterior ->PutColor (varItem); 
 pSheet ->PrintPreview ();
#endif
    ::CoUninitialize();
    return 0;
}

 

posted on 2008-01-07 11:34  施昌权-博客  阅读(1130)  评论(0编辑  收藏  举报

导航