I recently received a question about doing some Word automation using .NET, and I saw an interesting little difference between the C# and VB.NET calling into the Interop Assembly for certain properties/methods…

If I take VBA or VB6 code that works, and convert it to the almost identical VB.NET code … (after adding a reference to Word in my VB.NET project);

Dim wordApp As New Word.Application() Dim newDoc As Word.Document = wordApp.Documents.Add newDoc.Range.Text = “Test” newDoc.AttachedTemplate = “C:.…\Macmillan.dot” wordApp.Visible = True

It works fine, but in C# I get an error setting the AttachedTemplate property;

error CS1545: Property, indexer, or event ‘AttachedTemplate’ is not supported by the language; try directly calling accessor methods ‘Word._Document.get_AttachedTemplate()’ or ‘Word._Document.set_AttachedTemplate(ref object)’

object missing = System.Reflection.Missing.Value;Word.ApplicationClass wordApp = new Word.ApplicationClass(); Word.Document newDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); newDoc.Range(ref missing,ref missing).Text = “Test”; newDoc.AttachedTemplate = @“C:.…\Macmillan.dot”; wordApp.Visible = true; I was able to make it work by writing the code like this;

object missing = System.Reflection.Missing.Value; Word.ApplicationClass wordApp = new Word.ApplicationClass(); Word.Document newDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); newDoc.Range(ref missing,ref missing).Text = “Test”; object templateName = (object)@“C:.…\Macmillan.dot”; newDoc.set_AttachedTemplate(ref templateName); wordApp.Visible = true; I was interested in finding out more about this error so I asked around internally and had it explained to me quite quickly. Looking into the type-library for Word and then the IL of the Interop Assembly would have likely provided the answer as well, but I’m glad I didn’t have to get into that. I’ll try to pass the explanation along without mangling it too much in the translation (feel free to correct me if you can, or add additional details). Some properties of COM libraries are actually methods that support one or parameters, which is cool with VBA/VB6 as they supported this type of property as well, but they are translated (correctly it seems) by TlbImp.exe as methods (set_AttachedTemplate, get_AttachedTemplate)… VB.NET does some additional work for you so that you can still code against these property/methods as properties, but in C# you have to use them as methods. Interesting stuff, and likely a bit of a gotcha for people trying to move VBA code into .NET.

[Listening to: In the Air Tonight – [Phil Collins](http://www.windowsmedia.com/mg/search.asp?srch=Phil+Collins) – Miami Vice (05:29)]