Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 358
Default Why this error and how to solve it ??

Hello, friends,

We are developing a C#.net (2005) application, which will use Excel to view
..xls files.

I created a WindowForm. In one of its methods, I instantiated Excel like the
follows:

public void LoadExcelWorkBook(string fullPathFileName)
{
if (currentFileName == fullPathFileName)
return;

currentFileName = fullPathFileName;

if (ea == null) ea = new
Microsoft.Office.Interop.Excel.ApplicationClass();

if (excelWnd == 0) excelWnd = FindWindow("XLMAIN", null);
if (excelWnd != 0)
{
SetParent(excelWnd, this.Handle.ToInt32());

try
{
ea.Visible = true;

SetWindowPos(excelWnd, this.Handle.ToInt32(), 0, 0,
this.Bounds.Width, this.Bounds.Height, SWP_NOZORDER | SWP_NOMOVE |
SWP_DRAWFRAME | SWP_NOSIZE);

}
catch
{
MessageBox.Show("Error!");
}

}
}

whe

public Microsoft.Office.Interop.Excel.Workbook wb = null;
public static Microsoft.Office.Interop.Excel.ApplicationClass ea =
null;
public static int excelWnd = 0;
public static string currentFileName = "";

were declared in the same class.

In my WindowForm's Dispose(), I had source code like the follows, expecting
to clean up everything:

