Vba adding object library reference in Excel

A "library" in programming is nothing but a code which performs certain actions through some of its methods.

for example, an oulook object in excel can send mail through "Send" property.

But how does this complex task is accomplished in the program in a single line of code?

This is actually a large program packed into a small library which can be refered in the program to use its functionalities.

The excel has various libraries such as Outlook, ADO for database connectivity, MySQL for connecting with MySql and so on.

Without Libraries, the coding will be a very complicated task and each and every functionality has to be hard coded.

Adding Object Library Reference

1.Open VBA Editor

2.Goto Tools--->References

3.A dialog box appears with all available references.

4.Choose the required, and Click Ok.

The following code demonstrates the usage of references with the help of Outlook object.

  1. Dim OutApp as Object
  2. Set OutApp = CreateObject("Outlook.Application")
  3. Set OutMail = OutApp.CreateItem(0)
  4. With Destwb
  5. .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
  6. On Error Resume Next
  7. With OutMail
  8. .to = "abc@abc.com"
  9. .CC = ""
  10. .BCC = ""
  11. .Subject = "This is the Subject line"
  12. .Body = "Hi there"
  13. .Attachments.Add Destwb.FullName
  14. .Send 'or use .Display
  15. End With
  16. On Error GoTo 0
  17. .Close savechanges:=False
  18. End With

The above snippet is just for illustration.

Note that, to Use Outlook object, a reference has to be added beforehand.

Also, note the inbuilt keywords such as To, Send,Cc and so on.

These are all packed within the Outlook object and comprises of a large program, which are made accessible through a single line.

A screeshot of how to add reference is shown below:

excel vba adding object library reference

excel vba adding object library reference

Click Ok.

 

You can find similar Excel Questions and Answer hereunder

1) what are the main range object and what can the range objects do in VBA

2) What is a methods. Methods are action that can be performed by an object

3) What is the use relative references in the Excel Developer Tab

4) What are the main sheet objects and what can the objects do in the sheet in VBA

5) How do you close, save, open files and do other operations in the workbook in VBA

6) I want to pull data from a table using a reference cell in table. Is there a function to support that?

7) What are the main cell objects in VBA

8) How can I identify cells with Circular Reference in a WorkSheet?

9) How to avoid screen update in Excel VBA. Various application objects explained

10) How do I enter a formula in a cell using VBA (using Absolute Reference)?

 

Here the previous and next chapter