Retrieving Comment.Text using ole automation

M

Marek

Hi
I'm using C# to late bind to Excel and all is working well except when I
attempt to get the text associated with a comment (range.Comment.Text in VBA
terms).

The code looks like this

object objComment = ExcelWrapper.GetProperty(objRange, "Comment");

if (objComment != null)
{
try
{
object objText = ExcelWrapper.GetProperty(objComment,
"Text");

where GetProperty is implemented as follows:

public static object GetProperty(object targetObject, string
propertyName)
{
return targetObject.GetType().InvokeMember(propertyName,
BindingFlags.GetProperty, null, targetObject, null);
}

The problem is that when the Text property of the Comment object is
retrieved, the call returns with DISP_E_UNKNOWNNAME. I've tried to invoke a
get_Text method and that yields similar results.

Please can anyone help?

Best regards

Marek
 
J

Jialiang Ge [MSFT]

Hello Marek,

From your post, my understanding on this issue is: you want to know how to
get the comment text of a Excel Range with late binding. If I'm off base,
please feel free to let me know.

According to the MSDN:
http://msdn2.microsoft.com/de-de/library/microsoft.office.interop.excel.comm
ent.text(VS.80).aspx, the Text of comment object is a method, instead of a
property:
Object Text(Object arg1, Object arg2, Object arg3);
When arg1 is not Type.Missing, the call of comment.Text is used to assign
the text (arg1) to the comment object. If arg1 == Type.Missing, it will
return the comment's Text. Therefore, we should use
BindingFlags.InvokeMethod. The correct call of Text in late binding should
be:

Object objText = Program.InvokeMethod(objComment, "Text"); // because we
set all the params as Type.Missing, these values are optional in late
binding.
Console.WriteLine(objTest.ToString());

static object InvokeMethod(object obj, string sMethod)
{
return obj.GetType().InvokeMember(sMethod,
BindingFlags.InvokeMethod, null, obj, null);
}

Please try the codes above and let me know the result. If you have any
other concern or need anything else, please feel free to let me know.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Marek

Hi Jialiang Ge
Thanks very much for the post - it has solved my problem. Just one point to
note is that the link you sent me ended up at page not found.

Thanks again.

Marek
 
J

Jialiang Ge [MSFT]

Hello Marek,

I am glad to see your problem is resolved.

I check the MSDN link again. It seems that the newsgroup system
automatically wrap the long URL string into two:
"http://msdn2.microsoft.com/de-de/library/microsoft.office.interop.excel.com
m" and "ent.text(VS.80).aspx". The page refers to Excel Primary Interop
Assembly reference of Comment.Text method. Please try it again, and sorry
for the inconveniences.

If you any other concern or need anything else, please feel free to let me
know.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top