0

I have a C# Web API and Web Application that is an all-in-one program. This project is going to be hosted on my company's intranet. I'm attempting to setup a mail merge process within this Web API using Microsoft.Office.Interop.Word. It works just fine when debugging. The mail merge template file is hosted at C:\inetpub\wwwroot\MailMergeTemplate\Template.docx.

However, this stops working as soon as I deploy the website to IIS. Every time I attempt to run the merge I'm met with a HTTP 500 Internal Server Error. I tried to move the template to a mapped network drive but that didn't work. I also made a different web directory for the template via IIS and put the file in there. I could browse to the folder via Chrome just fine and could see and download the template. But this didn't work either.

How should I host a mail merge template so the Web Application can see it and access it? Is this even possible?

Rhendar
  • 390
  • 3
  • 25
  • This is not a good idea - from [Considerations for server-side Automation of Office](https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office) _"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."_ – stuartd Mar 08 '19 at 14:46
  • Even if it's just a web application that maybe 5 people are going to use? What's the best way to perform a mail merge then? Not using a web application? – Rhendar Mar 08 '19 at 14:51
  • Well, they don't say you _can't_ do it, just that it might not be reliable.. A 500 error means your app is throwing an exception, setting up an [unhandled exception handler](https://stackoverflow.com/questions/16028919/catch-all-unhandled-exceptions-in-asp-net-web-api) should enable you to see what's going wrong. – stuartd Mar 08 '19 at 14:53
  • Once that's enabled where are the error logs? – Rhendar Mar 08 '19 at 15:13
  • .. wherever you tell it to put them. Do you not have any kind of logging enabled? – stuartd Mar 08 '19 at 15:29
  • I've never done logging before. I followed the link you sent me and added that code in. Does that actually do the logging? – Rhendar Mar 08 '19 at 15:30
  • That just logs to System.Diagnostics.Trace, you'd need to add a [trace listener to your config](https://stackoverflow.com/questions/3113650/diagnostic-trace-logging-not-working-when-deployed-to-iis-7) to your config to write them to a file. Your other options are a) use a logging framework like NLog, log4net etc and get that to save the logs to a file or b) just do something quick and dirty like `File.AppendAllText(path, context.ExceptionContext.Exception.ToString())` in the exception handler. Setting up logging is a bit of a pain but it's a useful skill and in case something goes wrong later. – stuartd Mar 08 '19 at 15:44
  • I added that but nothing happened. Here's a screenshot of the code: https://i.imgur.com/e0zVWuN.png – Rhendar Mar 08 '19 at 16:00
  • Have you tried running it in IIS locally? – stuartd Mar 08 '19 at 16:20
  • By locally do you mean on the same workstation that I'm debugging on? – Rhendar Mar 08 '19 at 16:22
  • Yes, but in IIS rather than IIS Express – stuartd Mar 08 '19 at 16:22
  • Same thing. HTTP 500 but the log file isn't updated. :( – Rhendar Mar 08 '19 at 17:05
  • Ah, but now you can debug it!! – stuartd Mar 08 '19 at 17:10

0 Answers0