.NET Framework Developer Center > .NET Development Forums > .NET Base Class Library > Writing to a specific field in word file form c#
Ask a questionAsk a question
 

AnswerWriting to a specific field in word file form c#

  • Wednesday, November 04, 2009 1:51 PMALH1982 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can i send vlaues form c# application to a specific fields in word file
    because i have template of word file and this file contains fields like location, country
    i want to fill from c#
    so how can i do that ?

Answers

  • Wednesday, November 04, 2009 2:46 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    hi,

    can you try the following code?

                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 = @"C:\Sample.docx";
                object oNewFile = oLoc;
    
                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 (Field f in my_Document.Fields)
                {
                    if (f.Type == WdFieldType.wdFieldAddressBlock)
                    {
                        f.Result.Text = "sample";
                    }
                }
    
                my_Document.Save();
                my_Document.Close(ref oTrue, ref oMissing, ref oMissing);
                my_Word.Quit(ref oTrue, ref oMissing, ref oMissing);
    

All Replies

  • Wednesday, November 04, 2009 2:46 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    hi,

    can you try the following code?

                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 = @"C:\Sample.docx";
                object oNewFile = oLoc;
    
                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 (Field f in my_Document.Fields)
                {
                    if (f.Type == WdFieldType.wdFieldAddressBlock)
                    {
                        f.Result.Text = "sample";
                    }
                }
    
                my_Document.Save();
                my_Document.Close(ref oTrue, ref oMissing, ref oMissing);
                my_Word.Quit(ref oTrue, ref oMissing, ref oMissing);