Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Variables Visual Basic for Applications 3.

Similar presentations


Presentation on theme: "Object Variables Visual Basic for Applications 3."— Presentation transcript:

1 Object Variables Visual Basic for Applications 3

2 Objectives n In this tutorial, you will learn how to: n Explain how properties and variables are stored in memory n Create an object variable using the Dim statement n Select the appropriate name and data type for an object variable n Use the Set statement to assign an object’s address to an object variable n Insert a row, value, and formula into an Excel worksheet using VBA code 3

3 Objectives n In this tutorial, you will learn how to: n Format an Excel worksheet using VBA Code n Assign a theme to a Word document using VBA code n Create a hyperlink in a Word document using VBA code n Open an Access report using VBA code n Order the records in an Access report using VBA code 3

4 Concept Lesson: Memory Cells n Every object in a VBA-enabled application has a set of properties whose values control the object’s appearance and behavior n VBA stores each property, along with its corresponding value, inside the computer in an area called internal memory n When VBA creates an object, it reserves a group of boxes (memory cells) in which it stores information about the object 3

5 Illustration of Memory Cells that Store Property Values 3

6 How Each Object Occupies a Separate Section in Memory 3

7 Memory Cells n In addition to assigning both a name and a value to each property’s memory cell, VBA also assigns a data type n The data type refers to the type of data the memory cell can store n You can determine the type of data that a property's memory cell can store by viewing the property’s Help screen 3

8 Variables n A programmer also can reserve memory cells for storing information n The memory cells reserved by the programmer are called variables n Like all reserved memory cells, the variables that you create must have both a name and a data type 3

9 Variables n Numeric variables, for example, can store only numbers, while String variables can store numbers, letters, and special characters, such as the dollar sign ($) n Programmers use object variables to make procedures easier to write and understand n Object variables also help to improve the performance of a procedure by allowing the procedure to run more quickly 3

10 Object Variables 3 n An object variable is a memory cell (box) that contains the address of an object in memory n The address tells VBA where the object is located n The address contained in an object variable “points” to the location of an object in memory n When VBA processes the MsgBox Prompt: =Application.Presentations(1).Slides(1).Name instruction, first it must locate the appropriate Application object in memory, then it must locate the first Presentation object within the Presentations collection, followed by the first Slide object within the Slides collection

11 An Object Variable and the Object to Which It Refers 3

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

13 Object Variables n VBA repeats this process when it executes the MsgBox Prompt:=Application.Presentations(1).Slides(1).Layout instruction—once again having to locate the appropriate Application, Presentation, and Slide objects in memory n If you assign the Slide object’s address to an object variable named sldFirst, you can use the last two instructions shown in Figure 3-4 to display the Slide object’s Name and Layout properties 3

14 Object Variables 3 n The sldFirst object variable contains an address that points directly to the location of the Slide object in memory n A variable’s scope refers to which procedures in the project can use the variable n Variables in VBA can have one of three scopes: – Procedure-level – Module-level – Public

15 Reserving a Procedure-Level Variable n A procedure-level variable is reserved, or declared, within a procedure, and it can be used only by the procedure in which it is declared n You use the VBA Dim statement to reserve a procedure-level variable n The syntax of the Dim statement is Dim variablename As datatype, where variablename represents the name of the variable (memory cell) and datatype represents its data type n When VBA processes the Dim statement in a procedure, it reserves a memory cell to which it assigns variablename as the name and datatype as the data type 3

16 Selecting the Appropriate Data Type and Name for an Object Variable 3 n You must assign a data type to each of the variables (memory cells) that you reserve n If an object variable will point to a Document object, then the object variable’s data type will be Document n If an object variable will contain the address of a Worksheet object, then the object variable’s data type will be Worksheet n In addition to assigning a data type to a variable, you also must assign a name to the variable n The name should help you remember both the data type and purpose of the variable

17 Data Types Corresponding to Some of the Objects Available in the Microsoft Office Applications 3

18 Data Types and Their Three-Character IDs 3

19 Selecting the Appropriate Data Type and Name for an Object Variable n It is a common practice to type the three- character ID in lowercase and capitalize the first letter in the part of the name that identifies the purpose n In addition to being descriptive, the name that a programmer assigns to a variable must follow several specific rules n When you use the Dim statement to declare an object variable, VBA reserves a memory cell to which it attaches variablename as the name and datatype as the data type 3

20 Rules and Examples for Variable Names 3

21 Selecting the Appropriate Data Type and Name for an Object Variable 3 n VBA also automatically stores the keyword Nothing in the object variable, which is referred to as initializing the variable

22 Using the Set Statement n You use the Set statement to assign the address of an object to an object variable n The syntax of the Set statement is Set objectVariableName=object, where objectVariableName is the name of an object variable, and object is the object whose address you want to store in the variable n The Set statement locates the object in memory and then stores the object’s address in the memory cell whose name is objectVariableName 3

23 Examples of the Set Statement 3

24 Summary To create a procedure-level object variable, and then assign an address to it: n Use the Dim statement to create the variable n For a listing of data types corresponding to some of the objects available in the Microsoft Office applications refer to Figure 3-5 n Use the Set statement to assign the address of an object to an object variable 3

