各位大侠,我有一个问题想请教一下,我现在由于工作需要,作了一个C#操作word文档的小程序,但是在调试的过程中出现了一个问题。我用单步调试,发现是程序运行到
Word.Document WordDoc=WordApp.Documents.Open(ref filename, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair , ref documentDirection, ref noEncodingDialog);
这一步时再也不往下运行,我打开任务管理器,发现进程里面已经有了word进程。
我的程序如下:
public void output_word(string templatename,string outputname, bool Print, bool PrintPreview, bool DropWork)
{
           bool printpreview=PrintPreview;
           bool print = Print;
           object filename=templatename;
           object savename=outputname;
           object confirmConversions = Type.Missing;
           object readOnly = Type.Missing;
           object addToRecentFiles = Type.Missing;
           object passwordDocument = Type.Missing;
           object passwordTemplate = Type.Missing;
           object revert = Type.Missing;
           object writePasswordDocument = Type.Missing;
           object writePasswordTemplate = Type.Missing;
           object format = Type.Missing;
           object encoding = Type.Missing;
           object visible = Type.Missing;
           object openConflictDocument = Type.Missing;
           object openAndRepair  = Type.Missing;
           object documentDirection = Type.Missing;
           object noEncodingDialog = Type.Missing;
           object missing = Type.Missing;
           Word.ApplicationClass WordApp = new Word.ApplicationClass();
           Word.Document WordDoc=WordApp.Documents.Open(ref filename, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair , ref documentDirection, ref noEncodingDialog);
           WordDoc.Activate();   
           WordDoc.SaveAs(ref savename,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
WordDoc.Close(ref missing,ref missing,ref missing);  
WordApp.Application.Quit(ref missing, ref missing, ref missing); 
}

解决方案 »

  1.   

    http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/dv_wrcore/html/wrconwhiterabbitarchitecture.asp
      

  2.   


    using System;
    using System.IO;
    using System.Web;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;
    using System.Collections;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using Word;namespace ExcelSample
    {
    /// <summary>
    /// WordMaker 的摘要说明。
    /// </summary>



    public class WordMaker : System.IDisposable
    {
     private static readonly log4net.ILog objLog = log4net.LogManager.GetLogger(typeof(WordMaker));
            
    object filename;
    object MissingValue = Type.Missing;  private Word.Application objWord = null;
    private Word.Document objDocument = null; 

    private string strTempleteFileName = null;
    private HttpRequest objRequest = null;        //创建一个新word对象  构造函数
    public WordMaker(string templeteFileName)
    {
    if(templeteFileName != null && templeteFileName != string.Empty)
    {
    try
    {
    objRequest = HttpContext.Current.Request;
    strTempleteFileName = templeteFileName; string strTempleteFilePath = objRequest.MapPath("XlsTemplets/" + strTempleteFileName);
    //创建一个Word对象 objWord =new Word.ApplicationClass();
    filename = objRequest.MapPath("XlsTemplets/" + strTempleteFileName);
    objDocument = objWord.Documents.Add(ref filename,ref MissingValue,ref MissingValue,ref MissingValue);
    /*objDocument = objWord.Documents.Open(ref filename,ref MissingValue,
    ref Nothing,ref Nothing,ref Nothing,ref Nothing,
    ref Nothing,ref Nothing,ref Nothing,ref Nothing,
    ref Nothing,ref Nothing,ref Nothing,ref Nothing,
    ref Nothing,ref Nothing); 
    */
    objWord.Visible = false;

    }
    catch (Exception ex)
    {
    Clean();
    throw ex;
    }
    }
    else
    {
    throw new Exception("模版不存在");
    }
    }
    //向Word模版中插入数据
    public void Insertstr(string str)
    {

    try
    {
                    
    objDocument.Paragraphs.Last.Range.Text = str;
    }
    catch (Exception ex)
    {

    Clean();
    throw ex;
    }
    } //填充Word并下载
    public void DoResponse()
    {
    try
    {
    string strOutDirPath = objRequest.MapPath(RtnOutPath.GetOutDirPath()); DirectoryInfo dir = new DirectoryInfo(strOutDirPath);

    if (!dir.Exists)
    {
    dir.Create();//如果目录不存在,则创建
    } string strOutFileWebPath = RtnOutPath.GetOutFilePath(strTempleteFileName);

    //保存填充后的Word文件
    filename = objRequest.MapPath(strOutFileWebPath);
    object Nothing  = System.Reflection.Missing.Value; 
    objDocument.SaveAs(ref filename,ref MissingValue,ref MissingValue,ref MissingValue,ref MissingValue,
    ref MissingValue,ref MissingValue,ref MissingValue,
    ref MissingValue,ref MissingValue,ref MissingValue,
    ref MissingValue,ref MissingValue,ref MissingValue,
    ref MissingValue,ref MissingValue);

    //重定向下载
    HttpContext.Current.Response.Redirect(strOutFileWebPath, false);
    }
    catch (Exception ex)
    {
    Clean();
    throw ex;
    } }
    private void Clean()
    {
    //关闭WordDoc文档对象
    objDocument.Close(ref MissingValue, ref MissingValue, ref MissingValue); 

    //关闭WordApp组件对象
    objWord.Quit(ref MissingValue, ref MissingValue, ref MissingValue);

     try
     {
     if(objDocument != null)
     {  Marshal.ReleaseComObject(objDocument);
     objDocument = null;
     }
     }
     catch (Exception ex)
     {
     objLog.Error(ExceptionHelper.GetExceptionLogString(ex));
     }  try
     {
     if(objWord != null)
     {  Marshal.ReleaseComObject(objWord);
     objWord = null;
     }
     }
     catch (Exception ex)
     {
     objLog.Error(ExceptionHelper.GetExceptionLogString(ex));
     }
    }
            public void Dispose()
    {
    Clean();

    } ~WordMaker()
    {
    Clean();

    } public class ExceptionHelper
    {
    private ExceptionHelper() {} public static string GetExceptionLogString(Exception ex)
    {
    return new StringBuilder(150)
    .Append("Source: ")
    .Append(ex.Source)
    .Append(Environment.NewLine)
    .Append("Message: ")
    .Append(ex.Message)
    .Append(Environment.NewLine)
    .Append("StackTrace: ")
    .Append(ex.StackTrace)
    .ToString();
    }
    }// End Class ExceptionHelper
    }
    }
      

  3.   

    luoboqingcai:
    我的程序中就是需要打开一个已经存在的word模板,好像是要用Documents.Open()方法才能实现,可是不知道为什么,我的机器这个方法老是过不去,编译能够通过,而且程序运行时也没有报错,单步调试到那一步就运行不下去了。你怎么把我要用的东西注释掉了?
      

  4.   

    我在Word.ApplicationClass WordApp = new Word.ApplicationClass();
    之后添加了WordApp.Activate();
    我在调试的时候通过捕捉异常,结果出现
    最有可能的“System.Runtime.InteropServices.COMException”类型的异常出现在 contract.dll 中其他信息: 无法激活应用程序请问我该如何解决?
      

  5.   

    你可以试试用进程打开,用System.Diagnostics.Process类操作!
    试试!
    不知道能不能对你有用!