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

  1. Quit方法,解决办法是加\color{red}{下划线} Word._Application oWord
 /// <summary>
 /// 关闭 Word.Application
 /// </summary> 
 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方法,解决办法是加\color{red}{下划线} Word._Document oDoc
/// <summary>
/// 关闭Word文件
/// </summary>
/// <param name="oDoc"></param>
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;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

友情链接更多精彩内容