nancypnp
10 years agoOccasional Contributor
DDT excel driver and Sys.OleObject("Excel.Application") are throwing exception
Hi All,
I am trying to compare two excel files with same sheet names and trying to save the result into a new excel file
compariosn is done using DDT as i dont want to open the file, just want to read
and result update to new excel i am using Sys.OleObject("Excel.Application") as i need to open excel and set the result
please do help me with this
Function CompareExcelCellValues(nRowIndex,SheetName,RefFileDriver, FileToCompareDriver)
Dim NumberOfColumns, ColumnIndex, CurrentRow, RtnValue
Dim RefCellText,FileToCompareCellText, ComparisionFlag, TitleArray()
ComparisionFlag = True
CurrentRow = 0
RtnValue = False
Redim TitleArray(8)
NumberOfColumns = RefFileDriver.ColumnCount
If RefFileDriver.EOF() And FileToCompareDriver.EOF() then
RtnValue = True
Log.Message("EOF reached for both the files in the begining itself."+vbCrLf+_
"No Data is present.")
CompareExcelCellValues = RtnValue
Exit Function
Else
While Not RefFileDriver.EOF() And Not FileToCompareDriver.EOF()
For ColumnIndex = 0 To NumberOfColumns - 1
RefCellText = VarToStr(RefFileDriver.value(ColumnIndex))
RefCellTextName = VarToStr(RefFileDriver.ColumnName(ColumnIndex))
FileToCompareCellText = VarToStr(FileToCompareDriver.value(ColumnIndex))
FileToCompareCellTextName = VarToStr(FileToCompareDriver.ColumnName(ColumnIndex))
If CurrentRow = 0 And RefCellText <> "" Then
TitleArray(ColumnIndex) = RefCellText 'TitleArray(0) = ""
End If
If Utilities.CompareStr(RefCellText , FileToCompareCellText) <> 0 Then
If VarToStr(RefFileDriver.value(0)) <> "" Then
PanelNode = VarToStr(RefFileDriver.value(0))
' DDT.CloseDriver(RefFileDriver.Name)
' DDT.CloseDriver(FileToCompareDriver.Name)
Call SetValueToExcelUsingColumnDA(DistributedDownLoadSheet,nRowIndex,"SW_Version_Difference",_
"For Node: "&PanelNode& ""&vbLf&"" & TitleArray(ColumnIndex)& " Version: Before:" &RefCellText& ", After:" &FileToCompareCellText&""&vbLf)
End if
ComparisionFlag = False
End If
Next
CurrentRow = CurrentRow + 1 'go to next row
RefFileDriver.Next()
FileToCompareDriver.Next()
If RefFileDriver.EOF() XOR FileToCompareDriver.EOF() then
RtnValue = False
CompareExcelCellValues = RtnValue
Exit Function
End If
Wend
End If
DDT.CloseDriver(RefFileDriver.Name)
DDT.CloseDriver(FileToCompareDriver.Name)
If ComparisionFlag Then
Log.Message("Data in both the Excel Sheets are Equal")
Call SetValueToExcelUsingColumnDA(DistributedDownLoadSheet,nRowIndex,"SW_Version_Difference","Software Version:Before Download and After Download is same")
RtnValue = True
End If
CompareExcelCellValues = RtnValue
End Function
Public function SetValueToExcelUsingColumnDA(sheetname,nRow,sCol,szdata)
Set Excel = Sys.OleObject("Excel.Application")
if Excel.Visible then
usedColumnsCount = Excel.Sheets(sheetname).UsedRange.Columns.Count
For i = 1 to usedColumnsCount
if sCol = Excel.Sheets(sheetname).cells(1,i).text then
nCol = i
exit for
end if
Next
Excel.Sheets(sheetname).Cells(nRow,nCol).Value= Excel.Sheets(sheetname).Cells(nRow,nCol).Value +szdata
End If
End function
I am trying to compare two excel files with same sheet names and trying to save the result into a new excel file
compariosn is done using DDT as i dont want to open the file, just want to read
and result update to new excel i am using Sys.OleObject("Excel.Application") as i need to open excel and set the result
please do help me with this
Function CompareExcelCellValues(nRowIndex,SheetName,RefFileDriver, FileToCompareDriver)
Dim NumberOfColumns, ColumnIndex, CurrentRow, RtnValue
Dim RefCellText,FileToCompareCellText, ComparisionFlag, TitleArray()
ComparisionFlag = True
CurrentRow = 0
RtnValue = False
Redim TitleArray(8)
NumberOfColumns = RefFileDriver.ColumnCount
If RefFileDriver.EOF() And FileToCompareDriver.EOF() then
RtnValue = True
Log.Message("EOF reached for both the files in the begining itself."+vbCrLf+_
"No Data is present.")
CompareExcelCellValues = RtnValue
Exit Function
Else
While Not RefFileDriver.EOF() And Not FileToCompareDriver.EOF()
For ColumnIndex = 0 To NumberOfColumns - 1
RefCellText = VarToStr(RefFileDriver.value(ColumnIndex))
RefCellTextName = VarToStr(RefFileDriver.ColumnName(ColumnIndex))
FileToCompareCellText = VarToStr(FileToCompareDriver.value(ColumnIndex))
FileToCompareCellTextName = VarToStr(FileToCompareDriver.ColumnName(ColumnIndex))
If CurrentRow = 0 And RefCellText <> "" Then
TitleArray(ColumnIndex) = RefCellText 'TitleArray(0) = ""
End If
If Utilities.CompareStr(RefCellText , FileToCompareCellText) <> 0 Then
If VarToStr(RefFileDriver.value(0)) <> "" Then
PanelNode = VarToStr(RefFileDriver.value(0))
' DDT.CloseDriver(RefFileDriver.Name)
' DDT.CloseDriver(FileToCompareDriver.Name)
Call SetValueToExcelUsingColumnDA(DistributedDownLoadSheet,nRowIndex,"SW_Version_Difference",_
"For Node: "&PanelNode& ""&vbLf&"" & TitleArray(ColumnIndex)& " Version: Before:" &RefCellText& ", After:" &FileToCompareCellText&""&vbLf)
End if
ComparisionFlag = False
End If
Next
CurrentRow = CurrentRow + 1 'go to next row
RefFileDriver.Next()
FileToCompareDriver.Next()
If RefFileDriver.EOF() XOR FileToCompareDriver.EOF() then
RtnValue = False
CompareExcelCellValues = RtnValue
Exit Function
End If
Wend
End If
DDT.CloseDriver(RefFileDriver.Name)
DDT.CloseDriver(FileToCompareDriver.Name)
If ComparisionFlag Then
Log.Message("Data in both the Excel Sheets are Equal")
Call SetValueToExcelUsingColumnDA(DistributedDownLoadSheet,nRowIndex,"SW_Version_Difference","Software Version:Before Download and After Download is same")
RtnValue = True
End If
CompareExcelCellValues = RtnValue
End Function
Public function SetValueToExcelUsingColumnDA(sheetname,nRow,sCol,szdata)
Set Excel = Sys.OleObject("Excel.Application")
if Excel.Visible then
usedColumnsCount = Excel.Sheets(sheetname).UsedRange.Columns.Count
For i = 1 to usedColumnsCount
if sCol = Excel.Sheets(sheetname).cells(1,i).text then
nCol = i
exit for
end if
Next
Excel.Sheets(sheetname).Cells(nRow,nCol).Value= Excel.Sheets(sheetname).Cells(nRow,nCol).Value +szdata
End If
End function