Hi guys
I wrote this program that interacts with Excel and it was working pretty darn well. I was off work sick for a week and came back to a fresh windows install and a program that now hangs on this line:
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
with the following error: Invalid cast exception was unhandled.
I have absolutely no idea why this is happening and have been fiddling for ages with no success. Any help would be greatly appreciated. Oh, the program is also not working on other pc's.
Here is a code snippet:
public void MainLoop() //Function to process the files
{
int count1 = 0;
while (count1 < arraysize)
{
oWB = oXL.Workbooks.Open(openFileDialog1.FileNames[count1], Missing.Value, false, 4, Missing.Value, Missing.Value, //Opens selected Excel File
true, Missing.Value, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
oSheet = (Excel.Worksheet)oWB.ActiveSheet; //Makes oSheet the active sheet
filename = openFileDialog1.FileNames[count1]; //Acquires the filename and opens the 'open file' dialog box
ProgressUpdate();
LoadAllData(); //Calls the function to load all the data into an array
ProgressUpdate();
string files = System.IO.Path.GetFileName(openFileDialog1.FileNames[count1]);
if ((float)(double)(cells.GetValue(50, 1)) > 0) //This implements a check to see if the data is in negative values
{
MessageBox.Show("Data file incorrect. Positive values found in "+files); //Error box to tell you there are positive values in the file
count1++; //Implements the count in the case statement under DrawChart() otherwise the chart headings will be wrong
goto Filerror; //If file has positives, it jumps to the end of this proccess thus skipping this file
}
//lowestvalue();
Find2ndChange(); //Loop to find second change
LoadAllData(); //Puts all the data into an array
ProgressUpdate();
Find1stChange(); //Loop to find first change
DrawChart(); //Function to draw the chart
try /////THIS WORKS LIKE THE FREAKING BOMB! Uncomment when you are done for awesome saving skills!
{
length = filename.Length;
length = length - 4;
filename2 = filename.Remove(length, 4); //Takes the .csv off the filename
oWB.SaveAs(filename2 + ".xls", Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlExclusive, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //Resaves the file as an .Xls
//oWB.Close(false, false, false);
}
catch (Exception)
{
MessageBox.Show("Error: "+ files +" was not saved!" );
}
count1++;
}
killexcel();
//Close1();
count1++;
//MessageBox.Show("Processing is complete");
Filerror: ;
Thanks
Kevin