Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ASP MVC控制器运行Word时,继续获取InteropServices.COMException_C#_Asp.net Mvc_Asp.net Mvc 4_Com_Office Interop - Fatal编程技术网

C# 从ASP MVC控制器运行Word时,继续获取InteropServices.COMException

C# 从ASP MVC控制器运行Word时,继续获取InteropServices.COMException,c#,asp.net-mvc,asp.net-mvc-4,com,office-interop,C#,Asp.net Mvc,Asp.net Mvc 4,Com,Office Interop,从ASP.NET MVC应用程序中的控制器运行Microsoft.Interop.Word时,我收到远程过程调用失败的错误HRESULT:0x800706BE异常。奇怪的是,如果我调试应用程序并一步一步地运行它,就不会抛出异常。我无法理解在microsoft上找到的有关此异常的少数文档。我读到的是当它被调用时,与关闭套接字有关的单词,但我不知道它的任何内容,所以我希望有人知道可能是什么问题。异常总是在我的代码的特定行抛出(我已经在失败的地方进行了注释): // //获取/Asistencia/P

从ASP.NET MVC应用程序中的控制器运行Microsoft.Interop.Word时,我收到远程过程调用失败的错误HRESULT:0x800706BE异常。奇怪的是,如果我调试应用程序并一步一步地运行它,就不会抛出异常。我无法理解在microsoft上找到的有关此异常的少数文档。我读到的是当它被调用时,与关闭套接字有关的单词,但我不知道它的任何内容,所以我希望有人知道可能是什么问题。异常总是在我的代码的特定行抛出(我已经在失败的地方进行了注释):

