45
WORKING WITH OBJECT VARIABLES Visual Basic for Applications

Working with object variables

  • Upload
    marinel

  • View
    46

  • Download
    2

Embed Size (px)

DESCRIPTION

Working with object variables. Visual Basic for Applications. Objectives. Explain how properties and variables are stored in memory Create an object variable using the Dim statement Select the appropriate name and data type for an object variable - PowerPoint PPT Presentation

Citation preview

Page 1: Working with object variables

WORKING WITH OBJECT VARIABLES

Visual Basic for Applications

Page 2: Working with object variables

Objectives

• Explain how properties and variables are stored in memory

• Create an object variable using the Dim statement

• Select the appropriate name and data type for an object variable

• Use the Set statement to assign an object’s address to an object variable

Page 3: Working with object variables

Objectives

• Excel:• Insert a row, value, and formula into a worksheet• Format an Excel worksheet

• Word:• Assign a theme to a Word document • Create a hyperlink in a Word document

• Access: • Open an Access report• Order the records in an Access report

Page 4: Working with object variables

Concept Lesson:Using object variables

• Every object has a set of properties

• Values of those properties control the object’s appearance and behavior

• VBA stores each property, along with its corresponding value in the internal memory

• VBA automatically reserves memory cells for the properties of every object it creates

• Memory cells have value, name and data type

Page 5: Working with object variables

Variables

• A programmer also can reserve memory cells for

storing information

• The memory cells reserved by the programmer

are called variables

• Variables created by the programmer must have

• Name

• data type

Page 6: Working with object variables

Variables

• Numeric variables• Store only numbers

• String variables• Store numbers, letters, and special characters

• Object variables• Store memory address of the object

• Make procedures easier to write and understand

• Help to improve the performance of a procedure

• Faster access to the property of an object: Application.Presentations(1).Slides(1).Name

Page 7: Working with object variables

An Object Variable and the Object to Which It Refers

Page 8: Working with object variables

Instructions to Display the First Slide Object’s Name and Layout

Properties

Exhibit 3-2: The instructions to display the first Slide object’s Name and Layout properties

Page 9: Working with object variables

Instructions to Display the First Slide Object’s Name and Layout

Properties• Without an object variable

• Application.Presentations(1).Slides(1). Name• Application.Presentations(1).Slides(1). Layout

• With object variable named sldFirst• sldFirst.Name• sldFirst.Layout

Page 10: Working with object variables

Object Variables

• Without an object variable: • VBA needs to locate the appropriate Application, Presentation, and Slide objects

in memory for each property

• With an object variable:

• VBA uses to the memory address of the object for each property

• The sldFirst object variable contains an address that points directly to the location of the Slide object in memory

Page 11: Working with object variables

Scope of Variables

• A variable’s scope refers to which procedures in the project can use the variable

• There are three scoping levels: • procedure-level

• private module-level (available only to procedures in that module)

• public module-level (available to all procedures in all modules)

• Module-level variables and constants are defined in the Declarations section of a module

• Module-level constants are private by default.

Page 12: Working with object variables

Reserving a procedure-level variable

• A procedure-level variable is declared, within a procedure, and it can be used only by the procedure in which it is declared

• Use Dim statement to reserve a procedure-level variable• The syntax of the Dim statement is

Dim variablename As datatype

where variablename represents the name of the variable

and datatype represents its data type

• Using Dim statement in a procedure VBA reserves a memory cell called variablename whose data type is datatype

Page 13: Working with object variables

Selecting the Appropriate Data Type and Name for an Object Variable

• Need to assign a data type to each variable• If an object variable will point to a

• Document object, then the object variable’s data type willbe Document

• Worksheet object, then the object variable’s data type will be Worksheet

• You also must assign a name to the variable• The variable name should help you remember

• data type • purpose of the variable

Page 14: Working with object variables

Data Types Corresponding to Some of the Objects Available

in the Microsoft Office Applications

Exhibit 3-3: The data types corresponding to some of the objects available in Microsoft Office applications

Page 15: Working with object variables

Naming Conventions• Following a naming convention is a valuable practice. • RVBA Naming Conventions

• http://www.xoc.net/standards/rvbanc.asp

• Leszynski Naming Conventions• http://en.wikipedia.org/wiki/Leszynski_naming_convention

• Microsoft Consulting Services Naming Conventions for Visual Basic• http://support.microsoft.com/?kbid=110264

• Object Hungarian Notation Naming Conventions for VB• http://support.microsoft.com/kb/q173738

• Our Notation: Define your variable beginning with three-character ID that identifies it’s data type

Page 16: Working with object variables

Data Types and Their Three-Character IDs

Exhibit 3-4: The data types and their three-character IDs

Page 17: Working with object variables

Rules and Examples for Variable Names

Exhibit 3-5: The rules and examples for variable names

Page 18: Working with object variables

Selecting the Appropriate Data Type and Name

for an Object Variable• VBA automatically initializes the object variable to the

keyword Nothing

Exhibit 3-6: Some examples of the Dim statement used to declare object variables

Page 19: Working with object variables

Using the Set Statement

• You use the Set statement to assign the address of an object to an object variable

• The syntax of the Set statement is • Set objectVariableName=object

• objectVariableName is the name of an object variable

