Word Automation from C# vs. VB.NET


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 – Miami Vice (05:29)]


Comments (10)

  1. Ken Getz says:

    Duncan:

    Just wrote a white paper on this for the Visual Studio Tools for Office group. (That is, it outlines all the issues I could find when coding against the Word and Excel PIAs using C#–and there were quite a few.) I’ll be happy to forward my draft to you if you’d like. Should be up on MSDN RSN. — Ken

  2. Ken Getz says:

    Also, I’d be a little leery of this:

    newDoc.Range(ref missing,ref missing).Text = "Test";

    I originally wrote the code that way, but one of the team members reviewing my code suggested that this is a bad idea, since both parameters are passed by reference, and the internal code could modify the parameter value of one parameter before using the second, thereby causing weird side effects. Of course, that’s totally unlikely to happen, but why risk it? I rewrote all my examples to pass two separate parameters in cases like this (or 15 separate parameters, in cases that required that many <g>) — Ken

  3. Mike Angre says:

    Is this the only way?

    Isn’t there a direct access method to properties in c#?

    Reply to micke@arkpen.se

  4. Larry says:

    Hi:

    I have a quick question in regard of applying a locked template. It seems that after I applied the locked template, all the styles and formatting fields are still unlocked. Is there anyway I can lock thos fields based on the locked template?

  5. Nehal says:

    Hi

    I have a text box in a word document and I want set some values from C# through automation.

    Any suggestions are appreciated

    Mail me your reply at learningdotnet2004@hotmail.com please. as i may not be able to get back on this page.

  6. sivakumar says:

    how do i insert a text in my header/footer in my template

  7. zameer syed says:

    COM object with CLSID {000209FF-0000-0000-C000-000000000046} is either not valid or not registered.

    I keep getting this error.Does anybody know why

    Thanks in advance

  8. carlcamera says:

    This helped a lot, and I never thought of the problem Range(ref missing, ref missing) exposed. Thanks.