1

I am trying to replace the temporary text in a word document with new text from a list. It works if the text is not in a shape, but once it tries to find the text in a textbox it throws an error. Here is what I have so far:

public void FindReplace(List<repvals> replaceVals, string docLocation, int listLen)
        {

            //Opens a new Word application
            var app = new Microsoft.Office.Interop.Word.Application();
            //Opens the .docx
            var doc = app.Documents.Open(docLocation, true, false);

            //Selects the document
            var range = doc.Range();

            for (int i = 0; i < listLen; i++)
            {

                //Finds the parameter, then replaces
                range.Find.Execute(FindText: Convert.ToString(replaceVals[i].tempVal), Replace: WdReplace.wdReplaceAll, ReplaceWith: Convert.ToString(replaceVals[i].Boxes));

                var shapes = doc.Shapes;
                //Finds text within textboxes, then changes them
                foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
                {
                    var initialText = shape.TextFrame.TextRange.Text;
                    var resultingText = initialText.Replace(Convert.ToString(replaceVals[i].tempVal), Convert.ToString(replaceVals[i].Boxes));
                    shape.TextFrame.TextRange.Text = resultingText;
                }

            }
            //prints document
            doc.Save();
            doc.Close();

            //fully closes Word
            Marshal.ReleaseComObject(app);
        }

The problem occurs when it hits

var initialText = shape.TextFrame.TextRange.Text;

And throws an error saying: "This object does not support attached text."

The text in the shapes are nothing special. (e.g. tDATE, tNAME, etc.)

Any ideas?

7
  • Are you using Word 2007? c-sharpcorner.com/Forums/Thread/42176 Commented Nov 5, 2013 at 16:37
  • Have you tried what Andrew Paes is proposing? Writing just shape.TextFrame.TextRange instead of shape.TextFrame.TextRange.Text? Commented Nov 5, 2013 at 19:20
  • I have, but it still throws the same error. Picture of the error: i.imgur.com/1ld41hP.png Commented Nov 5, 2013 at 20:01
  • @Corvertbibby If you have found an answer please pose it as an answer not as an edit to the question. Commented Nov 5, 2013 at 20:50
  • @Servy I tried that at first, But since I am under 10 reputation I cannot do that until after 8 hours from asking. Commented Nov 5, 2013 at 21:12

2 Answers 2

2

I found the answer. Turns out my code was fine, however the document I was using (which I didn't write), had another shape on the second to last page to form a place to sign your name. I replaced that with an underscore, ran the code, and everything changed perfectly.

For those who also experience this problem, try checking how many shapes your foreach loop has counted: https://i.sstatic.net/ACzah.png

Thank you Andrew and varocarbas for the help

Sign up to request clarification or add additional context in comments.

Comments

0

*"In 2003 the AltText default for a standard textbox WAS the contained text BUT since you can change the Alt Text to NOT match it was never a good idea to read it this way. In 2010 the default for Alt Text is blank

If the textbox is named "Text Box 2" (substitute the correct name if not) MsgBox ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange should work."*

-- John SR Wilson

http://answers.microsoft.com/en-us/office/forum/office_2010-customize/shapesalternativetext-is-blank-for-the-docx/7671c746-2c2b-41d9-b7de-389a766587a7?page=2&msgId=31041d67-e62b-4ce0-b283-57fd6a4ff6b2

3 Comments

You can write a bit by your own and refer to the original Q/A via link; don't need to quote another answer word by word.
Alright. I just wanted to keep the response as the original and also maintain the credit of the author. Thanks for the tip, I'm still learning how to interact with the questions that arise and how to respect other people's responses. I noticed that the question has been answered. I will wait for a response from the person who asked to not generate more irrelevant content.
This is what I thought. You should answer the question with your own words and, eventually, add a link to another answer. If you consider that there is another answer addressing this problem exactly and want to add nothing to it, just post the link via comment. Also you should edit your answer as soon as you see a problem with it, regardless of what others do (think about any reader coming here looking for some help).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.