Link to home
Start Free TrialLog in
Avatar of zhukovl
zhukovl

asked on

C# to replace text boxes with text in a word document

I need to replace all of the text boxes in a .docx file with the text they contain in c#.

I have some code that converts the document to a plain text file but the problem is that it doesnt save the stuff inside of text boxes when it saves it as a .txt.

Does that make sense? I'm converting a .docx file to a .txt file but before I can do that I need to get the text out of text boxes and replace them with just text. Actually it doesnt even matter if we remove the text box, just copying the text inside and pasting it either above the text box or below it would do it since the text boxes are ignored when the file is saved.
Avatar of roeib
roeib

i recommend you to use the following library,
http://docx.codeplex.com/

its a library for manipulating Docx via Code

hope this helps
ASKER CERTIFIED SOLUTION
Avatar of ashraf882
ashraf882
Flag of Bangladesh image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zhukovl

ASKER

That will find the text boxes but it search for particular text in those text boxes. What I need to do is replace the text boxes with what is in them. We're getting close.
Avatar of zhukovl

ASKER

Alrighty, here is where we are now.

[code]
    foreach (Word.Shape shp in wordDocument.Shapes)
                {
                        if (shp.TextFrame.HasText < 0)
                        {

                                       String theText = shp.TextFrame.TextRange.Text;
                                       MessageBox.Show(theText);
                       
                         }
                }
[/code]

Is where I have the text in each text box. How do I set the range to right before or right after the shape and then paste the contents of the string in there?


Avatar of zhukovl

ASKER

I'm a genius.

                foreach (Word.Shape shp in wordDocument.Shapes)
                {
                       if (shp.TextFrame.HasText < 0)
                       {
                               String theText = shp.TextFrame.TextRange.Text;
                                shp.Anchor.InsertAfter(theText);
                        }
                }
Avatar of zhukovl

ASKER

Gave me a good start but didn't answer the question.