Word Automation -- what a bind!

Hi everyone,

Before I go completely insane , can somebody please help me solve this single, simple problem:

Using late binding, how on earth do I get hold of a single Section in a Word document

I can get hold of the full Sections collection like this:

object sections = theDocument.GetType().InvokeMember("Sections", BindingFlags.GetProperty, null, theDocument, null);

...but can I hell get at the individual Section objects that it contains. Surely it can't be that hard, but I've been stuck on this for days now!

Any help or guidance would be greatly appreciated!

Thanks in advance,

-tegs



Answer this question

Word Automation -- what a bind!

  • webguynj

    Hi JJ,

    Thanks for the quick reply...

    > I won't ask why late binding, but...

    No, please do ask why! I'm new to this binding lark and would appreciate any insights you might have. I'm currently using early binding, but from what I can find on the web that's a big no-no, so I've been trying to convert.

    The application I'm building needs to run on client machines with Word versions 97 and up (and gracefully catch those that have none installed).

    > what have you tried so far

    Everything! I'm fed up...

    Using the link you provided, I tried this:

    object sections = theDocument.GetType().InvokeMember("Sections", BindingFlags.GetProperty, null, theDocument, null);
    sections.GetType().InvokeMember("Item", BindingFlags.GetProperty|BindingFlags.Instance, null, sections, new object[] { 1 });

    Word launches an I still get an invocation exception. Nightmare!

    -tegs


  • Giriraj Singh

    I won't ask why late binding, but...

    - what have you tried so far

    - does this help: http://www.dotnet247.com/247reference/msgs/4/23438.aspx


  • Trivikram Dwivedi

    Hey - that'll do me! Works perfectly!

    Thanks JJ - you're a star!

    -tegs


  • ScuzziOne

    Another way (inspired by the answer to your other post):



    object section = sections.GetType().InvokeMember("Item", BindingFlags.Default | BindingFlags.InvokeMethod, null, sections, new object[] {1} );

    In the other post, Information was treated as a property, here Item is treated as a method. Seems confusing and inconsistent to me but whoever said COM Interop was simple.


  • ZsoltKiraly

    While you're waiting for someone more knowledgeable than me to come up with the answer you could try the following rather kludgey workaround which uses the fact that the Sections object implements IEnumerable.



    IEnumerator enumerator = ((IEnumerable)sections).GetEnumerator();
    enumerator.Reset();
    enumerator.MoveNext();
    object section = enumerator.Current;


  • Word Automation -- what a bind!