1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: OLE Automation wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers
33 #include "wx/msw/ole/automtn.h"
36 #error "Sorry, this sample works under Windows only."
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
42 // the application icon
43 #if !defined(__WXMSW__) && !defined(__WXPM__)
44 #include "../sample.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // Define a new application type, each program should derive a class from wxApp
52 class MyApp
: public wxApp
55 // override base class virtuals
56 // ----------------------------
58 // this one is called on application startup and is a good place for the app
59 // initialization (doing it here and not in the ctor allows to have an error
60 // return: if OnInit() returns false, the application terminates)
61 virtual bool OnInit();
64 // Define a new frame type: this is going to be our main frame
65 class MyFrame
: public wxFrame
69 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
71 // event handlers (these functions should _not_ be virtual)
72 void OnQuit(wxCommandEvent
& event
);
73 void OnAbout(wxCommandEvent
& event
);
74 void OnTest(wxCommandEvent
& event
);
77 // any class wishing to process wxWidgets events must use this macro
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // IDs for the controls and the menu commands
93 // controls start here (the numbers are, of course, arbitrary)
97 // ----------------------------------------------------------------------------
98 // event tables and other macros for wxWidgets
99 // ----------------------------------------------------------------------------
101 // the event tables connect the wxWidgets events with the functions (event
102 // handlers) which process them. It can be also done at run-time, but for the
103 // simple menu events like this the static method is much simpler.
104 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
105 EVT_MENU(OleAuto_Quit
, MyFrame::OnQuit
)
106 EVT_MENU(OleAuto_About
, MyFrame::OnAbout
)
107 EVT_MENU(OleAuto_Test
, MyFrame::OnTest
)
110 // Create a new application object: this macro will allow wxWidgets to create
111 // the application object during program execution (it's better than using a
112 // static object for many reasons) and also declares the accessor function
113 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
122 // the application class
123 // ----------------------------------------------------------------------------
125 // `Main program' equivalent: the program execution "starts" here
128 if ( !wxApp::OnInit() )
131 // Create the main application window
132 MyFrame
*frame
= new MyFrame(wxT("OleAuto wxWidgets App"),
133 wxPoint(50, 50), wxSize(450, 340));
135 // Show it and tell the application that it's our main window
136 // @@@ what does it do exactly, in fact? is it necessary here?
140 // success: wxApp::OnRun() will be called which will enter the main message
141 // loop and the application will run. If we returned false here, the
142 // application would exit immediately.
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
151 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
152 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
154 // set the frame icon
155 SetIcon(wxICON(sample
));
158 wxMenu
*menuFile
= new wxMenu
;
160 menuFile
->Append(OleAuto_Test
, wxT("&Test Excel Automation..."));
161 menuFile
->Append(OleAuto_About
, wxT("&About..."));
162 menuFile
->AppendSeparator();
163 menuFile
->Append(OleAuto_Quit
, wxT("E&xit"));
165 // now append the freshly created menu to the menu bar...
166 wxMenuBar
*menuBar
= new wxMenuBar
;
167 menuBar
->Append(menuFile
, wxT("&File"));
169 // ... and attach this menu bar to the frame
173 // create a status bar just for fun (by default with 1 pane only)
175 SetStatusText(wxT("Welcome to wxWidgets!"));
176 #endif // wxUSE_STATUSBAR
182 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
184 // true is to force the frame to close
188 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
190 wxMessageBox(wxT("This is an OLE Automation sample"),
191 wxT("About OleAuto"), wxOK
| wxICON_INFORMATION
, this);
194 /* Tests OLE automation by making the active Excel cell bold,
195 * and changing the text.
197 void MyFrame::OnTest(wxCommandEvent
& WXUNUSED(event
))
199 wxMessageBox(wxT("Excel will be started if it is not running after you have pressed OK button.")
200 wxT("\nThe active cell should then say 'wxWidgets automation test!' in bold."),
203 wxAutomationObject excelObject
;
204 if ( !excelObject
.GetInstance(wxT("Excel.Application")) )
206 wxLogError(wxT("Could not create Excel object."));
210 // Ensure that Excel is visible
211 if (!excelObject
.PutProperty(wxT("Visible"), true))
213 wxLogError(wxT("Could not make Excel object visible"));
215 const wxVariant workbooksCountVariant
= excelObject
.GetProperty(wxT("Workbooks.Count"));
216 if (workbooksCountVariant
.IsNull())
218 wxLogError(wxT("Could not get workbooks count"));
221 const long workbooksCount
= workbooksCountVariant
;
222 if (workbooksCount
== 0)
224 const wxVariant workbook
= excelObject
.CallMethod(wxT("Workbooks.Add"));
225 if (workbook
.IsNull())
227 wxLogError(wxT("Could not create new Workbook"));
232 if (!excelObject
.PutProperty(wxT("ActiveCell.Value"), wxT("wxWidgets automation test!")))
234 wxLogError(wxT("Could not set active cell value."));
237 if (!excelObject
.PutProperty(wxT("ActiveCell.Font.Bold"), wxVariant(true)) )
239 wxLogError(wxT("Could not put Bold property to active cell."));