h jafarnezhad

h jafarnezhad

  • NA
  • 3
  • 1.7k

how to Unable to cast COM object of type 'System.__ComObject

Sep 19 2015 12:51 AM

hi my error is

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Range'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020846-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).

and cod is

Directory.CreateDirectory(textBox2.Text + ":\\" + textBox3.Text);

int i = 0;

foreach (string txt in richTextBox1.Lines)

{

if ((richTextBox1.Lines[i].Length) != 0)

{

Directory.CreateDirectory(textBox2.Text + ":\\" + textBox3.Text + "\\" + s);

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbook book = app.Workbooks.Add(XlSheetType.xlWorksheet);

Microsoft.Office.Interop.Excel.Worksheet sheet = book.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

;

app.Visible = false;

Range rng1 = sheet.get_Range("A1", "A1");

rng1.Value2 = "?????";

rng1.Font.Bold = true;

Range rng2 = sheet.get_Range("B1", "B1");

rng2.Value2 = "??? ???";

rng2.Font.Bold = true;

Range rng3 = sheet.get_Range("C1", "C1");

rng3.Value2 = "?? ????";

rng3.Font.Bold = true;

                   

Class1.com3.Connection = Class1.con;

Class1.con.Close();

Class1.con.Open();

Class1.com3.CommandText = "SELECT * FROM [mobile_list] WHERE (city = '" + s + "') and (codeposti='" + richTextBox1.Lines[i] + "') ";

Class1.datareader1 = Class1.com3.ExecuteReader();

int j = 1;

while (Class1.datareader1.Read())

{

j++;

                       

Range r1 = sheet.get_Range("A" + j.ToString(), "A" + j.ToString());

r1.Value2 = Class1.datareader1.GetValue(0).ToString();

Range r2 = sheet.get_Range("B" + j.ToString(), "B" + j.ToString());

r2.Value2 = Class1.datareader1.GetValue(1).ToString();

Range r3 = sheet.get_Range("C" + j.ToString(), "C" + j.ToString());

r3.Value2 = Class1.datareader1.GetValue(2).ToString();

richTextBox2.Text = "???? ??? ???? ??? ?? ??? ????? ???? ?? ????";

}

richTextBox2.Text = "???? ???? ?? ????" + richTextBox1.Lines[i] + "????? ?? ?? ????" + textBox2.Text + ":\\" + textBox3.Text + "\\" + s + "\\" + richTextBox1.Lines[i] + ".xls";

book.SaveAs(textBox2.Text + ":\\" + textBox3.Text + "\\" + s + "\\" + richTextBox1.Lines[i] + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

sheet = Nothing;

book.Close();

app.Quit();

i++;

}

else

{

i++;

}

please hlp me to fix it 
 

Answers (1)