Reputation: 422
I get the error the remote procedure call failed HRESULT: 0x800706BE exception when running Microsoft.Interop.Word from a controller in my ASP .NET MVC application. The strange thing is that if I debug the application and run it step by step, no exception is thrown. I haven't been able to understand the few documentation I've found on microsoft about this exception. I read is has to do with Word closing the sockets when it's called, but I'm at a loss to understand anything about it, so I hope someone may know what the problem may be. The exception is always thrown at a specific line of my code (I've commented where it fails):
//
// 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 });
}
Please help, I haven't worked with Interop before so I really can't guess what's wrong, I thought maybe the two for cycles may cause the problem, but I really have no idea.
Upvotes: 0
Views: 848
Reputation: 49395
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. You can read more about that in the Considerations for server-side Automation of Office article.
Consider using Open XML SDK, see Welcome to the Open XML SDK 2.5 for Office for more information. Also you may find third-party components that are designed for the server-side execution.
Upvotes: 2