//
//获取/Asistencia/PrintDocentes
公共行动结果打印文档(int IdCurso)
{
List materials=new-Metodos.Entidades().getListadoProfesores(IdCurso);
List comparitidas=新列表();
compartidas=materials.GroupBy(t=>t.idmaterialscursos)。其中(t=>t.Count()>1)。选择(t=>t.Key).ToList();
List catedrasCompartidas=新列表();
catedrasCompartidas=materials.Where(s=>compartidas.Any(t=>t.Equals(s.idmaterialascursos))).Select(r=>r.NombresMaestro.ToList();
string document=“asistenciaDocentes.docx”;
string uploadsFolder=HostingEnvironment.MapPath(“~/App\u Data/Docs”);
字符串imageFolder=HostingEnvironment.MapPath(“~/Images/Docs”);
字符串[]encabezado={“logocideasitencia.jpg”、“logosegobasitencia.jpg”、“logoPFAsistencia.jpg”、“logocideassistencia.jpg”};
字符串archivo=Path.Combine(uploadsFolder,@document);
ApWord=新单词.Application();
docAsistencia=ApWord.Documents.Add(ref-opc,ref-opc,ref-opc,ref-opc);
ApWord.Visible=true;
docAsistencia.PageSetup.RightMargin=(float)100;
docAsistencia.PageSetup.LeftMargin=(float)60;
foreach(ApWord.ActiveDocument.Sections中的Word.Section部分)
{
Word.Range headerRange=section.Headers[Word.wdheaderfooteridex.wdheaderfooterprisary].Range;
for(int i=0;it.idmaterialascursos.Equals(item.idmaterialascursos)).ToList();
List catedras=comparitir.Select(t=>t.NombresMaestro.ToList();
List catedras_aImprimir=catedras.Where(t=>!t.Equals(item.NombresMaestro)).ToList();
foreach(catedras_aImprimir中的字符串catedra)
{
Word.paragration grupo8=docAsistencia.paragrations.Add();
grupo8.射程插入器(catedra);
}
docAsistencia.Words.Last.InsertBreak(Word.WdBreakType.wdLineBreak);
}
Word.paragration grupo9=docAsistencia.paragrations.Add();
grupo9.Range.InsertAfter(“Módulo”);
Word.paragration grupo10=docAsistencia.paragrations.Add();
grupo10.范围.插入符(项.模);
Word.paragration grupo11=docAsistencia.paragrations.Add();
grupo11.范围插入符(“材料”);
Word.paragration grupo12=docAsistencia.paragrations.Add();
grupo12.范围插入符(项目材料);
//
    // GET /Asistencia/PrintDocentes
    public ActionResult PrintDocentes(int IdCurso)
    {
        List<AsistenciaDocentesViewModel> materias = new Metodos.Entidades().getListadoProfesores(IdCurso);
        List<int> compartidas = new List<int>();
        compartidas = materias.GroupBy(t => t.IdMateriasCursos).Where(t => t.Count() > 1).Select(t => t.Key).ToList();
        List<string> catedrasCompartidas = new List<string>();
        catedrasCompartidas= materias.Where(s => compartidas.Any(t=> t.Equals(s.IdMateriasCursos))).Select(r => r.NombresMaestro).ToList();

        string document = "asistenciaDocentes.docx";
        string uploadsFolder = HostingEnvironment.MapPath("~/App_Data/Docs");
        string imageFolder = HostingEnvironment.MapPath("~/Images/Docs");
        string[] encabezado = { "logoCideAsistencia.jpg", "logoSegobAsistencia.jpg", "logoPFAsistencia.jpg", "logoDiplomadoAsistencia.jpg" };
        string archivo = Path.Combine(uploadsFolder, @document);
        ApWord = new Word.Application();
        docAsistencia = ApWord.Documents.Add(ref opc, ref opc, ref opc, ref opc);
        ApWord.Visible = true;
        docAsistencia.PageSetup.RightMargin = (float)100;
        docAsistencia.PageSetup.LeftMargin = (float)60;
        foreach (Word.Section section in ApWord.ActiveDocument.Sections)
        {
            Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
            for (int i = 0; i < encabezado.Length; i++)
            {
                headerRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);
                string path = Path.Combine(imageFolder, encabezado[i]);
                Word.InlineShape map = headerRange.InlineShapes.AddPicture(path);
                if (i == 0) { map.Height = 48; map.Width = 39; } /*********
*********************This is the line that throws the exception ********/
                if (i == 1) { map.Width = 263; map.Height = 39; }
                if (i == 2) { map.Width = 141; map.Height = 26; }
                headerRange.Move(Word.WdUnits.wdCharacter);
                headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                if (i == 2) break;

            }

            Word.Shape titulo = docAsistencia.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,60, 5, 300, 50, ref opc);
            titulo.TextFrame.TextRange.Text = "DIPLOMADO EN MANDO POLICIAL 2015\nLista de Asistencia de profesores\n";
            titulo.Line.Visible = MsoTriState.msoFalse;
            titulo.TextFrame.TextRange.Font.Name = "Cambria";
            titulo.TextFrame.TextRange.Font.Size = 16;
            titulo.TextFrame.TextRange.Font.Bold = 1;
            titulo.TextFrame.TextRange.Font.Italic = 1;


            headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
            Word.InlineShape map2 = headerRange.InlineShapes.AddPicture(Path.Combine(imageFolder, encabezado[3]));
            map2.Height = 106;
            map2.Width = 137;


        }
        foreach(AsistenciaDocentesViewModel item in materias)
        {
        Word.Paragraph grupo1 = docAsistencia.Paragraphs.Add(ref opc);
        grupo1.Range.Font.Name = "Arial";
        grupo1.Range.Font.Size = 14;
        grupo1.Range.Font.Bold = 1;
        grupo1.Range.InsertAfter("Semana\t" + item.Semana);
        Word.Paragraph grupo2 = docAsistencia.Paragraphs.Add();
        grupo2.Range.InsertAfter("Sede\t" + item.Sede);
        Word.Paragraph grupo3 = docAsistencia.Paragraphs.Add();
        grupo3.Range.InsertAfter("Grupo\t" + item.Grupo);
        Word.Paragraph grupo4 = docAsistencia.Paragraphs.Add();
        docAsistencia.Words.Last.InsertBreak(Word.WdBreakType.wdLineBreak);
        grupo4.Range.InsertAfter("Fecha\t" + item.Fecha);
        Word.Paragraph grupo5 = docAsistencia.Paragraphs.Add();
        grupo5.Range.InsertAfter("Día\t" + item.Dia);
        Word.Paragraph grupo6 = docAsistencia.Paragraphs.Add();
        grupo6.Range.InsertAfter("Horario\t" + item.HorarioInicio + " - " + item.HorarioFin);
        Word.Paragraph grupo7 = docAsistencia.Paragraphs.Add();
        grupo7.Range.InsertAfter("compartir");

        if (compartidas.Contains(item.IdMateriasCursos))
        {
            List<AsistenciaDocentesViewModel> compartir = materias.Where(t => t.IdMateriasCursos.Equals(item.IdMateriasCursos)).ToList();
            List<string> catedras = compartir.Select(t => t.NombresMaestro).ToList();
            List<string> catedras_aImprimir = catedras.Where(t => !t.Equals(item.NombresMaestro)).ToList();
            foreach (string catedra in catedras_aImprimir)
            {
                Word.Paragraph grupo8 = docAsistencia.Paragraphs.Add();
                grupo8.Range.InsertAfter(catedra);

            }
            docAsistencia.Words.Last.InsertBreak(Word.WdBreakType.wdLineBreak);
        }
        Word.Paragraph grupo9 = docAsistencia.Paragraphs.Add();
        grupo9.Range.InsertAfter("Módulo");
        Word.Paragraph grupo10 = docAsistencia.Paragraphs.Add(); 
        grupo10.Range.InsertAfter(item.Modulo);
        Word.Paragraph grupo11 = docAsistencia.Paragraphs.Add();
        grupo11.Range.InsertAfter("Materia");
        Word.Paragraph grupo12 = docAsistencia.Paragraphs.Add();
        grupo12.Range.InsertAfter(item.Materia);
        Word.Paragraph grupo13 = docAsistencia.Paragraphs.Add();
        grupo13.Range.InsertAfter("Profesor");
        Word.Paragraph grupo14 = docAsistencia.Paragraphs.Add();
        grupo14.Range.InsertAfter(item.NombresMaestro);
        Word.Paragraph grupo15 = docAsistencia.Paragraphs.Add();
        grupo15.Range.InsertAfter("Firma:______________________________________________________");

        docAsistencia.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak);
        }
        docAsistencia.SaveAs2(archivo, ref opc, ref opc, ref opc, ref opc, ref opc, ref opc, ref opc, ref opc, ref opc, ref opc,
            ref opc, ref opc, ref opc, ref opc, ref opc, ref opc);
        docAsistencia.Close();
        Marshal.FinalReleaseComObject(ApWord);
        Marshal.FinalReleaseComObject(docAsistencia);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();

        return RedirectToAction("VerRecursos", new { file = document });

    }