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.
Related
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;
}
Was working on new functionality in a code file... ManageTime.cs.
Had a bug fix request come in for a bug in the same file.
Used Visual Studio TFS to Suspend My Work.
Fixed bug in ManageTime.cs.
Now What?
Do I Check in ManageTime.cs, then can I resume my shelf set and will it handle merging the shelfset with the new feature code?
Afraid to try without some direction for fear of losing code.
Yes, if your team's policies say that you check in the code after your bug fix, go ahead and do that. If you aren't ready to check in or don't want to check in at this time, but are worried about losing work, you can always create another shelveset just in case (keep the Preserve Pending Changes box checked so you keep your bug fix changes).
Once you are ready, find and unshelve the shelveset. Just like getting the latest version from source control, VS will try to automerge the changes. If there's a conflict that it can't resolve, you'll get the same Resolve Conflicts window that we're used to seeing from time to time to manually resolve the conflicts.
Your assumption appears to be correct. Suspending your code essentially shelves the code along with a few Visual Studio settings such as window location, open files, and similar things. As long as you shelved (suspended) your in process work, then checked in a different bug fix, you should be able to merge with your updated source code once you resume and get latest version.
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...
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.
I am using the Office Integration Pack
After following instruction correctly I am still not able to get the Excel to Import working
My Visual Studio Lightswitch 2011 application is configured to host on IIS Server and use's Easy Shell (so its the default Shell provided by MS).
So far I have tried is calling the
OfficeIntegration.Excel.Import(
this.States,
#"C:\Users\Mr_Mia_Gie\My Documents\ExcelSheet.xls",
"Sheet1",
"A1:C3");
on _Execute event of a button (the button does not live on the Shell Command Bar)
The exception I get back is "Object variable or With block variable not set."
Any solution or suggestion will be highly appreciated
Cheers
I agree with Nevyn (& I'd vote up his answer, but it's embedded in the question as an edit so I can't).
As Nevyn has pointed out, there are really only three objects in that particular line of code that can be causing a null exception:
the OfficeIntegration object
the OfficeIntegration.Excel object
or, the this.States collection (unlikely though)
As was also pointed out for you, it's most likely that one of those objects isn't correctly initialised. You need to check what the value of those three objects are by putting a break point on that line & checking what their values actually are at that point.
You could also put a guard clause in your code (just above that line):
if (OfficeIntegration == null) || (OfficeIntegration.Excel == null) return;
It won't neccesarily "fix" the problem, but it will stop the null exception from occuring (but this shouldn't be a problem in an Execute method). But it's good programing practice to put a guard clause any time you're referencing an object whose value could be null.
Failing that, the only other advice any of us can give you is to post a question in the questions section of the gallery page, where you downloaded the extension from. The autor of the extension should be able to help you.
Is this a web application, if so I dont think it supports it. It requires an extenstion. I dug up an article for you, try it out:
http://blogs.msdn.com/b/lightswitch/archive/2011/04/13/how-to-import-data-from-excel.aspx
The office integration Pack is for Visual Studio LightSwitch windows application and does work for application that are hosted on IIS hence the below code throws exception
OfficeIntegration.Excel.Import(
this.States,
#"C:\Users\Mr_Mia_Gie\My Documents\ExcelSheet.xls",
"Sheet1",
"A1:C3");
The link show that the extension does not support LS WebBrowser application http://officeintegration.codeplex.com/discussions/374585
Also the extension Import data from Excel does work for IIS hosted LS application that runs in webbrowser