Advertisement
Guest User

Word manipulation c#

a guest
Nov 24th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Microsoft.Office.Interop.Word;
  5.  
  6. namespace MyProjectNS
  7. {
  8.     public class HtmlWordDocument : IDisposable
  9.     {
  10.         private Application application;
  11.         private Document document;
  12.  
  13.         private string path;
  14.         private bool editing;
  15.  
  16.         public enum DocumentSection { Content, Header, Footer }
  17.  
  18.         public HtmlWordDocument(string path)
  19.         {
  20.             this.path = path;
  21.             this.editing = false;
  22.             if (File.Exists(path)) File.Delete(path);
  23.  
  24.             application = new Application();
  25.  
  26.             if (editing)
  27.             {
  28.                 document = application.Documents.Open(path, ReadOnly: false, Visible: false);
  29.             }
  30.             else
  31.             {
  32.                 document = application.Documents.Add(Visible: false);
  33.             }
  34.             document.Activate();
  35.         }
  36.  
  37.         public void Write(DocumentSection sectionType, string file)
  38.         {
  39.             try
  40.             {
  41.                 if (sectionType == DocumentSection.Content)
  42.                 {
  43.                     application.Selection.InsertFile(file);
  44.                 }
  45.                 else
  46.                 {
  47.                     foreach (Section wordSection in document.Sections)
  48.                     {
  49.                         Range range;
  50.  
  51.                         if (sectionType == DocumentSection.Header)
  52.                             range = wordSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
  53.                         else
  54.                             range = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
  55.  
  56.                         range.InsertFile(file);
  57.                     }
  58.                 }
  59.             }
  60.             catch (Exception ex)
  61.             {
  62.                 ((_Document)document).Close(SaveChanges: false);
  63.                 ((_Application)application).Quit(SaveChanges: false);
  64.             }
  65.         }
  66.  
  67.         private void WriteSection(DocumentSection sectionType, string file)
  68.         {
  69.             Write(sectionType, file);
  70.         }
  71.  
  72.         public void WriteContent(string file)
  73.         {
  74.             WriteSection(DocumentSection.Content, file);
  75.         }
  76.  
  77.         public void WriteHeader(string file)
  78.         {
  79.             WriteSection(DocumentSection.Header, file);
  80.         }
  81.  
  82.         public void WriteFooter(string file)
  83.         {
  84.             WriteSection(DocumentSection.Footer, file);
  85.         }
  86.  
  87.         public void Save()
  88.         {
  89.             if (editing)
  90.             {
  91.                 application.Documents.Save(true);
  92.             }
  93.             else
  94.             {
  95.                 document.SaveAs(path);
  96.             }
  97.         }
  98.  
  99.         #region IDisposable Members
  100.  
  101.         public void Dispose()
  102.         {
  103.             ((_Document)document).Close(SaveChanges: true);
  104.             ((_Application)application).Quit(SaveChanges: true);
  105.         }
  106.  
  107.         #endregion
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement