using vb with ms applications r. juhl, delta college

Post on 01-Apr-2015

220 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CST-273 Fall 2008Using VB with MS Applications

R. Juhl, Delta College

Getting to Know VBA Visual Basic for Applications (VBA)

• VBA works with many applications including Microsoft Office

• Use VBA to:• accomplish tasks automatically

• Event driven or when a user generates a stimulus• change the application environment

• VBA is integrated with all Microsoft Office products• In Office 2007 you have to enable VBA

• MS Word, Excel, and Powerpoint• Located in the Developer Tab

• MS Access• Located on the “Database Tools “ Tab

Getting to Know VBA - Excel

Getting to Know VBA - PowerPoint

Getting to Know VBA - Word

Getting to Know VBA - Access

Getting to Know VBA

• To enable VBA Click on:• ‘Show Developer tab in the Ribbon’

Getting to Know VBA

• The “Developer Tab” is now added:

Getting to Know VBA

Note that other 2007 Office products:

• Visio• Project• Publisher

and previous products in Office 2003 do not use the Ribbon Interface.

Getting to Know VBA

• You access VBA in these products by selecting the:• Tools Macro menu

Getting to Know VBA

• Other product s use VBA, too:

• In reference to our text book……..

• MSDN or Microsoft Developer Network website

http://msdn.microsoft.com/en-us/isv/bb190538.aspx

• States Microsoft's position on Licensing VBA

• Contains Support links for using VBA

Getting to Know VBA

• Use VBA to automate :• Documents • Compose letters by picking predetermined text

• Spreadsheets• Click a button for predefined automatic data entry

• Database• Importing Data and performing calculations• Labor Loading

Getting to Know VBA

• Use VBA to automate :

• Customizing an application interface• Create new version of features• Exclude unwanted information• Include only the information needed

• Performing calculations• Change data before you use it• Simplify by using small steps rather than large

Looking at the IDE

• Select the ‘Visual Basic’ Icon from the ‘Developer’ Menu

Looking at the IDE

• The Integrated Development Environment Parts

Looking at the IDE

• The Integrated Development Environment Parts

• Project Explorer• Contains• List of items in the project• Contains all project elements in a single file• VB Application exists within project file

Looking at the IDE

• The Integrated Development Environment Parts

• Properties• Contains• Properties or Attributes of the object selected• Color• Words on it• Boolean attributes• Password

Looking at the IDE

• The Integrated Development Environment Parts

• Code• Contains• The VB code needed to make the application• Subroutines• Functions• Procedures

Looking at the IDE – VBA Toolbox

Looking at the IDE

• IDE Supports Forms: You decide:• What appears on the form• How the form acts / user interaction

• Use the toolbox• To create controls used in forms• Text Box, Labels, Check box, etc…• More in Chapter 7

Looking at the IDE

• A note on objects

• Your VBA programs will contain objects• A form could be an object

• The Apple Metaphor • Properties ? • Methods ?

Looking at the IDE

• A note on objects

• Your VBA programs will contain objects• A form could be an object

• The Apple Metaphor • Properties ? (color, taste)• Methods ?

Looking at the IDE

• A note on objects

• Your VBA programs will contain objects• A form could be an object

• The Apple Metaphor • Properties ? (color, taste)• Methods ?

Looking at the IDE

• A note on objects

• Your VBA programs will contain objects• A form could be an object

• The Apple Metaphor • Properties ? (color, taste)• Methods ? ( pick, eat )

Looking at the IDE

o • A note on objects

• Your VBA programs will contain objects• A form could be an object

• The Apple Metaphor • Properties ? (color, taste)• Methods ? (pick, eat )• Events ? ( changes color over time )

Looking at the IDE

o

• Setting up security (to run macros / VBA)

Looking at the IDE

• Setting up security (to run macros / VBA)• Select the Developer or Database Tools Tab• Click Macro Security

• Using Project Explorer• Interact with the objects that make up the project• Objects listed depend on the application

Looking at the IDE

• Project Explorer - Word Project•Documents •Doucment Templates

•Project Explorer - Excel Project•Worksheets•Workbooks

