Using VB with MS Applications R. Juhl, Delta College.

56
CST-273 Fall 2008 Using VB with MS Applications R. Juhl, Delta College

Transcript of Using VB with MS Applications R. Juhl, Delta College.

Page 1: Using VB with MS Applications R. Juhl, Delta College.

CST-273 Fall 2008Using VB with MS Applications

R. Juhl, Delta College

Page 2: Using 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

Page 3: Using VB with MS Applications R. Juhl, Delta College.

Getting to Know VBA - Excel

Page 4: Using VB with MS Applications R. Juhl, Delta College.

Getting to Know VBA - PowerPoint

Page 5: Using VB with MS Applications R. Juhl, Delta College.

Getting to Know VBA - Word

Page 6: Using VB with MS Applications R. Juhl, Delta College.

Getting to Know VBA - Access

Page 7: Using VB with MS Applications R. Juhl, Delta College.

Getting to Know VBA

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

Page 8: Using VB with MS Applications R. Juhl, Delta College.

Getting to Know VBA

• The “Developer Tab” is now added:

Page 9: Using VB with MS Applications R. Juhl, Delta College.

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.

Page 10: Using VB with MS Applications R. Juhl, Delta College.

Getting to Know VBA

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

Page 11: Using VB with MS Applications R. Juhl, Delta College.

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

Page 12: Using VB with MS Applications R. Juhl, Delta College.

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

Page 13: Using VB with MS Applications R. Juhl, Delta College.

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

Page 14: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 15: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

• The Integrated Development Environment Parts

Page 16: Using VB with MS Applications R. Juhl, Delta College.

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

Page 17: Using VB with MS Applications R. Juhl, Delta College.

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

Page 18: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

• The Integrated Development Environment Parts

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

Page 19: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE – VBA Toolbox

Page 20: Using VB with MS Applications R. Juhl, Delta College.

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

Page 21: Using VB with MS Applications R. Juhl, Delta College.

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 ?

Page 22: Using VB with MS Applications R. Juhl, Delta College.

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 ?

Page 23: Using VB with MS Applications R. Juhl, Delta College.

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 ?

Page 24: Using VB with MS Applications R. Juhl, Delta College.

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 )

Page 25: Using VB with MS Applications R. Juhl, Delta College.

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 )

Page 26: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

o

• Setting up security (to run macros / VBA)

Page 27: Using VB with MS Applications R. Juhl, Delta College.

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

Page 28: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

• Project Explorer - Word Project•Documents •Doucment Templates

•Project Explorer - Excel Project•Worksheets•Workbooks

•Project Explorer - Access Project•Database(s)

Page 29: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 30: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 31: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 32: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 33: Using VB with MS Applications R. Juhl, Delta College.

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

Page 34: Using VB with MS Applications R. Juhl, Delta College.

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

Page 35: Using VB with MS Applications R. Juhl, Delta College.

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

Page 36: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

Page 37: Using VB with MS Applications R. Juhl, Delta College.

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

Page 38: Using VB with MS Applications R. Juhl, Delta College.

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

Page 39: Using VB with MS Applications R. Juhl, Delta College.

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.

Page 40: Using VB with MS Applications R. Juhl, Delta College.

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

Page 41: Using VB with MS Applications R. Juhl, Delta College.

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

Page 42: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 43: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 44: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 45: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

• Using the Immediate window• Select View Immediate Window

Page 46: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 47: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

MsgBox Myvar

Page 48: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

• IDE Object Browser•Use View Object Browser

Page 49: Using VB with MS Applications R. Juhl, Delta College.

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.

Page 50: Using VB with MS Applications R. Juhl, Delta College.

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.

Page 51: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

And their properties / Functions

Page 52: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

• The Object Browser • Applications

• Library has the name of the application

Page 53: Using VB with MS Applications R. Juhl, Delta College.

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

Page 54: Using VB with MS Applications R. Juhl, Delta College.

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

Page 55: Using VB with MS Applications R. Juhl, Delta College.

Looking at the IDE

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

Page 56: Using VB with MS Applications R. Juhl, Delta College.

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