Quantcast
Channel: NetOffice - MS Office in .NET
Viewing all articles
Browse latest Browse all 1741

Commented Unassigned: Workbook.ExportAsFixedFormat followed by Workbook.Close crashes the Excel app but no exception is thrown [22310]

$
0
0
When calling Workbook.ExportAsFixedFormat to save an Excel workbook as PDF quickly followed by Workbook.Close, the Excel app crashes but no exception is thrown by NetOffice.

Here's the code to reproduce the issue:
```
var s = $@"C:\users\{Environment.UserName}\desktop\test.xlsx";
var d = $@"C:\users\{Environment.UserName}\desktop\test.pdf";

var app = new Application() { Visible = true };

try
{
var wb = app.Workbooks.Open(s);
wb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, d, XlFixedFormatQuality.xlQualityMinimum, false, null, null, null, false);

// 2. Adding the sleep for at least 16ms on my machine makes it succeed.
//Thread.Sleep(20);

// 1. It crashes on the following line.
wb.Close(false);

// 3. Note that calling Close on the COM object works and properly closes the workbook.
//((dynamic)wb.UnderlyingObject).Close(false);

Console.WriteLine("Done");
}
catch (Exception ex)
{
ex.Dump();
}
finally
{
app.Quit();
app.Dispose();
}
```

Some more details.
1. On my machine, it crashes the Excel app when the code executes in 1.02s or less.
2. On my machine (or on slower machines), it doesn't crash the Excel app when the code executes in 1.1s or more.
3. Calling the Close method on the COM object itself doesn't crash the Excel app.

I'm using NetOffice 1.7.3.0 with Office 2016 16.0.6741.2056 on Windows 10 Enterprise Anniversary Update, 64-bit. I used LINQPad to reproduce the scenario above.

I have uploaded the LINQPad query (if needed). The Excel file (test.xlsx) is a very simple workbook with "Hello World!" in the first cell.

UPDATE:
It looks like in some cases, it still crashes the Excel app even when calling the Close method on the COM object itself. This could be due to the dynamics framework being a bit slower to execute...
Comments: this is some kind of bad magic -.- 1) Thread.Sleep(20); fix the problem as well ? 2) any details in the event log ? 3) is your solution an addin or something else? 4) your code is running in some kind of worker thread? 5.) Can you try the following call instead? object proxy = wb.UnderlyingObject; proxy.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, proxy, new object[] { false }); it is a plain late binding call without any netoffice logic to pin point the problem. *Sebastian

Viewing all articles
Browse latest Browse all 1741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>