cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if a sheet exits in Excel using Test Complete

SOLVED
Contributor

How to check if a sheet exits in Excel using Test Complete

My requirement is to check if sheet with name Test is present in a particular excel file.
If its not present insert a new sheet with name Test into excel.

So how check sheet name and insert new sheet in Excel
1 ACCEPTED SOLUTION

Accepted Solutions
Community Manager

Re: How to check if a sheet exits in Excel using Test Complete

Hi @rushikesh,

 

Helen has posted a sample script demonstrating how to check if a sheet exists in an Excel file. Here it is:

var oExcel = Sys.OleObject("Excel.Application");
var oWb = oExcel.Workbooks.Open("C:\\MyFile.xls");

var oSheet = null;
try {
  oSheet = oWb.Sheets("Sheet1");
}
catch (e) {}

if (oSheet != null) {
  Log.Message("The specified sheet exists.");
}
else {
  Log.Error("The specified sheet does not exists.");
}

oExcel.Quit();

 

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
4 REPLIES 4
Community Hero

Re: How to check if a sheet exits in Excel using Test Complete

Hi,

 

https://www.google.com/search?q=excel+check+if+sheet+is+present

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Community Manager

Re: How to check if a sheet exits in Excel using Test Complete

Hi @rushikesh,

 

Helen has posted a sample script demonstrating how to check if a sheet exists in an Excel file. Here it is:

var oExcel = Sys.OleObject("Excel.Application");
var oWb = oExcel.Workbooks.Open("C:\\MyFile.xls");

var oSheet = null;
try {
  oSheet = oWb.Sheets("Sheet1");
}
catch (e) {}

if (oSheet != null) {
  Log.Message("The specified sheet exists.");
}
else {
  Log.Error("The specified sheet does not exists.");
}

oExcel.Quit();

 

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Highlighted
Contributor

Re: How to check if a sheet exits in Excel using Test Complete

@TanyaGorbunova can you provide a simlilar sample code demonstrating how to insert a new sheet into excel file?

Community Manager

Re: How to check if a sheet exits in Excel using Test Complete

 

Excel's COM object provides the Sheets.Add method. So, you can play with the code to add this method call. Please take a look at this article for more information.

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Here?
Join us and watch the welcome video: