//版权所有 2001, Microsoft Corporation 
//保留所有权利 
// 
// 使用 
// 
// csc /r:word.dll example1.cs 生成此示例 
// using System; 
using System.Threading; 
using System.Reflection; 
using Word; public class MainClass 

public static int Main (string[] argv) 

//这相当于 CoCreateInstance 
Application app = new Application(); 
//确保 Word 可见 
app.Visible=true; 
Thread.Sleep (3000); //等待三秒钟 
// 现在像好用户一样,使用 Quit 方法来进行清理 
// 设置这些变量可视为向函数中传递空。 
// 这一点是必要的,因为引用不能传递 C# 空。 
object saveChanges = Missing.Value; 
object originalFormat = Missing.Value; 
object routeDocument = Missing.Value; 
app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); 
return 0; 


解决方案 »

  1.   

    //版权所有 2001, Microsoft Corporation 
    //保留所有权利 
    // 
    // 使用 
    // 
    // csc /r:word.dll example2.cs 生成此示例 
    // using System; 
    using System.Threading; 
    using System.Reflection; 
    using Word; public class MainClass 

    public static int Main (string[] argv) 

    //这相当于 CoCreateInstance 
    Application app = new Application(); 
    //确保 Word 可见 
    app.Visible=true; 
    //设置以创建一个空的纯文本文档 
    // 将这些变量设置为 Missing.Value 可视为向 
    // 函数中传递空。这一点很有必要,因为引用不能 
    // 传递 C# 空。 
    object template=Missing.Value; 
    object newTemplate=Missing.Value; 
    object documentType=Missing.Value; 
    object visible=true; 
    _Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible); Thread.Sleep (5000); //显示空文档五秒钟 
    doc.Words.First.InsertBefore ("This document is no longer empty!"); 
    Thread.Sleep (5000); //再等待五秒钟 //保存文件,并使用默认值(文件名除外) 
    object fileName = Environment.CurrentDirectory+"\\example2"; 
    object optional = Missing.Value; 
    #if OFFICEXP 
    doc.SaveAs2000( ref fileName, 
    #else 
    doc.SaveAs ( ref fileName, 
    #endif 
    ref optional, ref optional, ref optional, 
    ref optional, ref optional, ref optional, 
    ref optional, ref optional, ref optional, ref optional); // 现在像好用户一样,使用 Quit 方法进行清理 
    object saveChanges = true; 
    object originalFormat = Missing.Value; 
    object routeDocument = Missing.Value; 
    app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); 
    return 0; 


      

  2.   

    //版权所有 2001, Microsoft Corporation 
    //保留所有权利 
    // 
    // 使用 csc /r:word.dll example3.cs 生成此示例 
    // 
    // 
    // using System; 
    using System.IO; 
    using System.Threading; 
    using System.Reflection; 
    using Word; public class MainClass 

    public static int Main (string[] argv) 

    //这相当于 CoCreateInstance 
    Application app = new Application(); 
    //确保 Word 可见 
    app.Visible=true; 
    // 打开现有文档“example3.doc” 
    // 将这些变量设置为 Missing.Value 可视为向 
    // 函数中传递空。这一点很有必要,因为引用不能 
    // 传递 C# 空。 
    DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory); 
    FileInfo[] fi = di.GetFiles("example3.doc"); 
    if (fi.Length <= 0) 

    di = new DirectoryInfo(Environment.CurrentDirectory+@"\..\.."); 
    fi = di.GetFiles("example3.doc"); 

    object fileName = fi[0].FullName; 
    object optional=Missing.Value; 
    object visible=true; 
    #if OFFICEXP 
    _Document doc = app.Documents.Open2000( ref fileName, 
    #else 
    _Document doc = app.Documents.Open( ref fileName, 
    #endif 
    ref optional, 
    ref optional, 
    ref optional, 
    ref optional, 
    ref optional, 
    ref optional, 
    ref optional, 
    ref optional, 
    ref optional, 
    ref optional, 
    ref visible); Thread.Sleep (2000); //显示打开的文档五秒钟 
    object first=0; 
    object last=doc.Characters.Count; 
    //选择文档中的所有字符 
    doc.Range(ref first, ref last).Select(); 
    Thread.Sleep (2000); //显示此选择五秒钟 
    //现在剪切选定的文本 
    doc.Range(ref first, ref last).Cut(); 
    Thread.Sleep (3000); //再等待五秒钟 //保存文件,并使用默认值(文件名除外) 
    fileName += "_new"; 
    #if OFFICEXP 
    doc.SaveAs2000( ref fileName, 
    #else 
    doc.SaveAs ( ref fileName, 
    #endif 
    ref optional, ref optional, ref optional, 
    ref optional, ref optional, ref optional, 
    ref optional, ref optional, ref optional, ref optional); // 现在像好用户一样,使用 Quit 方法进行清理 
    object saveChanges = true; 
    object originalFormat = Missing.Value; 
    object routeDocument = Missing.Value; 
    app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); 
    return 0; 


      

  3.   

    我现在是在服务器端有个连接,点击后打开word文档,用户关闭时文档上传,看上去就像直接在本地编辑服务器上的word文档一样。
    主要问题是:客户一关闭word就保存。如何捕捉客户关闭了word文档这一事件?