VbaFin.com

Visual Basic for Financial Professionals
   Home      VBA Basics
Visual Basic is an event-driven object oriented programming language that can be used with most MS Office applications (Excel, Word, Access, Outlook, Powerpoint). This means that one can use it to create and manipulate Objects. Objects could have their own Properties, Methods and Events and can be organized in Collections. Objects belong to Classes and Classes belong to Libraries.

To use these classes and their objects as part of your Visual Basic project, you may want to establish a reference to their Libraries. This can be done following the steps below:

  1. From your Microsoft Application press Alt-F11 to activates the Visual Basic Editor
  2. From the Tools menu, choose References to display the References dialog box.
  3. The References dialog box shows all object libraries registered with the operating system. Scroll through the list for the application whose object library you want to reference. If the application isn't listed, you can use the Browse button to search for object libraries (*.olb and *.tlb) or executable files (*.exe and *.dll on Windows). References whose check boxes are checked are used by your project; those that aren't checked are not used, but can be selected and thus added. 
You can use the Object Browser (F2 to activate) to view all Libraries with their Classes, Objects, Methods, Properties, Events.
 
So for example let’s say we want to use the FileSystemObject class of the Microsoft Scripting Library to create a text file and write a line in it. We can reference the Library (path C:WindowsSystem32scrun.dll) and then write the following code:
 
Dim fs As FileSystemObject 'Declare fs as FileSystemObject object
Set fs = New FileSystemObject 'Create a FileSystemObject object
With fs
    With .CreateTextFile("c:test.txt", True) 'Use the CreateTextFile method to create "c:test.txt"
        .WriteLine "this is a test" 'Use the WriteLine method to write a line
        .Close 'Use the Close method to close the file
    End With
End With
Set fs = Nothing 'Clean up by destroying the object

If you gain a basic understanding of the language syntax, keywords and some of its important functions and apply the concept described above you’d be able to write powerful code to solve a wide range of problems. User Interface Help, Conceptual Topics, How-to Topics and Language Help are available with Microsoft Visual Basic Documentation (F1).