Working with VBScript Excel Objects
Posted by Superadmin on May 02 2019 03:37:55

Working with VBScript Excel Objects

 

 

Working with VBScript Excel Objects

Last Updated:April 23, 2019

Introduction to VBScript Excel Objects: Tutorial #11

In my previous tutorial, I explained about ‘Events’ in the VBScript. In this tutorial, I will be discussing Excel Objects that are used in the VBScript. Please note that this is the 11th tutorial in our ‘Learn VBScripting‘ series.

VBScript supports different type of objects and Excel Objects are among of those. Excel Objects are mainly referred to as objects the that provide support to the Coders to work and deal with the Excel Sheets.

 

This tutorial gives you a complete overview of the process of creation, addition, deletion etc., of an Excel file using Excel Objects in the VBScript with simple examples.

Working with Excel Objects in the VBScript

 

What You Will Learn: [show]

Overview

Microsoft Excel needs to be installed on your computer to work with the Excel files. By creating an Excel Object, VBScript provides you the assistance to perform important operations like Create, Open and Edit Excel files.

It is very important to understand this topic as this forms the basis of the working with the Excel sheets and hence I decided to pick this as one of the topics in the series of the VBScript tutorial.

I will try to make you understand all the different codes that are required to be written to work with the excel files in an easy manner so that you can easily write a piece of code on your own.

Now, let’s move on to the practical working of Excel files by understanding the code written for different scenarios focusing mainly on the important ones.

Creating an Excel File using Excel Object

In this section, we will see the various steps involved in creating an excel file using Excel Object mechanism in the VBScript.

Following is the Code for Creating an Excel File:

Set obj = createobject(“Excel.Application”)  ‘Creating an Excel Object
obj.visible=True                                    ‘Making an Excel Object visible
Set obj1 = obj.Workbooks.Add()       ‘Adding a Workbook to Excel Sheet
obj1.Cells(1,1).Value=”Hello!!”         ‘Setting a value in first row first column
obj1.SaveAs “C:\newexcelfile.xls”   ‘Saving a Workbook
obj1.Close                                             ‘Closing a Workbook
obj.Quit                                                  ‘Exit from Excel Application
Set obj1=Nothing                                 ‘Releasing Workbook object
Set obj=Nothing                                   ‘Releasing Excel object

Let’s understand how it works:

Note: It is a good practice to release the objects using ‘Set object name = Nothing’ after the completion of the task at the end.

Reading/Opening an Excel File using Excel Object

In this section, we will see the different steps of reading the data from an excel file using Excel Object mechanism in the VBScript. I will use the same excel file that is created above.

Following is the Code for reading the data from an excel file:

Set obj = createobject(“Excel.Application”)   ‘Creating an Excel Object
obj.visible=True                                    ‘Making an Excel Object visible
Set obj1 = obj.Workbooks.open(“C:\newexcelfile.xls”)    ‘Opening an Excel file
Set obj2=obj1.Worksheets(“Sheet1”)    ‘Referring Sheet1 of excel file
Msgbox obj2.Cells(2,2).Value  ‘Value from the specified cell will be read and shown
obj1.Close                                             ‘Closing a Workbook
obj.Quit                                                  ‘Exit from Excel Application
Set obj1=Nothing                                 ‘Releasing Workbook object
Set obj2 = Nothing                               ‘Releasing Worksheet object
Set obj=Nothing                                   ‘Releasing Excel object

Let’s understand how it works:

Deletion from an Excel File

In this section, we will take a look at the steps involved in the deleting a data from an excel file using Excel Object mechanism in VBScript. I will use the same excel file that is created above.

Following is the Code for deleting the data from an Excel file:

Set obj = createobject(“Excel.Application”)   ‘Creating an Excel Object
obj.visible=True                                    ‘Making an Excel Object visible
Set obj1 = obj.Workbooks.open(“C:\newexcelfile.xls”)    ‘Opening an Excel file
Set obj2=obj1.Worksheets(“Sheet1”)    ‘Referring Sheet1 of excel file
obj2.Rows(“4:4”).Delete           ‘Deleting 4th row from Sheet1
obj1.Save()                                   ‘Saving the file with the changes
obj1.Close                                             ‘Closing a Workbook
obj.Quit                                                  ‘Exit from Excel Application
Set obj1=Nothing                                 ‘Releasing Workbook object
Set obj2 = Nothing                               ‘Releasing Worksheet object

Let’s understand how it works:

Addition & Deletion of a Sheet from an Excel File

In this section, let's see the different steps of adding and deleting an excel sheet from an excel file using Excel Object mechanism in VBScript. Here also I will use the same excel file that is created above.

Following is the Code for this scenario:

Set obj = createobject(“Excel.Application”)   ‘Creating an Excel Object
obj.visible=True                                    ‘Making an Excel Object visible
Set obj1 = obj.Workbooks.open(“C:\newexcelfile.xls”)    ‘Opening an Excel file
Set obj2=obj1.sheets.Add  ‘Adding a new sheet in the excel file
obj2.name=”Sheet1”     ‘Assigning a name to the sheet created above
Set obj3= obj1.Sheets(“Sheet1”)  ‘Accessing Sheet1
obj3.Delete       ‘Deleting a sheet from an excel file
obj1.Close                                             ‘Closing a Workbook
obj.Quit                                                  ‘Exit from Excel Application
Set obj1=Nothing                                 ‘Releasing Workbook object
Set obj2 = Nothing                               ‘Releasing Worksheet object
Set obj3 = Nothing                              ‘Releasing Worksheet object
Set obj=Nothing                                   ‘Releasing Excel object

Let’s understand how it works:

Copying & Pasting of Data from one Excel file to Another Excel File

In this section, we will see the different steps involved in copying/pasting a data from one excel file to another excel file using Excel Object mechanism in the VBScript. I have used the same excel file that was used in the above scenarios.

Following is the Code for this scenario:

Set obj = createobject(“Excel.Application”)   ‘Creating an Excel Object
obj.visible=True                                    ‘Making an Excel Object visible
Set obj1 = obj.Workbooks.open(“C:\newexcelfile.xls”)    ‘Opening an Excel file1
Set obj2 = obj.Workbooks.open(“C:\newexcelfile1.xls”)    ‘Opening an Excel file2
obj1.Worksheets(“Sheet1”).usedrange.copy  ‘Copying from an Excel File1
obj2.Worksheets(“Sheet1”).usedrange.pastespecial  ‘Pasting in Excel File2
obj1.Save                                              ‘ Saving Workbook1
obj2.Save                                              ‘Saving Workbook2
obj1.Close                                             ‘Closing a Workbook
obj.Quit                                                 ‘Exit from Excel Application
Set obj1=Nothing                                ‘Releasing Workbook1 object
Set obj2 = Nothing                              ‘Releasing Workbook2 object
Set obj=Nothing                                  ‘Releasing Excel object

Let’s understand how it works:

These are some of the important scenarios which are required in the proper understanding of the concept. And they form the foundation to work and deal with the codes for handling different types of scenarios while dealing with the Excel Objects in the script.

Conclusion

Excel plays a prime major role everywhere. I'm sure that this tutorial must have given you a great insight regarding the importance and effectiveness of using VBS Excel Objects.