views:

40

answers:

1

Hi,

I'm new to programming with the OpenXML SDK and I've tried excessively to locate and read text that is between two document fields, but never really succeeded. There are tons of samples and tutorials on the web about almost everything you can think of doing with the OpenXML SDK, from setting watermarks to doing merge mail, but not only one about processing document fields.

My word document looks something like this:

{ Field1 } data { Field2 }

and what I want to do, is to read the data that is between Field1 and Field2.

I succeeded to the point to locate all the fields I need like this:

var qryFieldCode = (from p in procDoc.MainDocumentPart.Document.Body.Descendants()
                    where p.GetType() == typeof(FieldCode)
                    select p).ToList();

But what can I do to read the text that is between those fields I found?

Any help is greatly appreciated.

A: 

Find your first field (much like above) and then get an .ElementsAfterSelf.TakeWhile until where p.GetType() doesn't = typeof(FieldCode). Then just get the .Value of that query and you'll have your text. This won't be a great solution if you have things like tables between your two fields, but for your example above, it will work.

Otaku