•Project Explorer - Access Project•Database(s)

Looking at the IDE

• Project Explorer can also contain other objects:• Forms – User interface• Modules – Nonvisual code• Class Modules – Contain new object to build

Looking at the IDE

• Project Explorer – Right click to find context menu properties (more in chapter 3)

Looking at the IDE

• Working with special entries• In Word you may see a reference folder

Looking at the IDE

• Working with special entries• In Word you may see a reference folder

Looking at the IDE

• Working with special entries• In Word you may see a reference folder• It may contain templates• In many cases you can’t modify objects in the special folder• Its used for information only

•You normally don’t need to work with these objects

Looking at the IDE

• Using the Properties Window• Most objects in the VBA IDE have properties• Properties describe objects• Some common properties are:• text• boolean• i.e. visible property may be•True •False

Looking at the IDE

• Using the Properties Window• Object properties may also have:• Numeric value• i.e. to place a control use the• left / top properties set to numbers

• Drop down list• Color property

• For help with properties•Highlight the property and press

F1

Looking at the IDE

Looking at the IDE

• Using the Code Window• Where you write application code

Sub SayHello()

' Display the message box Result = MsgBox("Click a Button Please", vbYesNoCancel, "A Messege") 'See which button the user pressed. MsgBox Result

End Sub

Looking at the IDE

• Using the Code Window• Where you write application code

Sub SayHello()

' Display the message box Result = MsgBox("Click a Button Please", vbYesNoCancel, "A Messege") 'See which button the user pressed. MsgBox Result

End Sub

Looking at the IDE

• Using the Code Window• To open existing code• Click on the module folder and open the module you want to edit.

Looking at the IDE

• Using the Code Window• The code window also automatically appears when you perform other tasks• When you double click on a forms control:• The code window opens for that control•Allows you to edit code for the event handler that corresponds to the control

Looking at the IDE

• Creating a new code window• From the IDE select Insert Module• It appears as a new code window• It appears as a module in the Project / Module folder

Looking at the IDE

• Typing text in the Code window• If you make an error VBA checks what you type.

Looking at the IDE

• The code window has a context menu (right click)

Looking at the IDE

• Code context help is available by right-clicking the key code word

Looking at the IDE

• Using the Immediate window• Select View Immediate Window

Looking at the IDE

• You can type code directly into the immediate window to test…

Looking at the IDE

• The immediate window remembers the variable state…. • Using MyVar in the previous slide and typing

MsgBox Myvar

Looking at the IDE

• IDE Object Browser•Use View Object Browser

Looking at the IDE

• The Object Browser • Provides all the VBA objects at your disposal• Objects are the fundamental building block of Visual Basic;• Nearly everything you do in Visual Basic involves modifying objects. • Every element of Microsoft Word — documents, tables, paragraphs, bookmarks, fields and so on — can be represented by an object in Visual Basic.

Looking at the IDE

• The Object Browser • Provides all the VBA objects at your disposal• Objects are the fundamental building block of Visual Basic;• Nearly everything you do in Visual Basic involves modifying objects. • Every element of Microsoft Word — documents, tables, paragraphs, bookmarks, fields and so on — can be represented by an object in Visual Basic.

Looking at the IDE

• The Object Browser • Lists• Libraries of:• Objects • Modules• Forms• Methods• Projects

And their properties / Functions

Looking at the IDE

• The Object Browser • Applications

• Library has the name of the application

Looking at the IDE

• The Object Browser Office Library • Lists• Objects that Microsoft Office supports• Used to find support for Office Assistant• 2003

• The Object Browser also contains the current • VBA project

Looking at the IDE

• The Object Browser Lists• StdOLE Library• Object Linking and Embedding• Embed a picture in a document• Link one document to anther

• VBA Library• Special Utility Objects• MsgBox function

Looking at the IDE

• The Object Browser Searching • Type in the keyword / method etc….

Looking at the IDE

• You can cut & paste from the Object Browser by:• Selecting the item• Clicking the Double Document Icon• Copies to the clipboard

•Then pasting directly into your application

top related