25 Excel Lesson: Creating the FormatWorksheet Macro Procedure n To open Martin’s workbook, and then insert a module and a procedure, use the steps on pages 168 to 170 of the textbook 3

26 Add Procedure Dialog Box 3

27 Worksheet Format Desired by the District Sales Manager 3

28 Worksheet Format Desired by the Regional Sales Manager 3

29 Creating the FormatWorksheet Macro Procedure 3 n Pseudocode, which is composed of short English statements, is a tool programmers use to help them plan the steps that a procedure must take in order to perform an assigned task

30 Inserting Rows Into a Worksheet n You insert a row into a worksheet using the syntax worksheetObject.Rows(rowNumber).Insert, where worksheetObject is the name of a Worksheet object and rowNumber is the number of rows above which the new row will be inserted 3

31 Inserting Rows Into a Worksheet n To create an object variable and then use it to enter instructions, use the steps on pages 174 and 175 of the textbook 3

32 Entering a Value Into a Range Object n A row, a column, or a group of contiguous or noncontiguous cells in a worksheet also are Excel Range objects n To enter the instruction that will assign “Paradise Electronics” to cell A1, use the steps on pages 175 and 176 of the textbook 3

33 Entering a Formula Into a Range Object 3 n 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) n These formulas will add the contents of their respective columns

34 Formatting a Range Object n A collection of predesigned worksheet formats is available in Excel n To view a complete listing of the Excel predesigned formats, display the AutoFormat method’s Help screen n To enter the instruction that formats the worksheet, use the steps on page 178 of the textbook 3

35 Current Status of the FormatWorksheet Procedure 3

36 Previewing and Printing a Worksheet Object n To complete the FormatWorksheet procedure, then save and run the procedure, use the steps listed on pages 179 to 181 of the textbook 3

37 First Quarter Worksheet After Running the FormatWorksheet Macro 3

38 Word Lesson: Creating the FormatPromo Macro Procedure n Begin by opening Pat’s document, which is located in the Tut03\Word folder on your Data Disk n To open Pat’s document, and then insert a module and a procedure, use the steps on pages 184 to 186 in the textbook n A theme in Microsoft Word is a set of unified design elements and color schemes for background images, bullets, fonts, horizontal lines, and other document elements 3

39 Promo Document 3

40 Add Procedure Dialog Box 3

41 Example of a Formatted Document 3

42 Pseudocode for the FormatPromo Procedure 3

43 Partial Listing of Themes Available in Microsoft Word 3

44 Applying a Theme to a Document n You use the ApplyTheme method to apply a theme to a document n The syntax of the ApplyTheme method is documentObject.ApplyThemeName:=themeName, where documentObject is the name of a Document object and themeName is the name of a theme n To create an object variable named docPromo and then use it in the ApplyTheme instruction, use the steps on page 189 of the textbook 3

45 Changing the Document’s Font Size n You use the following syntax to change the size of the font used in the document: documentObject.Content.Font.Size=font Size n To change the document’s font size to 14 points: 1.Type ’change document’s font size and press the Enter key 2.Type docpromo.content.font.size=14 and press the Enter key 3

46 Adding a Hyperlink to a Document 3 n You use the Add method of the Document object’s Hyperlinks collection to add a hyperlink to a document n The syntax of the Add method is documentObject.Hyperlinks.Add Anchor:=rangeObject, Address:=linkAddress n To convert the club’s name to a hyperlink, use the steps on pages 190 to 192 of the textbook

47 Completed FormatPromo Procedure 3

48 Promo Document After Running the FormatPromo Macro 3

49 Access Lesson: Creating the DisplayByGrade Procedure n Begin by opening Professor Martinez’s database, which is located in the Tut03\Access folder on your Data Disk n Then view the StudentReport n To open Professor Martinez’s database, then view the StudentReport report, use the steps on pages 195 and 196 of the textbook n To begin creating the DisplayByGrade procedure, use the steps on pages 196 and 197 of the textbook 3

50 Maximized StudentReport Report 3

51 Creating the DisplayByGrade Procedure 3

52 Opening an Access Report n You use the OpenReport method of the DoCmd object to open an Access report n The syntax of the OpenReport method is DoCmd.OpenReport Reportname:=reportName, View:=viewName, where reportName is the name of a report and viewName is the name of a view n To create an object variable named rptStudent and then assign the StudentReport object’s address to it, use the steps on pages 199 and 200 of the textbook 3

53 Valid View Names for the OpenReport Method’s View Argument 3

54 Ordering the Records in a Report n 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 n To complete the DisplayByGrade procedure, use the steps on pages 200 and 201 of the textbook 3

55 Creating the DisplayByGradeMacro Macro 3 n You use the Macro window to create an Access macro n The macro then can be run from either the Macro window or the Database window in Access n To create the DisplayByGradeMacro macro, and then save and run the macro, use the steps on pages 201 to 203 of the textbook

56 Macro Window Showing the Completed DisplayByGradeMacro Macro 3

57 StudentReport Report in Grade Order 3


Download ppt "Object Variables Visual Basic for Applications 3."

Similar presentations


Ads by Google