protected override void Dispose(bool disposing)
{
if (wb != null)
{
object saveChanges = false;
object fileName = System.Reflection.Missing.Value;
object routeWorkbook = System.Reflection.Missing.Value;
wb.Close(saveChanges, fileName, routeWorkbook);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
}
if (ea.Workbooks != null)
{
ea.Workbooks.Close();
}
if (ea != null)
{
ea.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(ea);
ea = null;
}
DestroyWindow(excelWnd);
GC.Collect();

if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

It could launch Excel and made it as a child of my WindowForm window without
problem.

However, when I closed the app, everything was closed successfully but with
an error message popup window: Saying "Microsoft Office Excel has encountered
a problem and needs to close. We are sorry for the inconvenience." with
"Send Error Report" and "Don't Send" command buttons.

I then took out
SetParent(excelWnd, this.Handle.ToInt32());
This time, Excel was launched as an independent window, no longer a child of
the previous WindowForm. And, when I closed the app, everything was closed
without any error message.

I could not understand this and could not figure out why!

Anyone has any ideas? How to make it error free when making Excel a child
window in our app's WindowForm? Any reference papers?

Thanks a lot for your help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Why this error and how to solve it ??

Andrew,
I've done similar in VB6. Prior to close reset Excel's parent:

SetParent excelWnd, 0

NickHK

"Andrew" wrote in message
...
Hello, friends,

We are developing a C#.net (2005) application, which will use Excel to

view
.xls files.

I created a WindowForm. In one of its methods, I instantiated Excel like

the
follows:

public void LoadExcelWorkBook(string fullPathFileName)
{
if (currentFileName == fullPathFileName)
return;

currentFileName = fullPathFileName;

if (ea == null) ea = new
Microsoft.Office.Interop.Excel.ApplicationClass();

if (excelWnd == 0) excelWnd = FindWindow("XLMAIN", null);
if (excelWnd != 0)
{
SetParent(excelWnd, this.Handle.ToInt32());

try
{
ea.Visible = true;

SetWindowPos(excelWnd, this.Handle.ToInt32(), 0, 0,
this.Bounds.Width, this.Bounds.Height, SWP_NOZORDER | SWP_NOMOVE |
SWP_DRAWFRAME | SWP_NOSIZE);

}
catch
{
MessageBox.Show("Error!");
}

}
}

whe

public Microsoft.Office.Interop.Excel.Workbook wb = null;
public static Microsoft.Office.Interop.Excel.ApplicationClass ea =
null;
public static int excelWnd = 0;
public static string currentFileName = "";

were declared in the same class.

In my WindowForm's Dispose(), I had source code like the follows,

expecting
to clean up everything:

protected override void Dispose(bool disposing)
{
if (wb != null)
{
object saveChanges = false;
object fileName = System.Reflection.Missing.Value;
object routeWorkbook =

System.Reflection.Missing.Value;
wb.Close(saveChanges, fileName, routeWorkbook);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
}
if (ea.Workbooks != null)
{
ea.Workbooks.Close();
}
if (ea != null)
{
ea.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(ea);
ea = null;
}
DestroyWindow(excelWnd);
GC.Collect();

if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

It could launch Excel and made it as a child of my WindowForm window

without
problem.

However, when I closed the app, everything was closed successfully but

with
an error message popup window: Saying "Microsoft Office Excel has

encountered
a problem and needs to close. We are sorry for the inconvenience." with
"Send Error Report" and "Don't Send" command buttons.

I then took out
SetParent(excelWnd, this.Handle.ToInt32());
This time, Excel was launched as an independent window, no longer a child

of
the previous WindowForm. And, when I closed the app, everything was closed
without any error message.

I could not understand this and could not figure out why!

Anyone has any ideas? How to make it error free when making Excel a child
window in our app's WindowForm? Any reference papers?

Thanks a lot for your help.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 358
Default Why this error and how to solve it ??

thank you, Nick.

"NickHK" wrote:

Andrew,
I've done similar in VB6. Prior to close reset Excel's parent:

SetParent excelWnd, 0

NickHK

"Andrew" wrote in message
...
Hello, friends,

We are developing a C#.net (2005) application, which will use Excel to

view
.xls files.

I created a WindowForm. In one of its methods, I instantiated Excel like

the
follows:

public void LoadExcelWorkBook(string fullPathFileName)
{
if (currentFileName == fullPathFileName)
return;

currentFileName = fullPathFileName;

if (ea == null) ea = new
Microsoft.Office.Interop.Excel.ApplicationClass();

if (excelWnd == 0) excelWnd = FindWindow("XLMAIN", null);
if (excelWnd != 0)
{
SetParent(excelWnd, this.Handle.ToInt32());

try
{
ea.Visible = true;

SetWindowPos(excelWnd, this.Handle.ToInt32(), 0, 0,
this.Bounds.Width, this.Bounds.Height, SWP_NOZORDER | SWP_NOMOVE |
SWP_DRAWFRAME | SWP_NOSIZE);

}
catch
{
MessageBox.Show("Error!");
}

}
}

whe

public Microsoft.Office.Interop.Excel.Workbook wb = null;
public static Microsoft.Office.Interop.Excel.ApplicationClass ea =
null;
public static int excelWnd = 0;
public static string currentFileName = "";

were declared in the same class.

In my WindowForm's Dispose(), I had source code like the follows,

expecting
to clean up everything:

protected override void Dispose(bool disposing)
{
if (wb != null)
{
object saveChanges = false;
object fileName = System.Reflection.Missing.Value;
object routeWorkbook =

System.Reflection.Missing.Value;
wb.Close(saveChanges, fileName, routeWorkbook);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
}
if (ea.Workbooks != null)
{
ea.Workbooks.Close();
}
if (ea != null)
{
ea.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(ea);
ea = null;
}
DestroyWindow(excelWnd);
GC.Collect();

if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

It could launch Excel and made it as a child of my WindowForm window

without
problem.

However, when I closed the app, everything was closed successfully but

with
an error message popup window: Saying "Microsoft Office Excel has

encountered
a problem and needs to close. We are sorry for the inconvenience." with
"Send Error Report" and "Don't Send" command buttons.

I then took out
SetParent(excelWnd, this.Handle.ToInt32());
This time, Excel was launched as an independent window, no longer a child

of
the previous WindowForm. And, when I closed the app, everything was closed
without any error message.

I could not understand this and could not figure out why!

Anyone has any ideas? How to make it error free when making Excel a child
window in our app's WindowForm? Any reference papers?

Thanks a lot for your help.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to solve Error 2023 for Excel? Eric Excel Worksheet Functions 0 July 1st 07 02:24 PM
Runtime error 1004 - how to solve ladyhawke Excel Programming 0 June 27th 06 03:28 PM
Help needed to solve printing error madbloke[_23_] Excel Programming 5 August 8th 05 02:32 PM
How do solve window explorer error c\Doc~1\admin~1\local~1\temp\wer8c2f.dr0 Excel Programming 1 December 22nd 04 05:41 PM
How do I solve Error 2902 Farneholm Excel Programming 0 November 16th 04 11:49 AM


All times are GMT +1. The time now is 03:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"