1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: OLE Automation wxWidgets sample
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets headers
32 #include "wx/msw/ole/automtn.h"
35 #error "Sorry, this sample works under Windows only."
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
41 // the application icon
42 #ifndef wxHAS_IMAGES_IN_RESOURCES
43 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp
: public wxApp
54 // override base class virtuals
55 // ----------------------------
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame
: public wxFrame
68 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
70 // event handlers (these functions should _not_ be virtual)
71 void OnQuit(wxCommandEvent
& event
);
72 void OnAbout(wxCommandEvent
& event
);
73 void OnTest(wxCommandEvent
& event
);
76 // any class wishing to process wxWidgets events must use this macro
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // IDs for the controls and the menu commands
92 // controls start here (the numbers are, of course, arbitrary)
96 // ----------------------------------------------------------------------------
97 // event tables and other macros for wxWidgets
98 // ----------------------------------------------------------------------------
100 // the event tables connect the wxWidgets events with the functions (event
101 // handlers) which process them. It can be also done at run-time, but for the
102 // simple menu events like this the static method is much simpler.
103 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
104 EVT_MENU(OleAuto_Quit
, MyFrame::OnQuit
)
105 EVT_MENU(OleAuto_About
, MyFrame::OnAbout
)
106 EVT_MENU(OleAuto_Test
, MyFrame::OnTest
)
109 // Create a new application object: this macro will allow wxWidgets to create
110 // the application object during program execution (it's better than using a
111 // static object for many reasons) and also declares the accessor function
112 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
116 // ============================================================================
118 // ============================================================================
120 // ----------------------------------------------------------------------------
121 // the application class
122 // ----------------------------------------------------------------------------
124 // `Main program' equivalent: the program execution "starts" here
127 if ( !wxApp::OnInit() )
130 // Create the main application window
131 MyFrame
*frame
= new MyFrame(wxT("OleAuto wxWidgets App"),
132 wxPoint(50, 50), wxSize(450, 340));
137 // success: wxApp::OnRun() will be called which will enter the main message
138 // loop and the application will run. If we returned false here, the
139 // application would exit immediately.
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
148 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
149 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
151 // set the frame icon
152 SetIcon(wxICON(sample
));
155 wxMenu
*menuFile
= new wxMenu
;
157 menuFile
->Append(OleAuto_Test
, wxT("&Test Excel Automation..."));
158 menuFile
->Append(OleAuto_About
, wxT("&About"));
159 menuFile
->AppendSeparator();
160 menuFile
->Append(OleAuto_Quit
, wxT("E&xit"));
162 // now append the freshly created menu to the menu bar...
163 wxMenuBar
*menuBar
= new wxMenuBar
;
164 menuBar
->Append(menuFile
, wxT("&File"));
166 // ... and attach this menu bar to the frame
170 // create a status bar just for fun (by default with 1 pane only)
172 SetStatusText(wxT("Welcome to wxWidgets!"));
173 #endif // wxUSE_STATUSBAR
179 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
181 // true is to force the frame to close
185 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
187 wxMessageBox(wxT("This is an OLE Automation sample"),
188 wxT("About OleAuto"), wxOK
| wxICON_INFORMATION
, this);
191 /* Tests OLE automation by making the active Excel cell bold,
192 * and changing the text.
194 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
196 wxMessageBox(wxT("Excel will be started if it is not running after you have pressed OK button.")
197 wxT("\nThe active cell should then say 'wxWidgets automation test!' in bold."),
200 wxAutomationObject excelObject
;
201 if ( !excelObject
.GetInstance(wxT("Excel.Application")) )
203 wxLogError(wxT("Could not create Excel object."));
207 // Ensure that Excel is visible
208 if (!excelObject
.PutProperty(wxT("Visible"), true))
210 wxLogError(wxT("Could not make Excel object visible"));
212 const wxVariant workbooksCountVariant
= excelObject
.GetProperty(wxT("Workbooks.Count"));
213 if (workbooksCountVariant
.IsNull())
215 wxLogError(wxT("Could not get workbooks count"));
218 const long workbooksCount
= workbooksCountVariant
;
219 if (workbooksCount
== 0)
221 const wxVariant workbook
= excelObject
.CallMethod(wxT("Workbooks.Add"));
222 if (workbook
.IsNull())
224 wxLogError(wxT("Could not create new Workbook"));
229 if (!excelObject
.PutProperty(wxT("ActiveCell.Value"), wxT("wxWidgets automation test!")))
231 wxLogError(wxT("Could not set active cell value."));
234 if (!excelObject
.PutProperty(wxT("ActiveCell.Font.Bold"), wxVariant(true)) )
236 wxLogError(wxT("Could not put Bold property to active cell."));