[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-01-02
6
Medium Priority
?
1,428 Views
Last Modified: 2012-05-08
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.
0
Comment
Question by:zhukovl
  • 4
6 Comments
 
LVL 3

Expert Comment

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

its a library for manipulating Docx via Code

hope this helps
0
 
LVL 11

Accepted Solution

by:
ashraf882 earned 1000 total points
ID: 26165202
Here is the solution -

 Word._Application oWord;
 Word._Document oDoc;
 oWord = new Word.Application();
 oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,ref oMissing, ref oMissing);
 FindReplace(oDoc, oWord, "(Date)", DateTime.Now.ToString("dd MMM yyyy"));
-------------------

public bool FindReplace(object wordDoc, object wordApplication, string findText, string replaceText)
        {
            bool isSuccess = false;
            do
            {
                if (wordDoc == null)
                {
                    break;
                }
                if (wordApplication == null)
                {
                    break;
                }
                if (replaceText.Trim().Length == 0)
                {
                    break;
                }
                if (findText.Trim().Length == 0)
                {
                    break;
                }
                Word.Application wordApp = wordApplication as Word.ApplicationClass;
                Word.Document wordDocument = wordDoc as Word.Document;
                ReplaceRange(wordDocument.Content, wordDocument, wordApp, findText, replaceText);

                foreach (Word.Comment comment in wordDocument.Comments)
                {
                    ReplaceRange(comment.Range, wordDocument, wordApp, findText, replaceText);
                }

                foreach (Word.HeaderFooter header in wordDocument.Sections.Last.Headers)
                {
                    ReplaceRange(header.Range, wordDocument, wordApp, findText, replaceText);
                }

                foreach (Word.HeaderFooter footer in wordDocument.Sections.Last.Footers)
                {
                    ReplaceRange(footer.Range, wordDocument, wordApp, findText, replaceText);
                }

                foreach (Word.Shape shp in wordDocument.Shapes)
                {
                    if (shp.TextFrame.HasText < 0)
                    {
                        ReplaceRange(shp.TextFrame.TextRange,
                        wordDocument, wordApp, findText, replaceText);
                    }
                }
                isSuccess = true;
            }
            while (false);
            return isSuccess;
        }

------------------------

private void ReplaceRange(Word.Range range, Word.Document wordDocument, Word.Application wordApp, object findText, object replaceText)
        {
            object missing = System.Reflection.Missing.Value;
            wordDocument.Activate();
            object item = Word.WdGoToItem.wdGoToPage;
            object whichItem = Word.WdGoToDirection.wdGoToFirst;
            object forward = true;
            wordDocument.GoTo(ref item, ref whichItem,ref missing, ref missing);
            object replaceAll = Word.WdReplace.wdReplaceAll;
            object matchAllWord = true;
            range.Find.ClearFormatting();
            range.Find.Replacement.ClearFormatting();
            range.Find.Execute(ref findText, ref missing, ref matchAllWord,
                               ref missing, ref missing, ref missing, ref forward,
                               ref missing, ref missing, ref replaceText, ref replaceAll,
                               ref missing, ref missing, ref missing, ref missing);
        }
     
0
 

Author Comment

by:zhukovl
ID: 26168315
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zhukovl
ID: 26168712
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?


0
 

Author Comment

by:zhukovl
ID: 26168817
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);
                        }
                }
0
 

Author Closing Comment

by:zhukovl
ID: 31672000
Gave me a good start but didn't answer the question.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question