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.
- Dim OutApp as Object
- Set OutApp = CreateObject("Outlook.Application")
- Set OutMail = OutApp.CreateItem(0)
- With Destwb
- .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
- On Error Resume Next
- With OutMail
- .to = "abc@abc.com"
- .CC = ""
- .BCC = ""
- .Subject = "This is the Subject line"
- .Body = "Hi there"
- .Attachments.Add Destwb.FullName
- .Send 'or use .Display
- End With
- On Error GoTo 0
- .Close savechanges:=False
- 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:
Click Ok.