Get text above table MS Word
Asked Answered
M

2

7

This one is probably a little stupid, but I really need it. I have document with 5 tables each table has a heading. heading is a regular text with no special styling, nothing. I need to extract data from those tables + plus header. Currently, using MS interop I was able to iterate through each cell of each table using something like this:

app.Tables[1].Cell(2, 2).Range.Text;

But now I'm struggling on trying to figure out how to get the text right above the table. Here's a screenshot: enter image description here

For the first table I need to get "I NEED THIS TEXT" and for secnd table i need to get: "And this one also please"

So, basically I need last paragraph before each table. Any suggestions on how to do this?

Muddy answered 15/11, 2011 at 1:6 Comment(0)
M
10

Mellamokb in his answer gave me a hint and a good example of how to search in paragraphs. While implementing his solution I came across function "Previous" that does exactly what we need. Here's how to use it:

wd.Tables[1].Cell(1, 1).Range.Previous(WdUnits.wdParagraph, 2).Text;

Previous accepts two parameters. First - Unit you want to find from this list: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdunits.aspx and second parameter is how many units you want to count back. In my case 2 worked. It looked like it should be because it is right before the table, but with one, I got strange special character: which looks like female indicator.

Muddy answered 15/11, 2011 at 7:19 Comment(1)
I thought googling for "How to get text above a table in word" would be completely futile, but I was desperate and I tried. This led me to a solution. Thanks! I C# with netoffice, you want to use the previousSibling() method on table.Scrivner
G
3

You might try something along the lines of this. I compare the paragraphs to the first cell of the table, and when there's a match, grab the previous paragraph as the table header. Of course this only works if the first cell of the table contains a unique paragraph that would not be found in another place in the document:

var tIndex = 1;
var tCount = oDoc.Tables.Count;
var tblData = oDoc.Tables[tIndex].Cell(1, 1).Range.Text;
var pCount = oDoc.Paragraphs.Count;
var prevPara = "";
for (var i = 1; i <= pCount; i++) {
    var para = oDoc.Paragraphs[i];
    var paraData = para.Range.Text;

    if (paraData == tblData) {
        // this paragraph is at the beginning of the table, so grab previous paragraph
        Console.WriteLine("Header: " + prevPara);
        tIndex++;
        if (tIndex <= tCount)
            tblData = oDoc.Tables[tIndex].Cell(1, 1).Range.Text;
        else
            break;
    }
    prevPara = paraData;
}

Sample Output:

Header: I NEED THIS TEXT

Header: AND THIS ONE also please
Glassware answered 15/11, 2011 at 2:33 Comment(1)
Thanks a lot for your help! Your solution gave me an idea on how to implement this. After spending some time, I figured out an easier way to do it. I'll post this as an answer.Muddy

© 2022 - 2024 — McMap. All rights reserved.