Windows Develop Bookmark and Share   
 index > Windows Forms General > Integration of windows application from Ms word
 

Integration of windows application from Ms word

Hello Everybody..

I got sum problem, plz help me for this.
I want to create a windows application. In which i have 20 textboxs and a button.
On other side i have a MS word file, in which i have a paragraph with sum blank spaces or fill in the blanks(in between paragraph).
Now the task is
When i click the button the textbox value should get entered in to MS word blank spaces..

How to do this? I have no idea abt this...
praveen.khade  Tuesday, October 06, 2009 5:11 PM
You already asked this question and I answered at http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a905cfce-714b-433e-92f4-dc4d43ddb64e 1st october.

Why don't you track your old post?

If my solution didn't work you can tell which part didn't work or what's the problem.

However here is my answer again.


Lets assume you have a word file which likes this. and named Template.docx.

Hi <%Name%>

This is a sample text.

Here is the text : <%Text%>

 

        static void Main(string[] args)
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("<%Name%>", "Tamer");
            d.Add("<%Text%>", "This is the text in my software");
            GenerateWord(@"C:\users\Tamer\Desktop\template.docx", @"C:\users\Tamer\Desktop\generated.docx", d);
        }
        protected static void GenerateWord(string templateFile, string newFile, Dictionary<string, string> replacements)
        {
            if (File.Exists(templateFile))
            {
                Microsoft.Office.Interop.Word.Application my_Word = new Microsoft.Office.Interop.Word.Application();
                my_Word.Visible = false;
                object oTrue = true;
                object oAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                object oFalse = false;
                object oMissing = System.Reflection.Missing.Value;
                object oLoc = templateFile;
                object oNewFile = newFile;

                Microsoft.Office.Interop.Word.Document my_Document = (Microsoft.Office.Interop.Word.Document)my_Word.Documents.Open(ref oLoc, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                foreach (KeyValuePair<string, string> kvp in replacements)
                {


                    my_Word.Selection.Find.Replacement.ClearFormatting();
                    my_Word.Selection.Find.ClearFormatting();
                    my_Word.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;

                    object oFindText = kvp.Key;
                    object oReplaceText = kvp.Value;
                    my_Word.Selection.Find.Execute(ref oFindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oTrue, ref oReplaceText, ref oAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                }
                my_Document.SaveAs(ref oNewFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                my_Document.Close(ref oTrue, ref oMissing, ref oMissing);
                my_Word.Quit(ref oTrue, ref oMissing, ref oMissing);
            }
        }




In this document <%Name%> means that this will be changed to something else.

And here is the method which will do your Job. You have to add Microsoft.Office.Interop as a reference to your project.

Tamer Oz  Tuesday, October 06, 2009 5:21 PM
Thanks Tamer.
I did in the same way, this may help sumone like me..
Now my problem is when ever i run this it finds "_____"  and replace it completely wherever it finds (Like i have object findtext = "_____" to be replaced by textbox1.text value).. I dnt want like dat

I have 10 text boxes in windows form and 10 blanks ("_____") in word.

I want
1st textbox to be placed in 1st blank and
2nd textbox value in 2nd blank
.
.
.
.
10th textbox value in 10th blank
How to do this? will u modify this code bit??

public Form1()
        {
            InitializeComponent();         
        }
private void button1_Click(object sender, EventArgs e)
        {
           
            String destpath = textBox1.Text + textBox2.Text;

            System.IO.Directory.CreateDirectory(destpath);

            System.IO.File.Copy(@"C:\Users\khade\Desktop\Text.doc", destpath + @"\IS" + textBox2.Text + "Text.doc");

            //Word._Application wdApp;

            //System.Diagnostics.Process.Start(destpath + @"\IS" + txtProjTitle.Text + "_FTP_001_01.doc");

            //this.Activate();

            object Missing = System.Reflection.Missing.Value;
            object findtext = "_____";
            object matchcase = true;
            object matchwholeword = true;
            object matchwildcards = false;
            object soundslike = false;
            object matchwordforms = false;
            object forward = true;
            object wrap = false;
            object format = false;
            object replacewith = textBox1.Text;
            object replace = true;
            object replaceAll = Word.WdReplace.wdReplaceAll;
            object matchkashida = false;
            object matchdiacritics = false;
            object alehamza = false;
            object matchcontrol = false;

            object fileToOpen = (object)@"C:\Users\khade\Desktop\Text.doc";
            object filetosave = (object)@"" + textBox1.Text + textBox2.Text;


            Word.Application app = new Word.ApplicationClass();
            Word.Document doc = new Word.Document();


            doc = app.Documents.Open(ref fileToOpen, 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);

            app.Visible = false;

            app.WindowState = Word.WdWindowState.wdWindowStateMaximize;

            app.Selection.Find.Replacement.ClearFormatting();
            app.Selection.Find.Replacement.Text = "IS" + textBox2.Text;

            app.Selection.Find.Execute(ref findtext, ref matchcase, ref matchwholeword, ref matchwildcards,
            ref soundslike, ref matchwordforms, ref forward, ref wrap, ref format,
            ref replacewith, ref replaceAll, ref matchkashida, ref matchdiacritics,
            ref alehamza, ref matchcontrol);



            doc.Save();

            doc.Close(ref Missing, ref Missing, ref Missing);
            app.Quit(ref Missing, ref Missing, ref Missing);

        }
praveen.khade  15 hours 36 minutes ago
Use some naming that differates fields from others.

ex:

instead of _______ for the area of textbox1 value use <%TextBox1%>, for the area of textbox2 use <%TextBox2%>

then if you use my code, it will put the textboxes values into these spaces.

The other way is more complicated.
Tamer Oz  8 hours 50 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Music
• TabControl problem...
• How to make tiles in Visual Basic 2005
• Date problem
• Progress Bar does is not updated!!!
• convert content of a textArea to BITMAP?
• How to create own spell checker window in vb.net (windows)
• connecting to a remote sql server database from form
• DragDrop file into RichTextBox *without* icon?
• Getting User's Windows Groups