如何在Windows Server 2003中生成Word文档? [英] How to generate word document in windows server 2003?

查看:84
本文介绍了如何在Windows Server 2003中生成Word文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了使用Office Interop Word对象生成Word文档的功能.我的代码如下所示.在我的本地系统上运行正常.但是,当我尝试从Windows Server 2003运行它时,它不起作用.它会继续加载Word.Document wordDoc = wordApp.Documents.Open,并且不执行任何操作.

I had written a function to generate a word document by using the Office Interop Word object. My code is as shown below. It is working fine in my local system. But when i try to run it from window server 2003 it is not working. It keeps loading at Word.Document wordDoc = wordApp.Documents.Open and doesn''t do anything.

private void GenerateEmploymentCertificate()
    {
        object Nothing = System.Reflection.Missing.Value;
        object format = Word.WdSaveFormat.wdFormatDocument;
        Word.Application wordApp = new Word.ApplicationClass();

        object srcFileName = Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Templates\Employment Certificate.doc"));
        Word.Document wordDoc = wordApp.Documents.Open
       (ref srcFileName, ref format, 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);
        try
        {

            object bookmarkDate = "Date";
            wordDoc.Bookmarks.get_Item(ref bookmarkDate).Select();
            wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblRequestdate.Text);

            object bookmarkEmployeeName = "EmployeeName";
            wordDoc.Bookmarks.get_Item(ref bookmarkEmployeeName).Select();
            wordApp.Selection.Text = lblEmployeeName1.Text;

            object bookmarkCompany = "Company";
            wordDoc.Bookmarks.get_Item(ref bookmarkCompany).Select();
            wordApp.Selection.Text = lblCompanyName1.Text; 

            object bookmarkJoiningDate = "JoiningDate";
            wordDoc.Bookmarks.get_Item(ref bookmarkJoiningDate).Select();
            wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblJoiningDate1.Text); 

            object bookmarkDesignation = "Designation";
            wordDoc.Bookmarks.get_Item(ref bookmarkDesignation).Select();
            wordApp.Selection.Text = lblDesignation1.Text;

            string DocName;
            DocName = string.Format("{0}_employment_certificate", lblRequestNo.Text);
            hFilename.Value = DocName;
            wordDoc.SaveAs(Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Letters\" + DocName + ".doc")));

        }
        catch (Exception exp)
        {
            Session["generalError"] = null;
            Session["generalError"] = "There was an error at generating the letter. Please send email to unify.admin@unilever.com with this screen shot.<br /><br /><br />Request No:" + lblRequestNo.Text + "<br />Action:Submit<br />" + exp.StackTrace.ToString();
            LogManager logHelper = new LogManager(Request.PhysicalApplicationPath.Trim(), "Leave System - Malaysia");
            logHelper.LogError("[btnSubmit_Click - ]" + exp.Message + ".StackTrace - " + exp.StackTrace.ToString());
            Response.Redirect(ResolveUrl("~/Error/ErrorHandler.aspx"));
        }
        finally
        {
            // Close wordDoc2
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            if (wordDoc != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
                wordDoc = null;
            }
            // close wordApp
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            if (wordApp != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                wordApp = null;
            }
        }
        GC.Collect();
    }

推荐答案

我还尝试将代码包装在try catch语句中.仍然没有任何反应.我的功能还有其他选择吗?您能帮我吗?
I also tried wrapping the code in a try catch statement. Still nothing happens. Is there any other alternative for my function? Could you please help me?


From the code which I have given in my above post, I am trying to achieve word automation under asp.net 4 and in windows server 2003 R2 Standart x64 Edition. I searched in google but I could not find any solution. I even tried the solutions from following threads but nothing is working for me.

[Word Automation][1]

[Word 2007 Documents][2]

What could be the problem and the solution to it? Could you please help me?

Thanks.


  [1]: http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/65a355ce-49c1-47f1-8c12-d9cf5f23c53e
  [2]: http://social.msdn.microsoft.com/Forums/en/architecturegeneral/thread/0f5448a7-72ed-4f16-8b87-922b71892e07


这篇关于如何在Windows Server 2003中生成Word文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