c#msoffice office错误0x80010108

时间:2016-02-29 10:31:06

标签: c# ms-word ms-office rpc

此代码在win7 + office 2007上完美运行,但在win8 + office 2013上我得到了 错误0x80010108 RPC_E_DICONNECTED调用的对象已与其客户端断开连接。

任何想法?

大家好吗

此代码在win7 + office 2007上完美运行,但在win8 + office 2013上我得到了 错误0x80010108 RPC_E_DICONNECTED调用的对象已与其客户端断开连接。

任何想法?

    public void PrintDoc()
        {
            try {

                System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
                exportXmlDocument = new XmlDocument();

                WordApp = new Microsoft.Office.Interop.Word.Application();
                //msWord.Document NewMainDoc = null;                    

                object missing = System.Reflection.Missing.Value;                   

                var ni = new NodeInfo();
                ni = (NodeInfo)TvXML.Nodes[0].Tag;
                XmlElement xe = exportXmlDocument.CreateElement(TvXML.Nodes[0].Text);
                xe.SetAttribute("name", ni.Name);
                xe.SetAttribute("type", ni.DocType);
                xe.SetAttribute("path", ni.Path);
                xe.SetAttribute("filename", ni.FileName);    

                exportXmlDocument.AppendChild(xe);

                //riempie l' oggetto xml
                RecursiveToXml(TvXML.Nodes[0], xe);

                WordApp.Application.Visible = true;
                if (GlobalVar.UseTemplate || File.Exists(GlobalVar.TemplateName)) {
                    object oTemplate = GlobalVar.TemplateName;
                    MainDoc = WordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
                } else {
                    MainDoc = WordApp.Documents.Add();
                }

                // stampa
                RecursiveToPrint(exportXmlDocument.ChildNodes);

                MainDoc.ActiveWindow.Activate();
                exportXmlDocument = null;
                System.Windows.Forms.Cursor.Current = Cursors.Default;    

            } catch (XmlException xmlEx) {
                MessageBox.Show(xmlEx.Message);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
        }
        }

void RecursiveToPrint(XmlNodeList nodes)
        {
            foreach (XmlNode childXmlNode in nodes) {
                // 
                var ni = new NodeInfo();

                ni.Name = ((XmlElement)childXmlNode).Name;
                ni.DocType = ((XmlElement)childXmlNode).GetAttribute("type");
                ni.Path = ((XmlElement)childXmlNode).GetAttribute("path");
                ni.FileName = ((XmlElement)childXmlNode).GetAttribute("filename");
                if (ni == null)
                    return;                 

                object missing = System.Reflection.Missing.Value;    

                switch (ni.DocType) {
                    case "doc":
                    case "docx":
                        object objFullFileName = ni.Path + "\\" + ni.FileName;
                        var doc = WordApp.Documents.Open(ref objFullFileName, 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);
                        doc.ActiveWindow.Activate();
                        doc.ActiveWindow.Selection.WholeStory();                            
                        doc.ActiveWindow.Selection.Copy();


                        MainDoc.ActiveWindow.Selection.PasteSpecial();
                        doc.ActiveWindow.Close();


                        if (GlobalVar.PageBreak) {
                            doc.ActiveWindow.Selection.InsertBreak();
                        }

                        break;

                    case "xls":
                    case "xlsx":

                        break;

                    case "xml":

                        break;                          

                }    
                RecursiveToPrint(childXmlNode.ChildNodes);                  
            }
        }

0 个答案:

没有答案