OutOfMemory Exeption VSTO Add-in Office 2016 64bit - c#

As the Title suggests I get an OutOfMemory Exeption in my VSTO Add-in in Excel 2016-64bit.
That happens when I update a Pivottable with the Table.Update() method which is provided by the VSTO Add-in by Microsoft.
My Machine has 16 GB RAM and the Process Memory in VS 2017 shows that only about 270 MB are used.
It works when I refresh the PivotCache wiht PivotCache().Refresh(); but that takes about 20 minutes which is
way too long.
AdditionalInformation would be that the Memory Exception occurs instantly when I hit my Update Button.
For a real memory exception I would expect the process Memory to rise over time till it is full and then throw an exception.
Any Ideas or suggestions would be nice. At this point I have no Idea where to look.

The issue is hard to reproduce in a minimal solution but apparently i am not the only one with that problem.
https://social.msdn.microsoft.com/Forums/office/en-US/18a6abc5-c390-41a9-8db7-206dbd86c509/getting-out-of-memory-exception?forum=exceldev
This person had the same problem in 2014 an the issue remained unresolved.
It seem like a rare bug in VSTO Addin.
I programmed a workaround:
Takes about 1 min so its fine since i need to update the Pivot Table once or twice a day.
try
{
//some other code
Table.Update();
//Some ohter Code
}
catch(OutOfMemoryExeption)
{
Table.Pivotcache().Refresh();
faildAny = true;
}

Related

Visual Studio exception of type 'system.outofmemoryexception'

I'm trying to load 2.5 million rows of data from a csv file as a string. This is x64 (not 32bit) for visual studio (Debug = Any CPU, Release = Any CPU. Windows is also 64 bit.)
I've preloaded the empty database I'm populating:
public static string[,] allNames = new string[2500000, 12];
I then proceed to fill it. I previously only had 2 million rows, and it was working fine. I then adding more rows, and got the memory error.
Looking at task manager, I could clearly see of my 16gb I had 4gb still free, but I went and bought 16gb more ram anyways as I couldn't figure out what the problem could be.
Lo and behold, the error still comes up.
I tried splitting the data into two different dataframes, and it still gives the error at the same spot. Removing one or two columns, makes it go a bit farther, or till the end if it's one of the columns with longer strings (longest strings are almost 1000 characters long).
This is Visual Studio 2019. Any help would be great!
There's a project setting that says "prefer 32-bit". Make sure it's not checked.
You should really look at refactoring you application so that you don't read all of your data into memory at once. None of us here can help you with this question unless you include more information
Solution 1:
You can split your csv file into 3 files each file should contain max 1 million of records and then you can upload it one by one.
If still problem exists then keep 5 lakh records per file.
Your problem will be resolved now for temporarily.
You can see here max size of string. What is the maximum possible length of a .NET string?
Solution 2:
Instead of string object you can use StringBuilder object.

Exception from Word.ApplicationClass.Activedocument all of a sudden

I have this single line of code in a Word VSTO add-in project which used to work but suddenly doesn't anymore:
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Before it just returned the Document object as it was supposed to, and I could read from and manipulate it without issue. But now it throws a System.Runtime.InteropServices.COMException exception. "Word has encountered a problem." Very helpful. The exception contains an HRESULT of 0x800A13E9, which hasn't helped me so far either. Out of memory or something? Idk.
The new behavior seems to have started around New Year's Eve (not sure since I wasn't using the add-in for the past couple of weeks), and it started while the add-in was installed, i.e. nothing in the code was changed, recompiled or reinstalled to make it happen. I've since rebuilt the project, thinking maybe a certificate had expired or something, but the error still occurs.
It's maybe worth noting that I can still access other properties of the Application instance. For instance, this line does not throw an exception:
int numDocuments = Globals.ThisAddIn.Application.Documents.Count;
But then when I iterate over the Documents collection with foreach, it just skips over the loop as if the count was zero.
The only thing I can think of is that an update to Office (365) has broken something related to VSTO. But where do I even begin debugging this?
Okay, so I tried creating a new Word VSTO add-in, changing none of the default project properties, but adding a single ribbon with a single button and the following method:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
try
{
var x = Globals.ThisAddIn.Application.ActiveDocument;
}
catch (Exception ex)
{
;
}
}
It throws exactly the same exception on getting the ActiveDocument property. I did however notice that there are two more exceptions thrown when loading the add-in, and they're also thrown when loading the original add-in (that used to work fine):
Exception thrown: 'System.Deployment.Application.DeploymentException' in System.Deployment.dll
Exception thrown: 'System.Security.Cryptography.CryptographicException' in Microsoft.VisualStudio.Tools.Applications.Hosting.dll
They don't prevent the add-in from loading and running, but maybe they're related to the problem anyway? I don't know if they were also thrown last year before the problem appeared.
I do however have a couple of Excel add-ins that still run fine with the same version of VSTO, Visual Studio and Office, and they don't throw the above two exceptions when loading. So the issue seems to be specific to Word.
And now I tried rolling back to Office 365 version 1810, the October release, which definitely worked before, so I think it's probably not a problem introduced by an Office update. It's something else. Probably.
Not that it really helps me much, but at least I've ruled that out. Probably.
The problem seems to be triggered by the Windows 10 October 2018 Update (or November). The affected customers had special Windows regional settings: Example: "English (Switzerland)".
Solution:
Change the Windows regional settings format to "English (UK)" or "English (US)"
The VBA Editor also no longer worked correctly for the affected customers (without installed add-ins). The error "Word has encountered a problem" (Visual Basic Error 5097) occurred directly during opening. The same error that occurs in C# for Application.ActiveDocument
Well, rolling back to the previous version of Windows 10 (1803 in my case, apparently) fixed the issue, and I can't reproduce it at the moment, so I'll mark this as solved for now.
While I still have no clue what exactly the deal was, I did at least learn in my research that clicking the "Check for updates" button in Windows 10 instantly makes you a beta tester for Microsoft. As in, they deliberately put you on the unstable update train, without any sort of notice, let alone a warning that you're about to install updates that they don't consider ready for release yet.
Anyway, I hope they got some good telemetry from my many, many hours of trying everything I could think of, so that maybe the issue is fixed by the time the update is forced on me. If not, I guess I'll be back to ask the same question again. At least I'll know where to start looking for the cause.
From my experience :
when i was closing the Active Document at that time i was refreshing Ribbon Menu according to the active document.
If the closing document was the Last document i wasn't able to read any property of
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
and i was facing the same issue like you.
whenever i close any document i check
if(Globals.ThisAddIn.Application.Documents.Count = 1){
LastActiveDocument = true;
}
And when i refresh the Ribbion Menu i check
if(!LastActiveDocument){
// then only read any property of Active document
}
Microsoft has fixed the issue
Microsoft has released an Office update to fix this issue.
With Office Version 1902 (Build 11328.20158) the issue is fixed on my side.