• object is the object whose address you want to store in the variable

• The Set statement locates the object in memory and then stores the object’s address in the memory cell whose name is objectVariableName

Page 20: Working with object variables

Examples of the Set Statement

• Set sldFirst = Application.Presentations(1).Slides(1)

• Set docSales = Application.Documents(1)

• Set shtPay = Application.Workbooks(1).Workshees(1)

• Set rptBonus = Application.Reports(“bonus”)

• Set rngVBA = Application.Documents(“vba.doc”).Sentences(4)

Page 21: Working with object variables

Summary

To create a procedure-level object variable, and then assign an address to it:

• Use the Dim statement to create the variable

• Use the Set statement to assign the address of an object to an object variable

Page 22: Working with object variables

Excel: Creating the FormatWorksheet Macro Procedure

Exhibit 3-8: The sales workbook showing First Quarter worksheet

Page 23: Working with object variables

Worksheet Format Desired by the District Sales Manager

Exhibit 3-9: The worksheet format desired by the district manager

Page 24: Working with object variables

Worksheet Format Desired by the Regional Sales Manager

Exhibit 3-10: The worksheet format required by regional sales manager

Page 25: Working with object variables

Creating the FormatWorksheet Macro Procedure

Exhibit 3-11: The pseudocode for the FormatWorksheet procedure

Page 26: Working with object variables

Inserting Rows Into a Worksheet

• You insert a row into a worksheet using the syntax:• worksheetObject.Rows(rowNumber).Insert

• worksheetObject is the name of a Worksheet object• rowNumber is the row above which the new row will be inserted

Page 27: Working with object variables

Entering a Value and Formula Into aRange Object

• Recall that a row, a column, or a group of contiguous

or noncontiguous cells in a worksheet also are Excel

Range objects

Page 28: Working with object variables

Entering a Formula Into a Range Object

• You need to enter the following formulas in cells B13 through D13 in the worksheet:B13 formula = SUM (B4:B12)

C13 formula = SUM (C4:C12)

D13 formula = SUM (D4:D12)

Exhibit 3-13: The two ways of entering the SUM formulas into cells B13 through D13

Page 29: Working with object variables

Formatting and previewing Worksheet Objects

• A collection of predesigned worksheet formats is availablein Excel

Exhibit 3-14: The names of some of the Excel predesigned formats

Page 30: Working with object variables

Word Lesson:Creating the FormatPromo Macro Procedure

• A theme in Microsoft Word is a set of unified design elements and color schemes for

• Background images

• Bullets

• Fonts

• Horizontal lines

• Other document elements

Page 31: Working with object variables

Promo Document

Page 32: Working with object variables

Example of a Formatted Document

Exhibit 3-16: The formatted document

Page 33: Working with object variables

Pseudocode for theFormatPromo Procedure

Exhibit 3-17: The pseudocode for the FormatPromo procedure

Page 34: Working with object variables

Partial Listing of Themes Availablein Microsoft Word

• The default location for theme data folders is C:\Program Files\Common Files\Microsoft Shared\Themes

• You must use the folder name for the theme

Page 35: Working with object variables

Applying a Theme to a Document

• You use the ApplyTheme method to apply a theme to a document

• The syntax of the ApplyTheme method is:

• documentObject.ApplyTheme Name:=themeName

• documentObject is the name of a Document object

• themeName is the name of a theme

• Example:

• docVBA.applytheme name:=“Breeze"

Page 36: Working with object variables

ApplyTheme in Word 2007

Page 37: Working with object variables

Changing the Document’s Font Size

• You use the following syntax to change the size of the font used in the document: documentObject.Content.Font.Size=font Size

• Example:

docVBA.content.font.size = l2

Page 38: Working with object variables

Adding a Hyperlink to a Document

• You use the Add method of the Document object’s Hyperlinks collection to add a hyperlink to a document

• The syntax of the Add method is documentObject.Hyperlinks.Add Anchor:=rangeObject, Address:=linkAddress

• Example:docVBA.Hyperlinks.Add _Anchor:=docVBA.Sentences(1),Address:="http://www.Oakton.edu"

Page 39: Working with object variables

Hyperlinks.Add in Word 2010

Page 40: Working with object variables

Access Lesson:Creating and using object variables in

Access• Open Professor Martinez’s database, which is located in the

• Student Data\Tut03\Access folder

• View the StudentReport

Page 41: Working with object variables

Creating the DisplayByGrade Procedure

Exhibit 3-19: The pseudocode for the DisplayByGrade procedure

Page 42: Working with object variables

Opening an Access Report

• You use the OpenReport method of the DoCmd object to open an Access report

• The syntax of the OpenReport method is:• DoCmd.OpenReport Reportname:=reportName, View:=viewName

• where reportName is the name of a report

• viewName is the name of a view

• Example

• DoCmd.OpenReport Reportname:=“Students”

Page 43: Working with object variables

DoCmd.OpenReport in Access 2010

Page 44: Working with object variables

Valid View Names for the OpenReport Method’s

View Argument

Exhibit 3-20: The valid view names for the OpenReport method’s View argument

Page 45: Working with object variables

Ordering the Records in a Report

• To change the order of the records that appear in a report, you need first to set the Report object’s OrderByOn property to the Boolean value True