How to read text from a Word document text box via C#

Asked

Viewed 370 times

2

I am using Interop to manipulate a word, through an application console.

using Word = Microsoft.Office.Interop.Word; 

I can extract the byte array, save as PDF, as image etc, but a relatively simple thing I’m needing I haven’t yet managed.

I need to replace a certain document text, which is inside a text box, but I don’t know how to do this.

From the variable _documentoWord I can access the elements, but in my attempts it hasn’t worked yet.

_aplicativoWord = new Word.Application() {Visible = false};
_documentoWord = _aplicativoWord.Documents.Open(caminho);

1 answer

2


I figured out a way that will suit me:

    public static void SearchTextBox(string name, string newContent)
    {
        _documentoWord.Content.ShapeRange.Ungroup();

        foreach (Word.Shape i in _documentoWord.Content.ShapeRange)
        {
            if (i.AlternativeText.IndexOf(name) != -1)
            {
                i.TextFrame.TextRange.Text = "";
            }
        }
    }

First I ungrouped everything that could be grouped together. Then I scanned the shapes of the document. If the alternative text is equal to the text I am looking for I enter Textframe.TextRange.Text and modify the text by deleting it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.