Explanation about Excel workbook opening issue in C# - c#

I had an issue with Excel workbook opening. After some research I've found a way (https://support.microsoft.com/en-us/kb/320369) to solve this issue by changing the CultureInfo of the current execution thread.
Does anyone could bring me more information about this issue ? I mean, why this issue is happening? Is there a way to know where the problem is coming from exactly ? (Is this from a specific column or row ? Does it related with some properties within the .xlsx file ?...)
Hope I've been clear.
Thanks.
P.S: Sorry for my english

The first two solutions set your thread's culture information to match what Excel is expecting. The last solution does the reverse, and creates the 'missing' localization file.
The CurrentCulture property tells Excel how to localize the GUI. If you are running a version of Excel that doesn't perfectly 'mesh' with your version of Windows, the localizer cannot figure out what localization files should be used. I suspect that the Excel startup code has some logic that figures this out, but it doesn't get executed when you call Excel via automation, so you essentially have to do it manually (ie, any one of the steps described in the MS article).

Related

Is there a way to supress "Name Conflicts" dialog before opening XLSX file by C# in Excel 2007?

I am facing a problem while opening xlsx workbook by C# code.
Problem:
A pop appear while opening the xlsx, and user expected to acknowledge it.
Reason:
Reason of pop is known since this this is known problem.
Probable Solution: Needs to automate and suppress this pop up so no manual intervention is needed.
Reference of one such solution in VB:
http://social.msdn.microsoft.com/Forums/office/en-US/546c7506-7146-45b6-873e-b3793df2979b/is-there-a-way-to-supress-name-conflicts-dialog-in-excel-2007?forum=exceldev
Question to Forum:
Solution mentioned above is VB based. I am in search of such solution in C#.** And importantly **query is how to detect such pop up within code.
Pop up is seen after invoking: Workbook.Open(.....).
Also it is neither exception nor there is return value.
Is there any way to find out such pop in advance before opening file?
the link shows you not VB.NET code, its VBA code.
For this code you must allready open the XLSX document.
But i know no way to find it before you open the document.
One way, not a good way. To look into the xlsx file (its a zip archive) and find the place and remove it. You can try to use the OpenXML SDK from Microsoft to remove the defined name.
Here you find the description of OpenXML SDK for Excel:
http://msdn.microsoft.com/de-de/library/documentformat.openxml.spreadsheet(v=office.15).aspx
I hope it helps a little!
I got the same problem in a workbook which is getting filled dynamicaly via C# and filters some data.
After i executed this VBA code:
On Error Resume Next
With Workbook("YourWokrbookName")
.Names("_FilterDatabase").Delete
.Names("Criteria").Delete
.Names("Extract").Delete
End With
On Error GoTo 0
Which i found here and adapted for my use i never got the naming confict messagebox again.
The simply deletes the _FilterDatabase object in the worksheet. This might be not usefull in every case but in my case it didn't made a difference.

Check if Excel is in dirty state

Is there any way I can know if Excel is in dirty state or not.
By dirty state I mean:-
When you do anything on Excel and close save button - Excel asks you to save the file.
So there must be some flag which is set when the file is edited.
Can I know the status of an Excel file through C# code?
Searched a lot, but not much help is available. One option is there which allows you to know if Excel is in edit state or not by looking at GetRibbonControlEnabled("FileNewDefault")
In this case you can see if Excel is in edit state only at the time when you execute this method.
What if I want to know if Excel was edited/made dirty since the time it was open.
Please don't advice to start to background thread which keeps looking if Excel was in edit mode by using the above function.
An help will be extremely appreciated.
Take a look at the Workbook.Saved property. It will tell you if the user has modified the document since it was last opened.
bool isDirty = !Globals.Application.ActiveWorkbook.Saved;
In an Excel VBA module one can test the ActiveWorkbook.Saved property, and if it is False then the workbook has unsaved changes (is "dirty"). Details here. See if you can check that property from your C# code.

C# Excel 2010 Workbook Open error

