120 likes | 253 Views
Visual Basic Programming II Lecture 13. MIS233 Instructor – Larry Langellier. This Week – Working with the MS Office Suite. Controlling Office Applications from VB Excel Object Model Controlling Excel programmatically Introduction to VBA in Office Applications
E N D
Visual Basic Programming IILecture 13 MIS233 Instructor – Larry Langellier
This Week – Working with the MS Office Suite • Controlling Office Applications from VB • Excel Object Model • Controlling Excel programmatically • Introduction to VBA in Office Applications • Explore with the Macro Recorder • Using the Object Browser to explore object models
Working with Objects from Microsoft Office • Microsoft Office Suite • Word • Excel (which we’ll focus on today) • Access • PowerPoint • Outlook/Exchange • Stand-alone Visual Basic applications can interact with the MS Office components • You can also use Visual Basic within an MS Office application (VBA) • The capabilities of MS Office products serve as useful add-ons to all kinds of programming tasks
Controlling Office Apps from VB • MS Office Applications are implemented as ActiveX EXE servers • They contain a public object library that can be used by other applications • They can service multiple applications • The MS Office application can be running on a different machine • A single user-application can call on the services of more than one MS Office application
Connecting to an MS Office application • Set a Project Reference • CreateObject() • Running an instance of Excel - Example 26.1 Dim xlapp As Excel.Application Private Sub cmdStart_Click() Set xlapp = CreateObject(“Excel.Application”) xlapp.Visible = True End Sub Private Sub cmdStop_Click() xlapp.Quit Set xlapp = Nothing End Sub
Exploring with the Object Browser • Set a reference to an MS Office application • Open Object Browser
Exploring with VBA Macros • Each MS Office application provides a VB development environment called Visual Basic for Applications • Let’s look at Macros in Excel • Macros.xls • Macro Edit • Extensive programming can be done directly inside of the Office application using VBA • Identical results can be achieved directly from Visual Basic also – using the ActiveX object wrappers provided for each MS Office Suite application
The Excel Object Model • Application • ActiveSheet, ActiveCell • Workbooks, Worksheets • Visible • Width, Height, Top, Left • Run (Macro, args…) • Quit() • Workbooks • Count, Item • Add(), Open(), Close(), OpenText() • Workbook • ActiveSheet, Sheets, Name • Close()
Excel Object Model (cont.) • Worksheet • Cells, Columns, Rows, Range • Name • Visible • Calculate() • Range • Formula, Value • EntireRow, EntireColumn • Name • NumberFormat, MergeCells, Orientation • Example • Exercise 26.4
Excel Example • Let’s add an Excel Report to the Baseball 3-Tier Solution we’ve been developing for the past few weeks • Put the statistics for the players into an Excel Spreadsheet • Examples • Using Existing Macros • Calling existing Macros in a Excel Spreadsheet – Macros.xls • Reports Menu -> Format Existing Report • Programmatically from within Visual Basic • Using the Excel Object Wrapper • Reports Menu -> Team Hitting
Details… • Next Class • Finals Week • Final Project Presentations • Attendance is required • Additional courses… • MIS288 – Visual Basic Software Development • MIS287 – Visual Basic for Applications • OSA257 – Database Management • MIS292 – SQL/Database Applications • Other Languages/Technologies • C++ • Java • Web • eCommerce