SeanVDH
SeanVDH

Reputation: 916

Word Interop Not Extracting Data

I'm using Word 2016 and I'm having trouble getting data from the object.

My snippet is as such:

    void Application_DocumentOpen(Word.Document document)
    {
        var test = document.Paragraphs;
    }

Before the assignment looking at paragraphs gives:

Evaluation of method Microsoft.Office.Interop.Word.DocumentClass.get_Paragraphs() calls requires a COM call. Evaluation of methods on COM objects is not supported in this context.

After it simply gives: System._ComObject with the member NativeView which has 0x0df4fd18

I've tried searching both errors, but haven't found related results.

UPDATE So I compared this to the example they give, which is:

    void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
        Doc.Paragraphs[1].Range.InsertParagraphBefore();
        Doc.Paragraphs[1].Range.Text = "This text was added by using code.";
    }

What's neat is that if I look at Doc.Paragraphs[1].Range.Text for their example, I get the text of the paragraph. If I try to do it with my version I get: Cannot apply indexing with [] to an expression of type 'Paragraphs'

So... Paragraphs isn't returning what I expect it to. Is it possible that I don't have access to the document since the method is firing before it loads?

Upvotes: 1

Views: 197

Answers (1)

SeanVDH
SeanVDH

Reputation: 916

Apparently only the methods/properties that have been called exist within the class. It makes sense, since it prevents the object from loading massive amounts of data constantly, but it was a bit confusing to debug.

In any case, changing test to doc.Paragraphs[1].Range.Text seems to have made the object viewable.

Upvotes: 1

Related Questions