[Collapse]TestComplete How To
 [Collapse]Microsoft Office
   Compare Excel files/viewarticle/9046/
   Compare Word documents/viewarticle/17635/
   Get ContentControl items in MS Word/viewarticle/17601/
   Quickly Compare Excel Files/viewarticle/42199/
   Receive e-mail messages via MS Outlook/viewarticle/9022/
   Run macros in MS Excel/viewarticle/9045/
   Search for Text in MS Excel/viewarticle/24734/
   Write data to MS Excel/viewarticle/20878/
   Write data to MS Word/viewarticle/42201/
 [Expand]TestComplete Utility Scripts
 [Expand]Third-party Controls
 [Expand]Web Utility Scripts
 [Expand]Windows Utility Scripts
Updated: 4/24/2012 Applies To: TestComplete Rating: Not badNot badNot badNot badNot bad 36 vote(s) Click to rate: PoorNot badAverageGoodExcellent
Compare Excel files

Supported version: Microsoft Office Excel 2003

The script below demonstrates how to compare two Excel files. The comparison algorithm is implemented in the ExcelSheetCompare script extension that you can download below. To learn how to install the script extension, read the "Installing and Uninstalling Script Extensions" article.

VBScriptCopy Code

Sub Main
  Dim oExcel, aSheets, vSheet, fileName, aInfo1, aInfo2 
  Set oExcel = Sys.OleObject("Excel.Application")
  Set aSheets = Sys.OleObject("Scripting.Dictionary")
  'You can specify the sheet to be compared 
  'vSheet = "Sheet1"
  'Call aSheets.Add(vSheet, vSheet) 
  fileName = "<Path_To_The_File1>"
  Set aInfo1 = ExcelObj.CreateInfo(oExcel, fileName, aSheets)
  fileName = "<Path_To_The_File2>"
  Set aInfo2 = ExcelObj.CreateInfo(oExcel, fileName, aSheets)
  oResult = ExcelObj.Compare(aInfo1, aInfo2)
  If Len(oResult) > 0 Then
    Log.Warning oResult
    Log.Message "The files are the same."  
  End If
End Sub

JScriptCopy Code

function Main()
  var oExcel = Sys.OleObject("Excel.Application");
  var aSheets = Sys.OleObject("Scripting.Dictionary");
  //You can specify the sheet to be compared
  //var vSheet = "Sheet1";
  //aSheets.Add(vSheet, vSheet); 
  var fileName = "<Path_To_The_File1>";
  var aInfo1 = ExcelObj.CreateInfo(oExcel, fileName, aSheets);
  var fileName = "<Path_To_The_File2>";
  var aInfo2 = ExcelObj.CreateInfo(oExcel, fileName, aSheets);
  var oResult = ExcelObj.Compare(aInfo1, aInfo2);
  if (oResult.length > 0)
    Log.Message("The files are the same.");  

© 2020 SmartBear Software. All rights reserved.
Email Send feedback on this document