Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a Windows Forms program using C#. It contains textboxes and a data-grid.

I want to export the data into a Word file to use the template. I added the library of Microsoft Word into my code.

Also I created textboxes "shapes" inside the Word file and putted inside them text to be find from my c# program

For now everything seems okay, But I'm stuck at the C# side, I can replace the normal text, but not the text inside textboxes "Inside the word file"

What I have tried:

And here is my code:
<pre lang="c#"><pre>private void FindAndReplace(Word.Application wordapp, object ToFindText, object replaceWithText)

        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object matchAllforms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiactitics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object matchread_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            wordapp.Selection.Find.Execute(ref ToFindText,
                ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundLike,
                ref matchAllforms, ref forward,
                ref wrap, ref format, ref replaceWithText,
                ref replace, ref matchKashida,
                ref matchDiactitics, ref matchAlefHamza,
                ref matchControl);

        }


        private void CreateWordDocument(object filename, object SaveAs)
        {
            Word.Application wordApp = new Word.Application();
            object missing = Missing.Value;
            Word.Document myWordDoc = null;

            if (File.Exists((string)filename))

            {
                object readOnly = false;
                object isVisible = false;
                wordApp.Visible = false;

                myWordDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);

                myWordDoc.Activate();

                //find and replave
                this.FindAndReplace(wordApp, "<date1>", textBox2.Text);
                this.FindAndReplace(wordApp, "<date2>", textBox3.Text);

            }

            else
            {
                MessageBox.Show("File not found!");
            }

            //Svae as
            myWordDoc.SaveAs2(ref SaveAs, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);


            myWordDoc.Close();
            wordApp.Quit();
            MessageBox.Show("File Created!");


         }
Posted
Updated 3-Sep-18 13:20pm

1 solution

The easiest way that I have found to figure out the code is to use the in-built Macro Recorder as it will write the code for you! ;)

Here is a video that should help: Intro to Word Macros - Tutorial on How to Edit a Word Macro - YouTube[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900