Code/Tea/Etc...

Duncan Mackenzie has too much time on his hands

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)]
Published Sunday, May 11, 2003 12:18 AM by Duncanma

Comments

 

Ken Getz said:

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
May 13, 2003 10:25 AM
 

Ken Getz said:

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
May 13, 2003 10:27 AM
 

Mike Angre said:

Is this the only way?

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

Reply to micke@arkpen.se
November 3, 2003 3:43 AM
 

sfdsfs said:

sdfsds
January 6, 2004 2:19 AM
 

sfdsfs said:

dsfs
January 6, 2004 2:19 AM
 

Larry said:

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?
March 8, 2004 10:00 PM
 

Nehal said:

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.
April 29, 2004 5:53 AM
 

sivakumar said:


how do i insert a text in my header/footer in my template
July 2, 2004 5:34 AM
 

zameer syed said:

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
July 12, 2004 2:24 PM
 

carlcamera said:

This helped a lot, and I never thought of the problem Range(ref missing, ref missing) exposed. Thanks.
August 18, 2004 10:21 AM
Anonymous comments are disabled

This Blog

Syndication

News

This blog has moved to my own VB site

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker