Guest User

Untitled

a guest
Aug 28th, 2010
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Word = Microsoft.Office.Interop.Word;
  5. using System.IO;
  6.  
  7. namespace WordTest
  8. {
  9.     class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             object oMissing = System.Reflection.Missing.Value;
  14.  
  15.             // Start Word and create a new document.
  16.             Word.Application oWord;
  17.             Word.Document oDocOriginal; // the template
  18.             Word.Document oDocNew; // the output
  19.  
  20.             object oEndOfDoc = @"\endofdoc";  // predefined bookmark of document end
  21.             oWord = null;
  22.             try
  23.             {
  24.                 oWord = new Word.Application();
  25.                 oWord.Visible = false;
  26.                 // We are reading th template
  27.                 object oFileName = Path.GetFullPath(@"..\..\Dyplom.doc");
  28.                 oDocOriginal = oWord.Documents.Add(ref oFileName, ref oMissing, ref oMissing, ref oMissing);
  29.  
  30.                 // We are creating the output document
  31.                 oDocNew = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  32.  
  33.                 string[] students = new string[] { "Jan Kowalski", "PaweÅ‚ Nowak", "Anna Nowakowska" };
  34.  
  35.                 // for each student
  36.                 for (int i = 0; i < students.Length; i++)
  37.                 {
  38.                     // We're copying data from the template document
  39.                     oDocNew.Bookmarks.get_Item(ref oEndOfDoc).Range.FormattedText = oDocOriginal.Content.FormattedText;
  40.  
  41.                     // And changing the tag
  42.                     foreach (Word.Range tmpRange in oDocNew.StoryRanges)
  43.                     {
  44.                         tmpRange.Find.Text = "%UCZEN%";
  45.                         tmpRange.Find.Replacement.Text = students[i];
  46.                         tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
  47.                         object replaceAll = Word.WdReplace.wdReplaceAll;
  48.                         tmpRange.Find.Execute(ref oMissing, ref oMissing, ref oMissing,
  49.                             ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  50.                             ref oMissing, ref oMissing, ref oMissing, ref replaceAll,
  51.                             ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  52.                     }
  53.                     // And adding a page break at the end if it's not the last student
  54.                     if (i < students.Length - 1)
  55.                     {
  56.                         object oPageBreak = Word.WdBreakType.wdPageBreak;
  57.                         oDocNew.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertBreak(ref oPageBreak);
  58.                     }
  59.                 }
  60.                
  61.                 // we save the resulting document
  62.                 object FileName = Path.GetFullPath(@"..\..\Wynik.doc");
  63.                 oDocNew.SaveAs(ref FileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  64.                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  65.                     ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  66.             }
  67.             finally
  68.             {
  69.                 if (oWord != null)
  70.                 {
  71.                     // Close Word
  72.                     object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
  73.                     oWord.Quit(ref doNotSaveChanges, ref oMissing, ref oMissing);
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment