The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Annoying COM Interop Problem

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Peter G Provost

Posts: 849
Nickname: pprovost
Registered: Aug, 2003

Peter G Provost is a Solution Architect for Interlink Group in Denver, CO.
Annoying COM Interop Problem Posted: Aug 29, 2003 8:36 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter G Provost.
Original Post: Annoying COM Interop Problem
Feed Title: Peter Provost's Geek Noise
Feed URL: /error.aspx?aspxerrorpath=/Rss.aspx
Feed Description: Technology news, development articles, Microsoft .NET, and other stuff...
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter G Provost
Latest Posts From Peter Provost's Geek Noise

Advertisement

Yesterday I discovered something annoying about COM Interop from C# (I don't know if this applies to VB.NET or not).

I was working my way through one of the Office 2K3 Smart Documents tutorials, which was written for VB6, when I ran across this line on VB code:

Dim strText As String
strText = objDoc.SmartTags.Item _
    (Index:=SMARTDOC).SmartTagActions.Item _
    (Index:=SMARTDOC & _
    SMARTDOC_TEXTBOX).TextboxText

Now, I assumed that I would be able to type the following C# code:

string text = TheDocument.SmartTags[SMARTDOC].SmartTagActions[SMARTDOC + SMARTDOC_TEXTBOX].TextboxText;

The most interesting thing is that the VS.NET Intellisense actually liked it when I typed it that way. It kept prompting me with the members lists as I worked my way through the object model.

But when I compiled it, I got a nasty error:

error CS1546: Property, indexer, or event 'this' is not supported by the language; try directly calling accessor method 'Microsoft.Office.Interop.Word.SmartTags.get_Item(ref object)'

Bummer. I popped over to the VS.NET object browser and it looked alright, but no dice. After digging around on Google Groups, I saw somebody mention that some VB-focused COM code will expect a ByRef Variant, hence the 'ref object' in the error message.

So the only way to fix that is to pass a ref object to the actual getter method, which isn't the array indexer but is a method called get_Item. Here is the code I ended up with:

object temp = SMARTDOC;
string text = ThisDocument.SmartTags.get_Item(ref temp).SmartTagActions.get_Item(ref temp).TextboxText;

The get_Item method isn't shown in the object browser, but it is really there so you can call it. Crazy, eh? But it works.

If you ever see that "not supported by the language" error, remember this.

Read: Annoying COM Interop Problem

Topic: RSS Bandit 1.1.0.29 now with commentRss Previous Topic   Next Topic Topic: Does wireless on Windows really suck?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use