COM+ Application Identity is not set

I see a strange issue(Most likely my code is not complete), can some one take a look and help me out why following code runs fine but no changes are happening. Whn I go and take a look at identity tab, nothing has changed. OS is windows 2008 server r2
COMAdminCatalog cc = new COMAdminCatalog);
COMAdminCatalogCollection ccc = (COMAdminCatalogCollection)cc.GetCollection("Applications");
ccc.Populate();
foreach(COMAdminCatalogObject cap in ccc){
if(cap.Name.ToString() == config.Application){
//cap.set_value("Authentication",COMAdminAuthenticationLevelOptions.COMAdminAuthenticationConnect)//I have changed all options here but no effect
cap.set_Value("Identity", config.user);
cap.set_Value("Password", config.password);
ccc.SaveChanges();//breakpoint here
}
}
It turned out that ccc.SaveChanges() returns an error code =1 does any one know what it means?
MS documentation says error codes returned are in hex values such as 0x800401 etc(just made up)
I did this myself (learn something new every day!) and this is my results, hopefully we can figure out your problem:
I created a new COM+ server Application and copied your code (with minor changes so it finds my app, because I don't know what config is in your example :)
From debugging up to ccc.SaveChanges() - this line should throw exceptions if something went wrong. In my case, I got an UnauthorizedAccessException - I fixed that by running my code in Visual Studio as administrator
Once I did that, ccc.SaveChanges() worked - I went to my Identity tab and it updated the user and password correctly (if you enter a wrong username or password in the code, it also throws an exception I believe).
The return type you are getting, the 1, isn't an error code I believe. Because that's what mine returns and it is working fine.
You could try surrounding your ccc.SaveChanges() in a try catch and see if any exceptions are thrown.
I hope this helps.
First of all THANK YOU ryrich for taking time to help me out. What it turned out to be the reason for which my changes were not shown is as dumb as it possibly can be. It is consistent on 3 machines where I tested it. Here is what I noticed.
Every time I run the code above, it did work as there were no errors, but I could not see the changes in Component Services Console, the messed up part is that I HAD to close the Component Services console completely, then restart it and only then I could see the changes.
This behavior was consistent on all three systems where I tested it.
1: WinXP sp3
2: Win 7 SP1
3: Win 2008 Server R2
I am explaining this so just in case some one else might see this STUPID behavior...

IRibbonUI.invalidate crashes Excel 2010 custom ribbon with COM exception 0x80004005... sometimes

OK, this is a strange problem we've got with our Add-in for Excel. It works well with Excels 2003 - 2013, but from time to time we get reports about our ribbon disappearing. It seems to happen only on start of Excel 2010 and when opening sheet with our data in it. Our add-in is huge and with problem happening once in a few weeks, there is little I can show you, but I will write some specifics and hope someone can come up with any recommendation on what to do next.
Right now, it seems to be happening only with Excel 2010 (not absolutely sure about this, though)
It happens in a callback so it smells like synchronization issue, but not sure if that should be happening at all
This is the code that gets called in our callback
private void SetShowNotConnectedButton(bool show) {
if(_ribbon != null) {
_ribbon.Invalidate();
}
if(_notConnectedButton != null) {
_notConnectedButton.Visible = show;
}
}
So my question is, is there any way I don't know of for calling invalidate from a callback, or anyone knows any other issue that might be there with invalidation of ribbon in any version of Excel.

Reconnect to a disconnected Excel COM Interop instance

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.

Categories