Search

Being a web developer for most of my career, I haven’t had much of an opportunity to use InterOp (i.e. API calls to Microsoft Office to provide functionality to another app). But as fate would have it, I’ve been working on a WinForms app as part of a project I’m on now, and I’ve chosen to use some InterOp features instead of a) reinventing the wheel, or b) using some other 3rd party component. There are some drawbacks to using InterOp, mainly that you have to have Office installed on any computer that the software is deployed to. The other main drawback is that you need to use COM for InterOp, which means the APIs can be very confusing, inconsistent, obfuscated, etc.

So I’ve decided to share what I’ve learn. I’ve done 3 major InterOp-dependent features: a spell checker, an HTML RTE, and an Outlook email sender. Here’s the code for the spell checker:

I hope the code I’ve written makes it easier to understand than just plain InterOp calls, but here are some notes that might help:

1. Missing.Value. If you use InterOp, get used to this (unless you are using .NET 4). COM APIs were intended for languages that have optional parameter features (like VB). C# did not get that until the .NET 4 release. So, in C#, you have to put something down for each parameter.

2. I hope you don’t mind my use of KeyValuePair. Again, if this were .NET 4, I’d probably use Tuple. I didn’t really want to create a whole other class just for a private method, so I used KeyValuePair, even though I totally ignore the semantics of ‘key’ and ‘value’. It’s basically just a lazy way to have two return values. Feel free to adjust if it bothers you.

3. This code actually opens up Microsoft Word, puts text into a new Word document, runs spell checker on it, returns the updated text, and then closes Word. The user won’t see the Word document, just the spell checker window, but be aware that Word is still being opened! If you don’t close the Word document, then Word stays loaded, which could cause problems for normal use of Word later.

So, InterOp can seem a little strange, but often it can provide value to an application very quickly. A spell checker feature without InterOp would basically mean using Aspell to do the checking, and probably writing your own spell-checking UI.

5 Responses to “Know your InterOp 1: Spell checking”

  • David says:

    Oh no please don’t shutter Aggreget!

    It’s my second favorite bookmark, Memorandum the first.

    What a shame, I really enjoyed your Aggreget, always something new and interesting.

    Dave

  • mgroves says:

    I don’t plan to shut it, but it is broken right now for some reason, and I just haven’t got around to fixing it.

  • Ozzy_98 says:

    Wow, Boo does COM programming now? How come you’ve never shown me your instances?

  • mgroves says:

    You have to get me really drunk before I’ll show you my instances.

  • Ozzy_98 says:

    Aww, you should use DCOM then. That way you can be like me, and share your interfaces with anything! Of course, that’s how you can get viruses and trojans and bugs. Oh and people probing you for back doors.

Leave a Reply