Niels Ziegler
Guest
|
Whenever I click on a document that is initially in protected view (or in other words, downloaded from the internet/ opened from outlook), I get the following exception:
Exception Source: MyCompany.WordAddIn
Exception Type: System.NullReferenceException
Exception Message: Object reference not set to an instance of an object.
Exception Target Site: WordEventManager_DocumentOpen
---- Stack Trace ----
MyCompanyWordAddIn.AddinModule.WordEventManager_DocumentOpen(sender As Object, hostObj As Object)
AddinModule.cs: line 1169, col 13, IL 0007 (0x7)
AddinExpress.MSO.ADXWordAppEvents.DoDocumentOpen(doc As Object)
MyCompany.WordAddIn.DLL: N 0017 (0x11) IL
I immediately realized it was probably my own fault, because I am accessing custom document custom properties. I do this in the open event:
private void WordEventManager_DocumentOpen(object sender, object hostObj)
{
PrepareDocumentProperties();
}
I don't want to paste all of it, but this leads to the following actions:
DocumentProperties docprops = doc.CustomDocumentProperties as DocumentProperties;
if (docprops != null)
{
string property = GetCustomDocumentProperty(doc, propertyName);
if (property == null)
{
docprops.Add(propertyName, false, type, propertyValue);
doc.Saved = false;
}
}
I had to add Debugger.break() and open a new instance of VS2013 as debugger, because it never reached my breakpoint in the first line of the document_open method. I do reach the code, when I open a non-downloaded document. Do you know if there is some sort of sandbox involved with protected documents?
The exception itself will be raised, when I try to access ActiveDocument. ActiveWindow causes a different exception ("no document is open")
I tried checking the ActiveDocument.ProtectionType (==none) and ActiveDocument.ActiveWindow.View.Type (==print view) before I access the document properties, but only removing or commenting out the code really helps, because this check immediately runs into the Activedocument == null.
How can I avoid accessing those null objects? |
|
Andrei Smolin
Add-in Express team
Posts: 18823
Joined: 2006-05-11
|
Hello Niels,
You should use the ProtectedViewWindowOpen event. There's no active document, if the only document is opened in a ProtectedViewWindow.
Andrei Smolin
Add-in Express Team Leader |
|
Niels Ziegler
Guest
|
I am now using the supplied object from the method parameters, this works for both cases so far. Do you see any disadvantages?
private void WordEventManager_DocumentOpen(object sender, object hostObj)
{
Word.Document doc = hostObj as Word.Document;
PrepareDocumentProperties(doc);
} |
|
Andrei Smolin
Add-in Express team
Posts: 18823
Joined: 2006-05-11
|
Hello Niels,
Does this handle protected view related scenarios? If yes, then I don't see a disadvantage.
Andrei Smolin
Add-in Express Team Leader |
|
Niels Ziegler
Guest
|
I have not experienced any other cases except the protected view where it would fail. |
|
Andrei Smolin
Add-in Express team
Posts: 18823
Joined: 2006-05-11
|
Hello Niels,
I've created a test project that handles the DocumentOpen event as follows:
private void adxWordAppEvents1_DocumentOpen(object sender, object hostObj) {
System.Diagnostics.Debug.WriteLine("!!! adxWordAppEvents1_DocumentOpen. 0.");
Word.Document doc = hostObj as Word.Document;
System.Diagnostics.Debug.WriteLine("!!! adxWordAppEvents1_DocumentOpen. 1.");
Office.DocumentProperties docprops = doc.CustomDocumentProperties as Office.DocumentProperties;
System.Diagnostics.Debug.WriteLine("!!! adxWordAppEvents1_DocumentOpen. 2.");
if (docprops != null) {
string property = GetCustomDocumentProperty(docprops, "MyProperty");
System.Diagnostics.Debug.WriteLine("!!! adxWordAppEvents1_DocumentOpen. 3.");
}
System.Diagnostics.Debug.WriteLine("!!! adxWordAppEvents1_DocumentOpen. 4.");
}
private string GetCustomDocumentProperty(Office.DocumentProperties docprops, object propertyName) {
string result = null;
Office.DocumentProperty property = null;
try {
property = docprops[propertyName];
} catch (Exception ex) {
//skip
}
if (property != null)
result = property.Value.ToString();
return result;
}
It works correctly. As expected, it isn't invoked when I open a document loaded from the web and you need to handle the ProtectedViewWindowOpen event in this case.
Andrei Smolin
Add-in Express Team Leader |
|