none
Access document properties during Document Open event? RRS feed

  • Question

  • Hi,

     

    I am trying to access a document's properties as it is being loaded, to check for a certain value and if exists trigger the document management systems save- dialog. 

     

    This fails whenI open the document from a custom document library-add in we have, but works if opened from a Sharepoint portal on our intranet. 

     

    The code that throws the exception is:

     

     

    object oDocAuthorProp = typeDocCustomProps.InvokeMember("Item",

                                           BindingFlags.Default |

                                           BindingFlags.GetProperty,

                                           null, oDocCustomProps,

                                           new object[] { strIndex });

     

     

    And the exception is just "[System.Reflection.TargetInvocationException] = {"Exception has been thrown by the target of an invocation."}" 

     

    and

     

    "InnerException = {"The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))"}".

     

     

    From this I assume there is a problem with one of the parameters I pass, but I cannot see the error...the values of the parameters at the time of exception are:

    http://bildr.no/view/768086

     

    Could anyone please assist with this?

     

     

    Thanks!

    Monday, November 29, 2010 12:49 PM

Answers

  • Hi csharp

    <<As you can see none of them have the sought for name, and I get an exception: http://bildr.no/view/768167>>

    Believe me, I know the feeling when I come back to a message and see that I forgot to copy what I meant to include... I take it this should be the same information in your original message?

    The way I know Word, the error is coming up because the Document Properties aren't yet accessible. Possibly, that add-in is still doing something with the document, which would explain why working with such a document is a bit different than when a document is opend otherwise. So, if the DocumentProperty.Items isn't avialable, you get the "bad parameter" exception.

    One way to test this would be to set a Timer in the Document_Open event that will execute a some milliseconds after the event has finished.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by csharp.net Tuesday, December 7, 2010 9:39 AM
    Friday, December 3, 2010 9:39 AM
    Moderator

All replies

  • I'd guess that the problem is that the document is not completely open at the time the code executes when the document is opened via the management system. You don't give much information about how this functions, though, so it's hard to be sure or to make suggestions...
    Cindy Meister, VSTO/Word MVP
    Monday, November 29, 2010 2:53 PM
    Moderator
  • Hi, thanks for you reply.

     

    My first idea too, was that it might not have been fully opened yet at the time of execution.

     

    I have tried debugging a little bit more and will try to explain a little better.

     

    The management system is a Word-add-in that opens a window next to Word itself. In this window there is a hierarchy of categories, and they in turn contain sample-documents that our employees can open and use a "template" of how that certain type of document might look. This add-in has been developed by external consultants, so I don't really know what it does when the user clicks on the "Use document" button, but it opens in Word and my code executes a document open event which I have tapped into. My code runs in  a separate add-in on VSTO 2008.

     

    I have som code to check whether the document that is being opened is from this library or not, that looks like this:

     bool ReallyIsKBDoc()

            {

                Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

                object oDocCustomProps = doc.CustomDocumentProperties;

                Type typeDocCustomProps = oDocCustomProps.GetType();

     

                Microsoft.Office.Core.DocumentProperties customProps = (Microsoft.Office.Core.DocumentProperties)doc.CustomDocumentProperties;

                Microsoft.Office.Core.DocumentProperty custProp = null;

                try

                {

                    custProp = customProps["Document_x0020_Type"];

                }

                catch (Exception ex)

                {

     

                }

     

                if (custProp != null && custProp.Value.ToString() != "")

                    return true;

                else

                    return false;

     

            }

     

    So, I look for the property "Document_x0020_Type", which is what tells me that the document originates for the library. When I put a watch on the customProps variable, I get these values:

    http://bildr.no/view/768166

     

    As you can see none of them have the sought for name, and I get an exception:

    http://bildr.no/view/768167

     

    However, when the document actually has loaded and I check the properties in Word, it looks like this:

    http://bildr.no/view/768168

     

    And there you can see both the property that I want, the ones mye code found, and some more as well...

     

    So, is there a way of finding this property through code? After all the same code works fine if I open the document from the SharePoint server instead of the library add-in...

     

    Thanks again!

     

    Monday, November 29, 2010 3:16 PM
  • Cindy Meister is a strong source of experienced help. Perhaps she will have more insights after reviewing your second post. Your issue is more complex than usual but there may be an answer in finding someone else's experience for the same set of errors.

    When you BING using the argument "Exception has been thrown by the target of an invocation" you get 3,580,000 results. Your second message
    innerException = {"The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))"}

    substantiates your conclusion that one of your arguments is incorrect, but which one?
    An alternative to writing code to find the source of the exception, consider running a debugger while you exercise your existing code in Visual Studio. Check the blog:

    http://blog.fredrikhaglund.se/blog/2009/12/22/debugging-exception-has-been-thrown-by-the-target-of-an-invocation/

    If you need more in-depth help with your question that falls into the paid support category which requires a more in-depth level of support.  In that case visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone


    Chris Jensen
    • Marked as answer by csharp.net Tuesday, December 7, 2010 9:39 AM
    • Unmarked as answer by csharp.net Tuesday, December 7, 2010 9:39 AM
    Thursday, December 2, 2010 10:46 PM
    Moderator
  • Hi csharp

    <<As you can see none of them have the sought for name, and I get an exception: http://bildr.no/view/768167>>

    Believe me, I know the feeling when I come back to a message and see that I forgot to copy what I meant to include... I take it this should be the same information in your original message?

    The way I know Word, the error is coming up because the Document Properties aren't yet accessible. Possibly, that add-in is still doing something with the document, which would explain why working with such a document is a bit different than when a document is opend otherwise. So, if the DocumentProperty.Items isn't avialable, you get the "bad parameter" exception.

    One way to test this would be to set a Timer in the Document_Open event that will execute a some milliseconds after the event has finished.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by csharp.net Tuesday, December 7, 2010 9:39 AM
    Friday, December 3, 2010 9:39 AM
    Moderator
  • Cindy, 

     

    once again you save my day :-)

     

    Using the Timer to postpone the execution of my code to check the properties of the document worked fine, and I am now able to countinue the project.

     

    Thank you so much!

     

     

    Tuesday, December 7, 2010 9:39 AM