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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.Te xt;
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?
[code]
foreach (Word.Shape shp in wordDocument.Shapes)
{
if (shp.TextFrame.HasText < 0)
{
String theText = shp.TextFrame.TextRange.Te
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?
ASKER
I'm a genius.
foreach (Word.Shape shp in wordDocument.Shapes)
{
if (shp.TextFrame.HasText < 0)
{
String theText = shp.TextFrame.TextRange.Te xt;
shp.Anchor.InsertAfter(the Text);
}
}
foreach (Word.Shape shp in wordDocument.Shapes)
{
if (shp.TextFrame.HasText < 0)
{
String theText = shp.TextFrame.TextRange.Te
shp.Anchor.InsertAfter(the
}
}
ASKER
Gave me a good start but didn't answer the question.
http://docx.codeplex.com/
its a library for manipulating Docx via Code
hope this helps