I have faced a big and inconvenient problem with Excel spreadsheets that have internal data connections (queries that selects ranges of the own workbook) in them.
I wiil start by showing the problem e exposing how to reproduce it.
Problem: if I have some internal connetion and another instance (pay attention, instance, not other workbook) was already open, when refreshing that connection, the same workbook (which have the internal connection) is instantly opened in readonly mode in another window...
Why this occurs? The problem is that I'm developing a .NET automation application for Excel and this behavior breaks some process flow (because the readonly file stays open and sometimes it causes a non refresh in the original workbook)
To solve this problem I began to use the same instance if it is already open, but this is not the best solution because I need to handle the concurrency when running several VBA macros in the same instance via .NET Interop.
Here a workbook for those who wish to test this issue that I presented (just change the string connection inserting the new file path in your computer):
Excel File for Test
I recorded a video to show you the issue with more details and how to reproduce it. Please, take a look:
Video
I hope someone knows how to solve this problem because I've been searching for this several days...
Thanks
Obs.: The Office version is 2013
Related
I have a 3rd party app that is exporting data to Excel using the Excel COM Interop. There is a bug in their program causing it to fail before it makes the Excel instance visible. However from where the exception is happening, according to the displayed stack trace, the information I need has already been written out to the Excel Worksheet.
Is there any way to use the Microsoft.Office.Interop.Excel namespace to connect to a existing excel instance instead of it generating it's own? Or is there any other way I could make that orphaned Excel instance visible so I can save what it completed?
As a note, the EXCEL.exe is still visible in task manager after the program closes so the instance is still live and running after the app has disconnected.
More Details: What the program is doing is exporting a report from it's own (proprietary) database, however for some reason some record in the database became malformed and causes a Integer Overflow error to occur while the report is generating. Looking at the stack trace it appears this integer overflow is happing while it is generating the summery at the end of the report.
For my uses I do not need the summary just the line items from the report, so I hoped to see what work it had done so far but the EXCEL.exe instance it was communicating with is not visible.
If I run the report for a different date range the report generates fine, it is just something about one record on one day that causes this specific set of inputs that causes it to fail.
The support contract with the provider of the app as expired and management is not interested in renewing it as we are in the process of switching to a new vendor (that is why the report is being generated, to be used as the data-source for the data conversion). So I have been tasked with "fixing it" so the data can be moved over.
Final Update: The reason I asked the question was solved. I was able to go through each record via the program itself and I found the record that had one of it's fields set to 16274176.00 (normal values are in the 100's (and don't ask me why what appears to be a float causes a Integer Overflow error)), once I changed that to 0.00 the report printed fine. However I still would like to know if there is a answer to my original question as I think it would be a useful tool in my toolbox.
In regards to your original question, I think it actually might be a possible duplicate of what have been asked here: Get instance of Excel application with C# by Handle
A couple of the answers given points to this blog post: Launching Office Apps Programmatically which gives you a palette of options of how to connect to an already running Office application from managed code.
The blog post as well as the answers given also contains sample code to illustrate how this can be done.
The problem I am having involves saving a workbook that is created in C# code on a clients machine. The published version of the program works perfectly on my machine, but not on the clients.
Overview of the Process:
When the program has completed it's iterations it will create worksheets once complete it will save the workbook and display a message box saying that the export was successful and display the workbook that was created.
The Problem:
When ran on the clients machine it will go through the process of creating the worksheets, but for some reason it will not save the file. It dose keep the instance of excel in memory, so when you open task manager you will see the process. Also after you run the program and try to turn off the computer it will prompt you to save or not save the workbook that was created since the instance is still in memory. This is how i have worked around and got to save the workbook to verify that the program did create the worksheets.
My thoughts:
I am thinking that there maybe some settings for windows or for excel that is preventing the excel file to be saved and automatically opened. I am also using background worker to accomplish the task of monitoring the progress of the workbook creation process, so this could also be a problem.
If anyone has encountered this problem or know of a solution please let me know. If more information is needed then i can provide that.
I did not include code, because i did not find it useful in knowing why the problem is happening since everything works on my development machine, but not the clients.
EDIT
I work with Jared and was finally able to debug the program on another machine with VS installed. It turns out that the problem wasn't in the save function at all but with the number of default sheets created on a client machine.
When creating the workbook, the program is supposed to go through the worksheets and delete all of them but the first one. There was a logic flaw (coded by me :/) that would leave an empty sheet in the workbook which caused the problem at this statement:
int lastRow = _excelWorksheet.Cells.Find("*", Type.Missing, Type.Missing, Type.Missing,
XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious, false, Type.Missing, Type.Missing).Row;
Apparently it doesn't like searching for stuff on an empty sheet. Our development machines are currently set to default to one sheet only, so we didn't run into the problem.
What I found odd was that while debugging, the program would throw a System.NullReferenceException at that line. However, the program wouldn't crash on the client machines. Instead, it would just leave the Excel instance in memory as described and would just sit there. As Jared said, the excel stuff runs on a background thread so maybe that has something to do with it.
I have written a C# program which import a product list from a .xlsx file and let the user create an order based on that product list.
When the user is finished, the program builds one or more system specifications based on the order.These specifications is written to a .docx file. I have Office 2007 installed on the computer and are using the Microsoft.Office.Interop.Excel and the Microsoft.Office.Interop.Word namespaces.
The problem:
After I have runned the program, Windows Explorer crashes very often and has to restart. This happens when navigating in folders or when right- clicking on folders etc.
This also happens after the program have been closed and the only solution to make it stop is to restart the computer. It seems like it only happens when I have created the output files (.docx). If i start the program and use it like I normally do, but without creating the word files, the problem don't seem to occur.
After the program have created the output files, Word gets "Visible" to the user for manual editing. The user closes the word application when finished editing the documents.
What can make the Windows Explorer crash when running word automation?
I really need help on this one. Any suggestions are welcome.
After execution, do you have ghost excel.exe and word.exe processes remaining?
These ghost are likely to make the system unstable.
You're likely not releasing properly the COM objects you instantiated via automation.
Use Marshal.ReleaseComObject(yourobj); on each and every COM objects you instantiate. It's a real pain, I know.
Note: be sure that you don't instantiate COM objects without knowing it:
mySheet = myExcelObject.workbooks[0].Sheet[0] won't just instantiate a sheet object, but also a workbook object.
Rule of thumb: never ever use a secondary property on a COM object ( foo.bar.baz ) and release everything.
Final note: don't use office automation at all on the server, it's bad, per Microsoft own words, there are fully managed libraries for that.
I am currently hosting an IE Browser control in a .NET (2.0) Form and using it to load Office files such as Excel and Word thusly:
_ieCtrl.Navigate("C:\\test.xls", False);
The hosting and loading works well except whenever I navigate to a file I am presented with a dialog that asks whether I want to save or open the file. (This is standard IE file-download behavior.) I always want to open it of course and I do not want the dialog to show.
Another issue is that when I close the window that hosts the IE control and the Office doc the document does not close and remains open on disk. This means that subsequent attempts to open the same file via my app or the native office app will fail because of the sharing violation.
Is there a programmatic way of avoiding this dialog and cleaning up resources afterward? I am asking for a programmatic answer because web research has only yielded solutions that entail modifying OS-level settings.
Bounty NOTE:
I am open to any solution to this issue that will allow me to:
Host an Excel spreadsheet inside my application
Work rather transparently (avoid usability issues like the one described above)
Avoid having to make any OS-specific changes that may affect other applications (especially icluding IE)
Is zero additional cost (no licensed 3rd party libs please) Code Project and other open source resources are OK
Not mess around with the DSO Framer ActiveX control, unless a stable version is developed/discovered
Is your intention for the user to be able to work with the Excel file in an Excel-ish way (i.e. columns, rows, formulas, etc.), possibly saving it back? If this is the case, I can't see how you can solve this problem well without relying on COM Interop with the Excel object model or by integrating third-party libraries to work with the Excel sheet. I know you said no paid solutions, but there are some feature-rich 3rd-party controls out there just for working with Excel files within applications.
I noticed in your comment to SLaks that the final product is a "dashboard of sorts". If your intention is to design a a custom dashboard application, have you considered parsing the Excel file(s) to extract the data and then presenting it in a logical manner within your application. This removes the need to directly display and work with the Excel file while still allowing you to work with the data inside that file. If you are trying to get the data outside of the file, here are two approaches among many:
You might consider using the Excel object model and COM interop to read the data from the Excel file into your application. Granted, this includes a dependency on Excel being installed, but it is a possibility. This article has some great code for getting started with reading Excel files in this way.
A better way might be to use a library that doesn't have a dependency on Excel being installed on the local system. This answer suggests using the Excel Data Reader library, available on CodePlex.
I know this answer side-steps your original answer of "hosting MS Office documents in [a] custom app," but in case what you're really interested in is the data inside those Excel files, hopefully this answer will prove helpful.
This is a horrible hack and should only be considered as a last resort: SendKeys.Send("{O}");
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys%28VS.71%29.aspx
Something similar to
_ieCtrl.Navigate("C:\\test.xls", False);
(code to sleep or wait may be needed here)
SendKeys.Send("{O}");
Basically, you send the "o" key to the dialog so it presses the "open" option. You are simulating a keyboard presses to click the "open" button. It is hackey because
1) you may need to wait in between
calls. If you send the o key before
the dialog is up it will be missed.
Hopefully the navigate call is finished when the dialog pops (dont know behavior of control in c#). You may need to experiment with the time since different computers will open faster\slower
2) If the dialog is not shown on a
computer, you will be inserting "o"s
into it. This may cause problems when
exiting because it may popup another dialog to try and save
the changes. May be able to prevent this by opening it in read-only mode
3) Different versions or windows may need different sendkeys commands. For example, you may need to send "o" and them the "{enter}" key
4) Probably more :)
If you want to open the file in a separate Excel instance (not embedded in the WebBrowser control), you can simply call
Process.Start(#"C:\Test.xls");
Office was never meant to run in embedded mode, not in a web page or in an ActiveX Document host. Microsoft had time and time again given us the warning. From pulling dsoframer from the knowledge base to skipping the BrowserFlags registry key in Office 2007.
Move to Office add-ins, Excel Web Access or Office Web Apps as quickly as you can.
We have a .NET Winforms application that hosts the Crystal Reports Viewer control (Version XI). It works well for the most part but when an export of data from the viewer is performed the application will crash on exit and in unmanaged code. The error message is not very useful and just says that an incorrect memory location was accessed. No other info such a specific DLL etc. is provided.
This only happens after the viewer is used to export a report to CSV, XML etc. My guess is that at some point in the export process Crystal creates a resource that attempts an action on shut down to a parent window (perhaps) that no longer exists.
I've seen a number of memory leak and shut down issues with Crystal but this one's new. Has anyone seen it and come up with a workaround or has ideas for workarounds?
So far we've tried explicitly disposing of all crystal-related objects, setting to null and even setting a Thread.Sleep cycle on shut down to "give Crystal time to clean up."
Update:
The crash happens only on shut down (so not immediate)
All export formats work
All export files are created properly
CR is installed on the same machine as the hosting .NET app
not sure about exporting from the IDE... is that even possible?
Check your code to ensure that you only open the Crystal Application object once during the lifecycle of your application. This will prevent excessive memory usage, usage that may be causing your crashes. Obviously, you can open and close reports at will.