c# Word Quit和Close方法的二义性问题解决

  1. Quit方法,解决办法是加 Word._Application oWord
 /// 
 /// 关闭 Word.Application
 ///  
 public static void CloseApplication(Word.Application oWord, Word.Document oDoc)
 {
      if (oWord != null)
      {
          CloseDocument(oDoc);
          //Quit这儿会有警告
          oWord.Quit(ref _missing, ref _missing, ref _missing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
          oWord = null;
      }
 }

//解决办法,只需要将参数中的Word.Application oWord改成Word._Application oWord即可
public static void CloseApplication(Word.Application oWord, Word.Document oDoc)
 {
      if (oWord != null)
      {
          CloseDocument(oDoc);
          //警告消失
          oWord.Quit(ref _missing, ref _missing, ref _missing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
          oWord = null;
      }
 }
  1. Close方法,解决办法是加 Word._Document oDoc
/// 
/// 关闭Word文件
/// 
/// 
public static void CloseDocument(this Word.Document oDoc)
{
    if (oDoc != null)
    {
        oDoc.Save();     
        //Close这儿会有警告           
        oDoc.Close(ref _missing, ref _missing, ref _missing);
        oDoc = null;
    }
}

//解决办法,只需要将参数中的Word.Document oDoc改成Word._Document oDoc即可
public static void CloseDocument(this Word._Document oDoc)
{
    if (oDoc != null)
    {
        oDoc.Save(); 
        //警告消失               
        oDoc.Close(ref _missing, ref _missing, ref _missing);
        oDoc = null;
    }
}

你可能感兴趣的:(c# Word Quit和Close方法的二义性问题解决)