Thursday, December 10, 2015

Word.Application.Documents.Open returns a Null



Okay, so you have embarked on a project that requires dynamically generating Microsoft Office Word documents based on a template Word document. 

Naturally you have already added a Reference to the "Microsoft Word Object Library" to your project, by right-clicking on your project, Adding Reference / COM / Type Libraries / Microsoft Word Object Library.
I have the 15.0 version of the Word object library for my project.

You have written the appropriate C# server-side code to open the document, by using the "Documents.Open" method of the Word.Application object.


 
Everything works in your local development environment.  Hurray for you!
But hold on. 

While everything works in your machine, it fails in the web server you deployed your application to.
You get the error message:
    An exception of type 'System.NullReferenceException' occurred but was not handled in user code.
    Additional information: Object reference not set to an instance of an object.

How can that be?

The answer is that Word failed to open the document because you did not have the proper permissions to do so.
Or rather, the user account which tried to open the Word document did not have appropriate access rights.

Below is the unintuitive solution for you.
Open the Component Services module from the Start Menu, or search for it.
You can manually start it from "C:\Windows\System32\comexp.msc"


In the Component Services window, expand the nodes to Console Root / Component Services / Computers / My Computer / DCOM Config


Under DCOM Config, scroll down to find "Microsoft Word 97 - 2003 Document" or its equivalent in your server.


Right-click on "Microsoft Word 97 - 2003 Document", select Properties / Identity.


In the Identity tab, change the option control selection from "The launching user" to "The interactive user".

Hit OK and Exit. 

And voila! 
Your frustrating error goes away, and your application magically works as you expected in your web server.



Monday, August 31, 2015

The type or namespace name 'Twilio' could not be found


So, you have finally joined the exciting new world of ASP.NET 5. 
Congratulations!  And welcome!

You may be running Visual Studio 2015 - or maybe Visual Studio Code - and creating new DNX projects and ASP.NET 5 applications that target the latest .NET Framework 4.6 or .NET Core 5, running on DNX, and compiling with RyuJIT or the Roslyn platform.
Good for you!

So, let's say that you writing a DNX project that makes use of the Twilio SMS API Client.
If you're reading this blog, then you probably already know that Twilio SMS is an API that lets you programmatically send, receive and track text messages globally.

Your code, in simple terms, will look like the one below


Your logic looks solid. 
You instantiate your Twilio client with your Account Sid and Auth Token, and you invoke the SendMessage service by passing in the appropriate parameters.

So why then, does your build fail and the Error List pane show the error below?


The type or namespace name 'Twilio' could not be found (are you missing a using directive or an assembly reference?)

How can this be? 
You already have the correct Using statement.
And you have already installed the Twilio package through NuGet.

Well, what's happening here can be explained by looking at your "project.json" file.


Here, we see that both the "dnx451" (.Net Framework) and "dnxcore50" (.Net Core 5) are being targeted.
However, .Net Core 5 does NOT support the Twilio SDK.  Ergo the "type or namespace" error.

There are a couple of ways to resolve this issue.
The first is to simply remove the "dnxcore50" from the list of targeted frameworks in the project.json file.


This is a simple and straightforward solution.
However, it does exclude the .NET Core 5 framework from your project altogether, and that may not be your intent.

The second alternative is something of a "shim". 
Using a compiler directive, you can exclude any code that "dnxcore50" does not support.



In this way, you are still targeting the .NET Core 5 framework, but at the same time,
your app does not break on any code that the framework does not support.

Happy .Net coding!