View
230
Download
4
Category
Tags:
Preview:
Citation preview
Integrate the VBA 6 Development Integrate the VBA 6 Development
Environment into your ApplicationEnvironment into your Application
Rebecca RinnerRebecca RinnerSenior Software EngineerSenior Software EngineerSummit Software CompanySummit Software Company
4-3074-307
OutlineOutline
Key benefits of VBAKey benefits of VBA Preparing for VBA IntegrationPreparing for VBA Integration Integrating VBAIntegrating VBA
What is APCWhat is APC VBA Integration ArchitectureVBA Integration Architecture Phases of VBA IntegrationPhases of VBA Integration
OutlineOutline
Key benefits of VBAKey benefits of VBA Preparing for VBA IntegrationPreparing for VBA Integration Integrating VBAIntegrating VBA
What is APCWhat is APC VBA Integration ArchitectureVBA Integration Architecture Phases of VBA IntegrationPhases of VBA Integration
Visual Basic for ApplicationsVisual Basic for Applications
The premier development technology found in applications that enables your customers to:
Customize Enable customers to tailor yourproduct to their specific needs
Leverage Deliver the power of Visual Basic to leverage the 3+ million VB developers
Integrate Enable customers to create entire line of business solutions
Buy vs. BuildBuy vs. Build
Pros Infinite Flexibility Your Code & DataCons High Risk High Cost
Pros Lower Initial Cost Fast DeploymentCons Not Flexible No Advantage
Buy Build
Buy and CustomizeBuy and Customize
Competitive Advantage!
Lower Initial Cost
Fast Deployment
Infinite Flexibility
Your Code & Data
FlexibilityFlexibility
Your application becomes a Your application becomes a platformplatform Write portions of your application in Write portions of your application in
VBAVBA Change features after you shipChange features after you ship Add features after you shipAdd features after you ship Eliminate one-off feature requestsEliminate one-off feature requests
Create a 3Create a 3rdrd party community party community
““Got VBA?”Got VBA?” VBA throughout Office 2000VBA throughout Office 2000
Outlook, FrontPageOutlook, FrontPage 100+ shipping applications 100+ shipping applications New announcements New announcements
every weekevery week
Types of ApplicationsTypes of Applications
Traditional “thick client”Traditional “thick client” Multi-threaded applicationsMulti-threaded applications
Multi-threaded designerMulti-threaded designer VBA in the Middle-tierVBA in the Middle-tier
Allows customizable business objectsAllows customizable business objects Duwamish sample on MSDNDuwamish sample on MSDN
Thin clientThin client
Check out the VBA Pavilion!Check out the VBA Pavilion!
DEMO
An Application with VBAAn Application with VBA
OutlineOutline
Key benefits of VBAKey benefits of VBA Preparing for VBA IntegrationPreparing for VBA Integration Integrating VBAIntegrating VBA
What is APCWhat is APC VBA Integration ArchitectureVBA Integration Architecture Phases of VBA IntegrationPhases of VBA Integration
VBA Building Blocks
COM-Enable Your Application
Design and Implement an Object Model
Preparing for VBA Integration
VBA Building Blocks…VBA Building Blocks…
Visual Basic Editor (VBE)Visual Basic Editor (VBE)Project ExplorerProject ExplorerCode EditorCode EditorImmediate WindowImmediate WindowProperties WindowProperties WindowLocals, Watch, ...Locals, Watch, ...VBE Object modelVBE Object model
““Visual Basic for Application Visual Basic for Application Extensibility”Extensibility”
Defined in vbe6ext.olbDefined in vbe6ext.olbCodeModuleCodeModuleCommandBarEventsCommandBarEventsVBProjectVBProjectVBComponentVBComponentReferencesReferences......
VBA Building Blocks...VBA Building Blocks...
Project HierarchyProject HierarchyVBA ProjectVBA Project
Project ItemsProject ItemsHost project ItemsHost project Items
ControlsControls**
UserFormsUserForms
Code modulesCode modules
Class modulesClass modules
Host classesHost classes****
DesignersDesigners****
* Controls are not visible in the Project Explorer window. They appear only in the Object combo box of the Code Editor** Host Classes and Designers are normally visible in the Project Explorer window, just not this one.
VBA Building Blocks...VBA Building Blocks...
VBA Project VBA Project Unit of persistenceUnit of persistence Uses IStorage to load/saveUses IStorage to load/save Often, a 1:1 association with Often, a 1:1 association with
documentsdocuments An application can have multiple VBA An application can have multiple VBA
projectsprojects Contains all code in “Project Items”Contains all code in “Project Items”
Project ItemsProject Items Host project itemsHost project items
Simply COM objectsSimply COM objects Expose events to VBAExpose events to VBA Code “behind” (using COM aggregation)Code “behind” (using COM aggregation) Can have controlsCan have controls
Host ClassesHost Classes Just like host project items, except that Just like host project items, except that
you can have multiple instances of themyou can have multiple instances of them
VBA Building Blocks...VBA Building Blocks...
VBA Building Blocks...VBA Building Blocks...
Project items...Project items... Code moduleCode module
Contain VBA code onlyContain VBA code only Global Global
Class moduleClass module Creatable VBA classesCreatable VBA classes
UserForms and Other DesignersUserForms and Other Designers UserForms (built-in)UserForms (built-in)
Can be used to build UI for your applicationCan be used to build UI for your application
VB6 DesignersVB6 Designers
VBA Building Blocks...VBA Building Blocks... Code ExecutionCode Execution
Macros dialog boxMacros dialog box Named macros executedNamed macros executed
in response to UIin response to UI Toolbar, menu selection, Toolbar, menu selection,
keystrokekeystroke Can pass parametersCan pass parameters
EventsEvents Code behindCode behind
VBA Building Blocks
COM-Enable Your Application
Design and Implement an Object Model
Preparing for VBA Integration
COM-Enable Your COM-Enable Your ApplicationApplication
Follow the COM SpecificationFollow the COM Specification Adopt the COM PhilosophyAdopt the COM Philosophy Use COM as the Binary Standard for Use COM as the Binary Standard for
Application ComponentsApplication Components For MFC applications, use ATL for For MFC applications, use ATL for
COM support.COM support.
COM-Enable Your COM-Enable Your Application... Application...
ApplicationApplication
““Peer”Peer”COM ObjectsCOM Objects
You don’t have to rewrite your You don’t have to rewrite your applicationapplication Use “peer” objectsUse “peer” objects
C++ or MFCC++ or MFCclassesclasses
APC APC VBAVBA
COM-Enable Your COM-Enable Your Application... Application...
ApplicationApplication
VB ClassesVB Classes
VB Applications are COM “friendly”COM object model exposed as VB classes
APC APC VBAVBA
VBA Building Blocks
COM-Enable Your Application
Design and Implement an Object Model
Preparing for VBA Integration
Design and ImplementDesign and Implementan Object Modelan Object Model
Definition: Definition: An Object Model is the set An Object Model is the set of objects, methods, and properties of objects, methods, and properties your application exposes for your application exposes for programmabilityprogrammability
Required for VBA integrationRequired for VBA integration
Why Expose an Object Model?Why Expose an Object Model?
Allow your application to be part of bigger Allow your application to be part of bigger solutionssolutions Can be driven by a wide variety of tools Can be driven by a wide variety of tools
(VBA, VB, VC++, Delphi, and others)(VBA, VB, VC++, Delphi, and others) Create a 3rd party community supporting Create a 3rd party community supporting
your productyour product Required for VBA integrationRequired for VBA integration
Object Model Design TipsObject Model Design Tips
Use standard constructs and behavior Use standard constructs and behavior Users will already know it.Users will already know it. Users will intuitively understand it.Users will intuitively understand it. VBA will not choke on it.VBA will not choke on it.
Events provideEvents provide Respond to user actionsRespond to user actions Respond to detected conditionsRespond to detected conditions Hook application internalsHook application internals
See the Summit white paperSee the Summit white paper
DEMO
Object ModelObject Model
OutlineOutline
Key benefits of VBAKey benefits of VBA Preparing for VBA IntegrationPreparing for VBA Integration Integrating VBAIntegrating VBA
What is APCWhat is APC VBA Integration ArchitectureVBA Integration Architecture Phases of VBA IntegrationPhases of VBA Integration
Q: How do you integrate VBA?Q: How do you integrate VBA?
Integrating VBAIntegrating VBA
?A: Use Microsoft APC.A: Use Microsoft APC.
Application Programmability Component
!UseAPC
What is APC?What is APC?
APC APC Application Programmability Application Programmability ComponentComponent
COM object with interfaces used to COM object with interfaces used to host VBA (IApc*)host VBA (IApc*)
Can be used from most COM Can be used from most COM consumers (VB, MFC, ATL, C++, consumers (VB, MFC, ATL, C++, Delphi, etc.)Delphi, etc.)
What is Required for Your What is Required for Your Application to use APC?Application to use APC?
Must be able to call COM interfacesMust be able to call COM interfaces Must sink COM source interfacesMust sink COM source interfaces If your application needs to show If your application needs to show
the VBA Editor…the VBA Editor… ……it must provide access to its it must provide access to its
message loopmessage loop
VBA Integration ArchitectureVBA Integration Architecture
APC COM Component (IApc)
Integration Code
Microsoft VBA (IVba*)
Core VBA Code
MFC C++ VB Delphi…
Application Code
APC/C++ (CApc*)
APC/MFCTemplate Code
ApcApc
APC Object Model (Simplified)APC Object Model (Simplified)
ProjectProject
Projects
ProjectItemProjectItem
ProjectItems
ControlControl
Controls
Code ModuleCode Module
Class ModuleClass Module
UserFormUserForm
Host ClassHost Class
Designer ItemDesigner Item
Host Project ItemHost Project Item
Object
Collection
Phase 1: Initial Steps
Phase 2: Projects
Phase 3: (Optional) Project Items
VBA Integration Tasks
Phase 4: (Optional) Controls
Sim
ple
Sim
ple
Mo
derate
Mo
derate
Co
mp
lex C
om
plex
STOPSTOP
STOPSTOP
STOPSTOP
Note: Advanced Features can be added to any completed VBA integration project.
Phase 1: Initial StepsPhase 1: Initial Steps Initializing APCInitializing APC
Create the APC HostCreate the APC Host Associate the Application ObjectAssociate the Application Object Manage windows and eventsManage windows and events Show the VBA IDEShow the VBA IDE
Phase 1: Initial StepsPhase 1: Initial Steps Initializing APCInitializing APC
What is the Application object?What is the Application object? Merges methods & properties into Merges methods & properties into
namespacenamespace
Sub Main MsgBox “Hello” r = Application.Foo r = FooEnd Sub
MsgBox is a VBA built-in keywordFoo is a method of the Application object
Foo is merged into VBA’s global namespace and can be called without further qualification
Phase 1: Initial StepsPhase 1: Initial Steps Managing windows and eventsManaging windows and events
VBA IDE is a top-level windowVBA IDE is a top-level window Window parentingWindow parenting Tracking the active componentTracking the active component
Forward accelerators to active Forward accelerators to active componentcomponent
Re-entrant message loopRe-entrant message loop Isolate to a single set of routinesIsolate to a single set of routines
DEMO
Phase 1: Initial stepsPhase 1: Initial steps
Phase 2: ProjectsPhase 2: Projects
Project is the unit of persistenceProject is the unit of persistence Create, Save, Load a ProjectCreate, Save, Load a Project Execute codeExecute code An application can have multiple An application can have multiple
VBA projectsVBA projects
Phase 2: ProjectsPhase 2: ProjectsPersistencePersistence Requires OLE structured storage Requires OLE structured storage
(IStorage)(IStorage) Disk-based Disk-based
In your document fileIn your document file In standalone fileIn standalone file
In-memoryIn-memory Stored in a database as a BLOBStored in a database as a BLOB
Must be transactedMust be transacted IApcProject supports IPersistStorageIApcProject supports IPersistStorage For VB Developers, APC supports:For VB Developers, APC supports:
Storage ObjectStorage Object Stream ObjectStream Object
Phase 2: ProjectsPhase 2: ProjectsExecuting VBA codeExecuting VBA code
Named macros -- use the Run methodNamed macros -- use the Run methodTheProject.Run "ThisDocument", "Main"TheProject.Run "ThisDocument", "Main"
Available for ProjectItem, Procedure, Available for ProjectItem, Procedure, and Projectand Project
IApcProjectItem::RunIApcProjectItem::Run
IApcProcedure::RunIApcProcedure::Run
IApcProject::RunIApcProject::Run
Macros dialog...Macros dialog...Dim TheScopes As MSAPC.ScopesDim TheScopes As MSAPC.Scopes
Set TheScopes = TheProject.CreateScopesSet TheScopes = TheProject.CreateScopes
TheScopes.MacrosDialog.ShowTheScopes.MacrosDialog.Show
Phase 2: ProjectsPhase 2: ProjectsOther usesOther uses Code sharing / reuseCode sharing / reuse
Microsoft Word templates Microsoft Word templates (NORMAL.DOT)(NORMAL.DOT)
Global libraryGlobal library Custom event routingCustom event routing
Event goes first to main host project Event goes first to main host project itemitem
If event not handled, routes to If event not handled, routes to templatetemplate
DEMO
Phase 2: ProjectsPhase 2: Projects
Phase 3: Project ItemsPhase 3: Project Items
Used to expose an object in your Used to expose an object in your COM object model’s hierarchyCOM object model’s hierarchy
VBA user can write VBA code to VBA user can write VBA code to handle eventshandle events
VBA user can write code behindVBA user can write code behind
Phase 3: Project ItemsPhase 3: Project Items
Host project items are simply COM Host project items are simply COM objectsobjects Must derive from IDispatch and Must derive from IDispatch and
support IProvideClassInfosupport IProvideClassInfo Method’s parameters strongly typed Method’s parameters strongly typed
and namedand named Coclass exposes a source interface Coclass exposes a source interface
(events)(events)
Phase 4: ControlsPhase 4: Controls
VBA controls are simply COM VBA controls are simply COM objectsobjects
TypesTypes Built-in Built-in COM objects COM objects External External ActiveX controls (.OCX) ActiveX controls (.OCX)
Placed in the VBA hierarchy on a Placed in the VBA hierarchy on a host project itemhost project item
Control name merged into Control name merged into namespacenamespace
DEMO
Phases 3 & 4: Phases 3 & 4: Project Items and ControlsProject Items and Controls
Phase 1: Initial Steps
Phase 2: Projects
Phase 3: (Optional) Project Items
VBA Integration Phases: Summary
Phase 4: (Optional) Controls
Sim
ple
Sim
ple
Mo
derate
Mo
derate
Co
mp
lex C
om
plex
STOPSTOP
STOPSTOP
STOPSTOP
Note: Advanced Features can be added to any completed VBA integration project.
Advanced VBA FeaturesAdvanced VBA Features
VBA Extensibility Object ModelVBA Extensibility Object Model Digital SignaturesDigital Signatures COM Add-insCOM Add-ins ActiveX DesignersActiveX Designers
DEMO
Advanced FeaturesAdvanced Features
OutlineOutline
Key benefits of VBAKey benefits of VBA Preparing for VBA IntegrationPreparing for VBA Integration Integrating VBAIntegrating VBA
What is APCWhat is APC VBA Integration ArchitectureVBA Integration Architecture Phases of VBA IntegrationPhases of VBA Integration
For more information…For more information…
http://msdn.microsoft.com/vbahttp://msdn.microsoft.com/vba http://www.summsoft.com/http://www.summsoft.com/vbavba
Recommended