-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiFunc.cpp
More file actions
114 lines (95 loc) · 3.51 KB
/
ApiFunc.cpp
File metadata and controls
114 lines (95 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "stdafx.h"
#include "krs.h"
#include "MainFrm.h"
#include <afxdisp.h>
#include "excel.h"
#include "ApiFunc.h"
void KNBK(int KNBKList, CString KBIPath)
{
try
{
_Application app; // app is an _Application object.
_Workbook book;
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range;
char buf[1024];
LPDISPATCH lpDisp; // IDispatch *; pointer reused many times.
long count; // Count of the number of worksheets.
// Common OLE variants. These are easy variants to use for
// calling arguments.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// Start Excel and get Application object.
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch on Excel");
return;
}
// Set visible.
app.SetVisible(TRUE);
// Get Workbooks collection.
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer
ASSERT(lpDisp); // or fail.
books.AttachDispatch( lpDisp ); // Attach the IDispatch pointer
// to the books object.
// Open a workbook.
lpDisp = books.Open(KBIPath,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional);//, covOptional); // Excel 2000 has 13 parameters
ASSERT(lpDisp); // It worked!
// Attach to a Workbook object.
book.AttachDispatch( lpDisp ); // Attach the IDispatch pointer
// to the book object.
// Get sheets.
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
// Get the number of worksheets in this book.
count = sheets.GetCount();
if(count > KNBKList)
{
// Enumerate through worksheets in book and activate in
// succession.
// Get the sheet. Note that 1 is added to the index to make sure
// it is 1-based, not zero-based. Otherwise, you will get odd
// exceptions.
lpDisp = sheets.GetItem( COleVariant((short)(KNBKList)) ); // 'Item' in
// the Worksheets collection = worksheet #.
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp);
// Activate and sleep for two seconds so you can see it happen.
sheet.Activate();
}
else
{
sprintf(buf, "%ld Ëèñò ñ òàêèì íîìåðîì îòñóòñòâóåò â äîêóìåíòå", KNBKList);
::MessageBox(NULL, buf, "Sheet Count", MB_OK | MB_SETFOREGROUND);
}
} // End of Processing logic.
catch(COleException *e)
{
char buf[1024];
sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
}
catch(COleDispatchException *e)
{
char buf[1024];
sprintf(buf,
"COleDispatchException. SCODE: %08lx,Description: \"%s\".",
(long)e->m_wCode, (LPSTR)e->m_strDescription.GetBuffer(1024));
::MessageBox(NULL, buf, "COleDispatchException",
MB_SETFOREGROUND | MB_OK);
}
catch(...)
{
::MessageBox(NULL, "General Exception caught.", "Catch-All",
MB_SETFOREGROUND | MB_OK);
}
}