Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void btnExportWord_Click_1(object sender, EventArgs e)
- {
- object oMissing = Missing.Value;
- Word.Application oWord = new Word.Application();
- Word.Document oWordDoc = new Word.Document();
- oWord.Visible = false;
- oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
- Object oTemplatePath = @"C:testMyXMLTemplate.dotx";
- oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
- foreach (Word.Field myMergeField in oWordDoc.Fields)
- {
- Word.Range rngFieldCode = myMergeField.Code;
- String fieldText = rngFieldCode.Text;
- if (fieldText.StartsWith(" MERGEFIELD"))
- {
- Int32 endMerge = fieldText.IndexOf("\");
- Int32 fieldNameLength = fieldText.Length - endMerge;
- String fieldName = fieldText.Substring(11, endMerge - 11);
- fieldName = fieldName.Trim();
- for (int i = 0; i < treeViewXMLFiles.Nodes[0].Nodes.Count; i++)
- {
- string strFilename=treeViewXMLFiles.Nodes[0].Nodes[i].Text;
- string strFull_path=treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[0].Text;
- string strCreationTime=treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[1].Text;
- string strSize = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[2].Text;
- if (fieldName == "File_Name")
- {
- myMergeField.Select();
- oWord.Selection.TypeText(strFilename);
- }
- if (fieldName == "Full_Path")
- {
- myMergeField.Select();
- oWord.Selection.TypeText(strFull_path);
- }
- if (fieldName == "CreationTime")
- {
- myMergeField.Select();
- oWord.Selection.TypeText(strCreationTime);
- }
- if (fieldName == "Size")
- {
- myMergeField.Select();
- oWord.Selection.TypeText(strSize);
- }
- }
- }
- }
- Object oSaveAsFile = (Object)@"C:testFINISHED_XML_Template.doc";
- oWordDoc.SaveAs(ref oSaveAsFile, 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);
- oWordDoc.Close(false, ref oMissing, ref oMissing);
- oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement