Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text above table MS Word

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?

like image 949
user194076 Avatar asked Nov 15 '11 01:11

user194076


People also ask

How do I overline in Word?

Select either the “Bar” under “Accents” on the drop-down menu… …or select the “Overbar” under “Overbars and Underbars”. The “Overbar” produces a slightly longer line above the text than the “Bar”.


2 Answers

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.

like image 69
user194076 Avatar answered Sep 19 '22 02:09

user194076


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
like image 20
mellamokb Avatar answered Sep 21 '22 02:09

mellamokb