Microsoft Developer Network >
Página Inicial dos Fóruns
>
.NET Base Class Library
>
Writing to a specific field in word file form c#
Writing to a specific field in word file form c#
- 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 ?
Respostas
- 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);- Sugerido como RespostaTamer OzMVPsexta-feira, 6 de novembro de 2009 4:05
- Marcado como RespostaeryangMSFT, Moderadorsegunda-feira, 9 de novembro de 2009 8:51
Todas as Respostas
- 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);- Sugerido como RespostaTamer OzMVPsexta-feira, 6 de novembro de 2009 4:05
- Marcado como RespostaeryangMSFT, Moderadorsegunda-feira, 9 de novembro de 2009 8:51

