0% found this document useful (0 votes)
9 views

Visual Basic For Applications Lesson Nine: Vba Objects and Properties

This document provides an overview of objects and properties in Visual Basic for Applications (VBA). There are two types of objects - built-in objects like command buttons and user-defined objects declared using the Type statement. An object represents an element of an application, like a cell or button, and its properties can be set or referenced in code. The document explains that when you type the name of an object in the VBA editor followed by a period, its valid properties are displayed. It provides examples of referencing properties of the ActiveDocument, Error, and Hyperlinks objects. User-defined objects can be created using the Type statement to define custom data types with elements like ID and Name, as demonstrated in an example.

Uploaded by

Patao Clp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Visual Basic For Applications Lesson Nine: Vba Objects and Properties

This document provides an overview of objects and properties in Visual Basic for Applications (VBA). There are two types of objects - built-in objects like command buttons and user-defined objects declared using the Type statement. An object represents an element of an application, like a cell or button, and its properties can be set or referenced in code. The document explains that when you type the name of an object in the VBA editor followed by a period, its valid properties are displayed. It provides examples of referencing properties of the ActiveDocument, Error, and Hyperlinks objects. User-defined objects can be created using the Type statement to define custom data types with elements like ID and Name, as demonstrated in an example.

Uploaded by

Patao Clp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

VISUAL BASIC FOR APPLICATIONS

LESSON NINE: VBA OBJECTS and PROPERTIES


There are two types of Objects in VBA.
Inbuilt Objects such as CommandButtons,
AND .
User defined Objects declared using the Type statement.

An Object represents an element [part] of an application, such as a cell, a form, a bookmark or a button. In Visual
Basic code, you must identify an object before you can reference [refer to] one of its properties.
You have already used Objects and Properties in these lessons .
In Lesson Three

OptionButtton2.value = False
Etc.
Here you set the Value property of the object OptionButton2 to False.

In Lesson Eight:

If Err.Number = 13 Then
Etc.
Here you asked if the Number property of the object Err was 13.
SO
The property of an object can either be set [given a value], or referenced. [Its value asked or used]
You will find that in the VB Editor, when you type the name of an object, then type a . directly after it,
indicating you wish to reference it [refer to it], you will be prompted with a list of valid properties. Try it now.
Create a Command button, then enter the VBA Editor.
Copy in the following script.
Dim Message
Message = Err.Number
MsgBox(Message)
The result should be 0, because there is no error.
Try some of the following remembering to reference them fully by replacing the ? with a property.
Message =ActiveDocument.Hyperlinks.? OR
Message =ActiveDocument.Bookmarks.? OR
To see a listing of ALL Objects and their properties, open View Object Browser in the VB Editor.
Look more closely at Lessons 3 and 8 as mentioned above to see how referencing Objects and their Properties is an
essential part of using VBA.

USER DEFINED OBJECTS:


Now that you have gained a little understanding about objects, the next step is to understand the creation
and use of User Defined Data Types. This is how you create your own object types. They are created
using the Type statement. (The following example is from the VBA help.)
Type EmployeeRecord
' Create user-defined type.
ID As Integer
' Define elements of data type.
Name As String * 20
Address As String * 30
Phone As Long
HireDate As Date
End Type

Sub CreateRecord()
Dim MyRecord As EmployeeRecord
MyRecord.ID = 12003
End Sub

' Declare variable


' Assign a value to an element.

NOTE: Assignment to EmployeeRecord variable must occur in a procedure.


Objects can be a difficult concept to use in practice when you are just beginning to learn programming,
so they will not be looked until a much later stage.
They are mentioned here so that you will recognize them when you see them later!

[To Lesson Eight]

[To Index Page]

You might also like