none
c# 导出到word RRS feed

  • 问题

  • c# word问题

    c#导出到word时出现异常,异常为:
    被呼叫方拒绝接收呼叫。 (异常来自 HRESULT:0x80010001 (RPC_E_CALL_REJECTED))  
    红色语句为出现异常的地方,请问该如何解决。
    具体代码如下
    private void creatWord()
            {
                object oDefault = System.Reflection.Missing.Value;
                object oFilePath = "D:\\asp.docx";
                object oWordExit = WdSaveOptions.wdSaveChanges;
                // Word文档导出
                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc";
                Word._Application oWord;
                Word._Document oDoc;
                oWord = new Word.Application();
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                //插入标题 //           
                Word.Paragraph oParaTitle;
                oParaTitle = oDoc.Content.Paragraphs.Add(ref oMissing);
                oParaTitle.Range.Font.Size = 18;
                oParaTitle.Range.Font.Bold = 1;
                oParaTitle.Range.Text = "造林方案设计";
                oParaTitle.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                oParaTitle.Format.SpaceAfter = 18;
                oParaTitle.Range.InsertParagraphAfter();
                //插入空白
                Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                //
                Word.Paragraph oParaDesign;
                oParaDesign = oDoc.Content.Paragraphs.Add(ref oMissing);
                oParaDesign.Range.Font.Bold = 0;
                oParaDesign.Range.Font.Size = 14;
                oParaDesign.Range.Text = "基本方案:";
                oParaDesign.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                oParaDesign.Format.SpaceBefore = 12;
                oParaDesign.Format.SpaceAfter = 6;
                oParaDesign.Format.LeftIndent = 0;
                oParaDesign.Range.InsertParagraphAfter();
                //注意下面语句的顺序,这样才能控制好格式(如果没有新的格式控制语句,则word中自动使用上一行的格式)
                Word.Paragraph oParaDesignContent;
                oParaDesignContent = oDoc.Content.Paragraphs.Add(ref oMissing);
                oParaDesignContent.Range.Font.Size = 10.5f;
                oParaDesignContent.Range.Text = "推荐树种:" ;
                oParaDesignContent.Format.SpaceBefore = 6;
                oParaDesignContent.Format.LeftIndent = 21f;
                oParaDesignContent.Range.Text += "\n推荐培育目标:" ;
                oParaDesignContent.Range.Text += "\n初值密度:";
                oParaDesignContent.Range.Text += "\n株行距:" ;
                oParaDesignContent.Range.InsertParagraphAfter();
                oDoc.SaveAs(ref oFilePath, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault, ref oDefault);
                //关闭WordDoc文档对象
                oDoc.Close(ref oWordExit, ref oDefault, ref oDefault);
                //关闭WordApp组件对象
                oWord.Quit(ref oWordExit, ref oDefault, ref oDefault);
                MessageBox.Show("造林方案导出成功!", "提示");

            }
    2009年5月25日 2:59

答案

  • 在运行里面输入:
    dcomcnfg

    在“服务”里找到这三个服务,都去启动

    Distributed Transaction Coordinator

    Remote Procedure Call (RPC)

    Security Accounts Manager

    如果:Distributed Transaction Coordinator 启动不了
    就先在运行里面输入:
    msdtc -resetlog

    然后在开启:Distributed Transaction Coordinator


    接着关闭服务组件窗体 ,并重新打开,这个时候每个组建就有属性了
    然后:在命令行中输入:dcomcnfg,会显示出“组件服务”管理器
    2、打开“组件服务->计算机->我的电脑->DCOM 配置”,找到“Microsoft Word文档”,单击右键,选择“属性”
    在“属性”对话框中单击“标识”选项卡,选择“交互式用户””,关闭“组件服务”管理器。

    http://feiyun0112.cnblogs.com/
    2009年5月25日 3:23
    版主