Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am having a requirement where i need to write/ fill up a textbox which has been added in the MS word 2007 through developer tool kit. I have written the following code
private void CreateWordDocument(object filename, object saveAs)
    {
        object missing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application wordapp = new ApplicationClass();
        Microsoft.Office.Interop.Word.Document aDoc = null;
        if (File.Exists((string)filename))
        {
            DateTime today = DateTime.Now;
            object readOnly = false;
            object isVisible = false;
            wordapp.Visible = false;
            aDoc = 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 isVisible, ref missing, ref missing,
                 ref missing, ref missing);
            aDoc.Activate();
            this.FindAndReplace(wordapp, "Health", "HEALTH1");
       
            aDoc.Content.InsertBefore("this is at the begin\r\n\r\n");
            if(aDoc.Content.ContentControls.Count > 0)
            {

               //I am not able to enter in to this loop because the   
               // above conditon is not able to find out what are a            // all the controls i have added to the word document
            }

            aDoc.Content.InsertAfter("\r\n\r\nThis is at the end");
            aDoc.SaveAs(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);
        }
        else
        {
            Response.Write("file doesnot exist");
            return;
        }

    }
    private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordapp, object findtext, object replaceWithtext)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchwildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordFormds = false;
        object forward = true;
        object format = false;
        object matchkashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object Visible = true;
        object replace = 2;
        object wrap = 1;
        wordapp.Selection.Find.Execute(ref findtext, ref matchCase, ref matchWholeWord, ref matchwildCards, ref matchSoundsLike,
                                           ref nmatchAllWordFormds, ref forward, ref wrap, ref format, ref replaceWithtext,
                                           ref replace, ref matchkashida, ref matchDiacritics, ref matchAlefHamza,
                                           ref matchControl);

    }


The Above code is working fine for writing the new text and replacing the existing text which in the MS word document. But i am not getting how to write the code so that i can programtically pass the value to fill up the Text Box what i have added through developer tool box in the word document. Please kindly help ASAP.

Regards,
Posted

1 solution

Why are you passed variables in this way:
private void CreateWordDocument(object filename, object saveAs)

and then you used:
(string)filename)


the correct is:
private void CreateWordDocument(String filename, String saveAs)


If you want to get TextBox, try to walk through InlineShapes collection.
 
Share this answer
 
Comments
Dalek Dave 26-Oct-10 17:40pm    
Good Call.

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