CodePudding user response:
The import import
//Copyright (c) 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 Excel10 OfficeXP in
//the Compile with cl/GX comexcel. CPP
# include "stdafx. H"
# define an 1
# define OFFICEXP 2
# define OFFICE2000 3
//the Default Settings
# define OFFICE_VER an
# define USE_PROGID 1
# define USE_LIBID 0
# # define _M2STR (x) x
# define M2STR _M2STR (x) (x)
//Ignore hard wired default paths if MSDLL_PATH is
//defined from the command line
# # ifndef MSDLL_PATH
//Paths to the required MS OFFICE files.
//Make sure these paths are correct for the machine
# pragma message (" Make sure the path to the oledata.mso DLL is correct. The ")
# if OFFICE_VER==an
# define _MSDLL_PATH "C: \ Program Files \ Common Files \ Microsoft Shared \ Office12 \ the oledata.mso DLL"
# elif OFFICE_VER==OFFICEXP
# define _MSDLL_PATH "C: \ Program Files \ Common Files \ Microsoft Shared \ Office11 \ the oledata.mso DLL"
# elif OFFICE_VER==OFFICE2000
# define _MSDLL_PATH "C: \ Program Files \ Microsoft Office \ Office \ MSO9 DLL"
# endif
# the else
# define _MSDLL_PATH M2STR (MSDLL_PATH)
# endif
//
//Delete the *. TLH files when changing the import the qualifiers
# import _MSDLL_PATH rename (" RGB ", "MSRGB")
# ifdef VBE6EXT_PATH
# import M2STR (VBE6EXT_PATH)
# the else
# import "C: \ Program Files \ Common Files \ Microsoft Shared \ VBA \ VBA6 \ VBE6EXT OLB"
# endif
# if USE_PROGID
# import "progid: Excel Sheet" auto_search auto_rename rename_search_namespace (" Office12 ")
# elif USE_LIBID
# import "libid: {00020813} - 0000-0000 - C000-000000000046" auto_search auto_rename version (1.3) lcid (0) no_search_namespace
# the else
//Ignore hard wired default paths if MSDLL_PATH is
//defined from the command line
# # ifndef MSEXCEL_PATH
# pragma message (" Make sure the path to excel the. Exe is correct ")
# if OFFICE_VER==an
# define _MSEXCEL_PATH "C: \ Program Files \ Microsoft Office \ Office12 \ excel exe"
# elif OFFICE_VER==OFFICEXP
# define _MSEXCEL_PATH "C: \ Program Files \ Microsoft Office \ Office11 \ excel exe"
# elif OFFICE_VER==OFFICE2000
# define _MSEXCEL_PATH "C: \ Program Files \ Microsoft Office \ Office \ excel exe"
# endif
# the else
# define _MSEXCEL_PATH M2STR (MSEXCEL_PATH)
# endif
# import _MSEXCEL_PATH auto_search auto_rename dual_interfaces
# endif//USE_LIBID
Void dump_com_error (_com_error & amp; E)
{
_tprintf (_T (" Oops - hit an error! \ n "));
_tprintf (_T (" \ \ a tCode=% 08 lx \ n "), e.E rror ());
_tprintf (_T (" \ \ a tCode fancy=% s \ n "), e.E rrorMessage ());
_bstr_t bstrSource (e.S ource ());
_bstr_t bstrDescription (e.D escription ());
_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 the namespace Excel;
_ApplicationPtr pXL;
Try {
PXL. CreateInstance (L "Excel. Application");
The pXL - & gt; The Visible [0]=VARIANT_TRUE;
WorkbooksPtr pBooks=pXL - & gt; Workbooks.
_WorkbookPtr pBook=pBooks - & gt; Add ((long) xlWorksheet);
_WorksheetPtr pSheet=pXL - & gt; ActiveSheet.
Try {
//one will fail; It is done on purpose to demonstrate the error.
PSheet - & gt; Name="Market Share?" ;
} the catch (_com_error & amp; E) {
Dump_com_error (e);
}
PSheet - & gt; Name="Market Share!" ;
//the When using the parameterized properties, optional args must be explicitly dealt with.
PSheet - & gt; Range [" A2 "] [vtMissing] - & gt; Value2="Company A";
PSheet - & gt; Range [" B2 "] [vtMissing] - & gt; Value2="Company B".
PSheet - & gt; Range [" C2 "] [vtMissing] - & gt; Value2="Company C";
PSheet - & gt; Range [" D2 "] [vtMissing] - & gt; Value2="Company D";
//Of course, you can call a parameterized property as a method and then optional args are implicit.
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull