2

I am developing a .NET program using VSTO 2010 running .NET 4.0 to find a specific subheading in a set of word documents and copy all content under that subheading (say "Requirements") using Word.Interop. I succeeded by means of a for loop that matched words, using which I search for this word and then the starting word of the next section (say "Functionality").

Now the documents also have a contents page so i found that simple word matching wouldn't do as it would return the first seen occurrence which was definitely in the contents section. So I tried finding the second occurrence an was successful but then realized that it could even be that the word might repeat itself much before the subheading. Hence I resorted to finding the sentence. here I was successful here in finding both the words (I had to modify the search string to "Requirements\r" because thats how it was being read) Anyhow. The problem i am facing now is that after I get the starting and ending sentences, I selected the entire document and using MoveStart and MoveEnd , i reduced down the selection before copying it and pasting it in another word document,(as i dont know about using Range or Bookmark) However , while i was successful in moving the start and though the end position was correct, the MoveEnd always moves to some text that is at least 10 sentences beyond the actual. I've been at this for 2 weeks now and any help in this matter would be greatly appreciated. I dont mean any disrepect to all the programmers out there in the world. I've shown the code I'm using.

The variables used are self explanatory. //SourceApp and SourceDoc - Word application that reads source of release notes //DestinationApp and DestinationDoc = Word application that writes into new document

    private void btnGenerate_Click(object sender, EventArgs e)
    {
        int startpos = findpos(SourceDoc, 1, starttext, sentencecount);
        int endpos = findpos(SourceDoc, startpos, endtext, sentencecount);

        object realstart = startpos - 1; // To retain the subheading
        object realend = -(sentencecount - (endpos - 1)); // to subtract the next subheading

        SourceDoc.Activate();
        SourceDoc.ActiveWindow.Selection.WholeStory();
        SourceDoc.ActiveWindow.Selection.MoveStart(WdUnits.wdSentence, realstart);
        SourceDoc.ActiveWindow.Selection.MoveEnd(WdUnits.wdSentence, realend); // the problematic bit
        SourceDoc.ActiveWindow.Selection.Copy();
        IDataObject data = Clipboard.GetDataObject();
        string allText = data.GetData(DataFormats.Text).ToString();

        DestinationDoc.Activate();
        DestinationDoc.ActiveWindow.Selection.WholeStory();
        DestinationDoc.ActiveWindow.Selection.Delete();
        DestinationDoc.ActiveWindow.Selection.Paste();
        DestinationDoc.Save();

        ((_Application)SourceApp).Quit();
        ((_Application)DestinationApp).Quit();
        textBox1.AppendText(allText);

    }


    int findpos(Document docx, int startpos, string txt, int sentencecount)
    {
        int pos = 0;
        string text;
        for (int i = startpos; i <= sentencecount; i++)
        {
            text = docx.Sentences[i].Text;
            if (string.Equals(text, txt))
            {
                pos = i;
                break;
            }
        }

        return pos;
    }

I would also be extremely grateful if there was a way to extract specific subheading only (like 3.1 , 5.2.3 etc.) which is what I'm trying to achieve. The question is just my way of doing things and I'm open to a better way as well. Many thanks in advance.

ValarMorghulis
  • 79
  • 1
  • 13

0 Answers0