Example Method for C to Operate word

  • 2021-12-13 09:07:23
  • OfStack

This article illustrates how C # operates word. Share it for your reference, as follows:


#region  Read word
/// <summary>
///  Read word All text content (excluding tables) 
/// </summary>
/// <returns>word Character content in ( Plain text )</returns>
public string ReadAllFromWord()
{
  Word.ApplicationClass app = null;
  Word.Document doc = null;
  object missing = System.Reflection.Missing.Value;
  object FileName = m_FilePath;//@"E:/ Learning pilot project /ReadFromWordDoc/test.doc";
  object readOnly = true;
  object isVisible = false;
  try
  {
    app = new Word.ApplicationClass();
    doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
      ref missing, ref missing, ref missing, ref missing, ref missing,
      ref missing, ref missing, ref missing, ref isVisible, ref missing,
      ref missing, ref missing, ref missing);
    string textString = "";
    // Read everything   He asked  hovertree.com
    textString = doc.Content.Text.Trim();
//        int ParCount = this.getParCount(doc);// Number of segments 
//        for (int i = 1 ; i <= ParCount ; i++)
//        {
//          textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
//        }
    textString = textString.Replace("/a","");  // Replaces an empty string with null. ( word Medium /a Represents an empty string, but in the C# In, it stands for ringing   Halo ~ ~) Otherwise it will ring when displaying console program 
    textString = textString.Replace("/r","/n");  // Replace carriage return with carriage return and wrap 
    return textString;
  }
  catch(Exception ex)
  {
    throw ex;
  }
  finally
  {
    if (doc != null)
    {
      try
      {
        doc.Close(ref missing, ref missing, ref missing);
      }
      catch
      {}
      doc = null;
    }
    if (app != null)
    {
      try
      {
        app.Quit(ref missing, ref missing, ref missing);
      }
      catch
      {}
      app = null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
  }
}
#endregion
#region  Append write word /// <summary>
///  Append write word
/// </summary>
/// <param name="InsertText"> String to be written to </param>
public void WriteToWord(string InsertText)
{
  Word.ApplicationClass app = null;
  Word.Document doc = null;
  object missing = System.Reflection.Missing.Value;
  object FileName = m_FilePath;//@"E:/ Learning pilot project /ReadFromWordDoc/test.doc";
  object readOnly = false;
  object isVisible = false;
  try
  {
    app = new Word.ApplicationClass();
    doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
      ref missing, ref missing, ref missing, ref missing, ref missing,
      ref missing, ref missing, ref missing, ref isVisible, ref missing,
      ref missing, ref missing, ref missing);
    // Activate word Document 
    doc.Activate();
    // Append to last 1 Segment ( The paragraph is based on  /n  To serve as a sign )
    doc.Paragraphs.Last.Range.Text = InsertText + "/n";// Add a Terminator ( Increase 1 Segment ), Otherwise, when you insert it again, it becomes a replacement .
    // Save 
    doc.Save();
  }
  catch(Exception ex)
  {
    throw ex;
  }
  finally
  {
    if (doc != null)
    {
      try
      {
        doc.Close(ref missing, ref missing, ref missing);
      }
      catch
      {}
      doc = null;
    }
    if (app != null)
    {
      try
      {
        app.Quit(ref missing, ref missing, ref missing);
      }
      catch
      {}
      app = null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
  }
}
#endregion

For more readers interested in C # related content, please check the topics on this site: "Summary of C # Operating Excel", "Summary of C # Programming Thread Use Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: