When to set excel Objects to nothing

12 views
Skip to first unread message

sarah...@gmail.com

unread,
Feb 2, 2018, 5:03:42 PM2/2/18
to QTP - HP Quick Test Professional - Automated Software Testing
Hi guys,

Can we use the same excel object in different functions but same Action?
I guess my concern is I'm calling the subs more than 1 time and not setting the
object to nothing until the end.  Is that ok?  What's the best approach?

Thanks

Example:

Dim objExcel, objWorkbook, objWorksheet, i

Set objExcel = CreateObject("Excel.Application")

for i = 1 to 20
      AddNumbers
      FormatNumber
Next



Sub AddNumbers
 objExcel.Visible = True
 Set objWorkbook = objExcel.Workbooks.open("abc.xlsx")
 set objWorksheet= objWorkbook.Worksheets("MySheet")
 objWorksheet.Activate
 Wait(2)

 objExcel.Cells(RowCount,1) = 89.98  
 objExcel.Cells(RowCount,1) = 11.3 
 objExcel.Cells(RowCount,1) = 19.00
 objExcel.ActiveWorkbook.Save
 objExcel.application.quit 
End Sub



Sub FormatNumber
 objExcel.Visible = True
 Set objWorkbook = objExcel.Workbooks.open("abc.xlsx")
 set objWorksheet= objWorkbook.Worksheets("MySheet")
 objWorksheet.Activate
 Wait(2)

 objExcel.Columns.NumberFormat = 0

 objExcel.ActiveWorkbook.Save
 objExcel.application.quit 
End Sub


Set objExcel = nothing
set objWorkbook = nothing
Set objWorksheet = nothing

Reply all
Reply to author
Forward
0 new messages