We recently upgraded from Excel 2007 to Excel 2010, and we have found that existing code started failing.
Exception Message:
Office has detected a problem with this file. To help protect your
computer this file cannot be opened.
We have traced this to the line where we open the file
excelApp.Workbooks.Open
Even when opening the file manually, the Protected View Messagebox comes up.
How can we work arround this using C#.
Have a look at using Application.FileValidation Property (Excel) before your Open statement.
Returns or sets how Excel will validate files before opening them.
Read/write
Files that do not pass validation will be opened in a Protected View
window. If you set the FileValidation property, that setting will
remain in effect for the entire session the application is open.
You can set it to one of the enum values in MsoFileValidationMode Enumeration
msoFileValidationDefault
msoFileValidationSkip
if you set it to msoFileValidationSkip before the Open statement, it should bypass the check.
Something like
excelApp.FileValidation = MsoFileValidationMode.msoFileValidationSkip;
before the open statement.
Late to the game here, but this is a common annoyance: you need to define a 'Trusted Location'.
You're not the only developers encountering this problem when your code tries to open a spreadsheet file, and "Office has detected a problem with this file. To help protect your computer this file cannot be opened." is an extremely unhelpful error message.
Look up the Trusted Location code published by Daniel Pineault on DevHut.net in 2010:
DevHut code example: Trusted Location using VBScript
I'll get downvoted to hellandgone for posting VBA in a C# forum, so I'd better not post my implementation of Daniel's code (yes, I'm a VBA developer, bashing out VBA macros all day, not a real coder working with pointy things and curly braces). If you really want to see the VBA, it's in a reply to another post:
https://stackoverflow.com/questions/2962728/office-trusted-locations/28115700#28115700
I believe the proper salutation is 'Share and Enjoy'.
Do, please, put an acknowledgement of the original author, Daniel Pineault, if you reuse the code: it's been widely published without attribution on 'Expert' sites, and that's rather rude.
We had the same issue. Our SSIS package at SQL server uses Excel.Interop to parse files. One day we installed Office 2010 x64 on new server and for some files started getting error:
Office has detected a problem with this file. To help protect your computer this file cannot be opened.
At the same time, other servers work good. We found distinguish in versions of Excel: 14.04763.1000 doesn't work, but 14.0.7015.1000 works for us. The last version number belong to Office 2010 SP2. Eventually we downloaded SP2 and installed it,as result, the error has gone.

Preserving clipboard on VSTO load

I've written a small application as a test to see what Excel VSTOs are capable of. However, I've run into a problem.
When I start Excel my clipboard is cleared. I've traced this down via Google to be a problem with Add-Ins (specifically COM add-ins) that when not initialized correctly will clear the clipboard automatically. Unfortunately for me I don't know how to preserve the clipboard in a VSTO and Google isn't much help. I don't call any functions that would obviously clear the clipboard. The only thing in the ThisAddin function is an if/else block that reads from some properties and sets some flags.
I've mulled over the idea of copying data on the clipboard to some object and then just replacing this but that seems very hacky and probably won't work.
Can anyone shed some light on how/why my VSTO clears the clipboard when Excel starts and how I can remedy it?
EDIT:
After tracing a little bit deeperI was wrong about it being my person test add-in. I figured out that it was either the Team Add-In or Adobe Contribute. Given Adobe's track record with these things I would go ahead and say it was probably the contribute add-in. Thanks everyone for your replies. This will teach me for using a non-vanilla installation of Excel for VSTO practice.
I will leave this topic open so that more people can contribute to possible reasons why a VSTO Excel Add-In (or any Add-In for the office suite) would clear the clipboard on start.
It would be best to identify the addin that's trashing the clipboard and removing it.
If that's not an option checkout some of the answers in Chris' link. I've done this using the Win32 API before. I think what's lacking in the listed answers is calls to OpenClipboard( http://msdn.microsoft.com/en-us/library/windows/desktop/ms649048%28v=vs.85%29.aspx) and CloseClipboard.
It's folly to try to backup/restore the clipboard, when complex data types are present. And you will generate unwanted clipboard events. See my prior answer here:
https://stackoverflow.com/a/2579846/289135
The COM addin Adobe PDF Maker, Bluetooth module clears the clipboard when Excel starts. Disable it to stop this behaviour.

VSTO Excel Add-in Caching issue and Load Behavior settings?

The strange problem I am having now is the Excel 2010 Add-in won't let me use the newer version. For some reason, it is still pointing to the old version and displaying the old UI.So my first impression about this is perhaps Excel caches stuff in the background or something.
This problem seems to be disappeared if I set the Add-in LoadBehavior value to 16 or 3 instead of 9 (the default value is set to 9). But I am not sure if it's a fix or probably I just somehow get lucky. Anyway, I hope someone can help explain a bit further about this problem. Thanks.
Have you appended "|vsto" to manifest path (check registry software/microsoft/office/exce/addins/[your addin name]/manifest. if this is not done vsto will always pick dll from clickone cache

Categories

Resources