473,651 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BuiltinProperti es DirectCast Fails

I get the following failure when trying to access the builtin properties;

An unhandled exception of type 'System.Invalid CastException' occurred in
resultsoutput.d ll
Additional information: Specified cast is not valid.

In general terms the code is constructed as follows;

ModuleA
Public xlWb As Excel.Workbook

ModuleB
SubA
Dim prps As Microsoft.Offic e.Core.Document Properties
prps = DirectCast(xlWb .BuiltinDocumen tProperties,
Microsoft.Offic e.Core.Document Properties)

This is using;
Framework 1.1
Excel 2002 SP3
Reference to COM Microsoft Excel 10.0 Object Library (Version 1.4)

I've been following the article by Ken Getz
(http://msdn.microsoft.com/library/de...-us/odc_vsto20
03_ta/html/ExcelObj.asp) which suggests the code should work. Ken's article
refers to Excel 2003, but all the objects he references are also in the
Microsoft Excel 10 COM object. I wondered whether this was a bug in the 2002
COM or I've missed something in my code.

Nov 20 '05 #1
6 2127
Try CType instead of DirectCast.
In this case, you're dealing with COM wrappers, and I have the nagging
feeling you're using a newer interface and trying to cast directly to an
older one. CType should call QueryInterface under the hood, so that if the
interface exists, it will get you a proper reference.

Hope that helps,

-Rob Teixeira [MVP]

"Mark Nethercott" <Ma************ *@iFabre.com> wrote in message
news:up******** *****@TK2MSFTNG P09.phx.gbl...
I get the following failure when trying to access the builtin properties;

An unhandled exception of type 'System.Invalid CastException' occurred in
resultsoutput.d ll
Additional information: Specified cast is not valid.

In general terms the code is constructed as follows;

ModuleA
Public xlWb As Excel.Workbook

ModuleB
SubA
Dim prps As Microsoft.Offic e.Core.Document Properties
prps = DirectCast(xlWb .BuiltinDocumen tProperties,
Microsoft.Offic e.Core.Document Properties)

This is using;
Framework 1.1
Excel 2002 SP3
Reference to COM Microsoft Excel 10.0 Object Library (Version 1.4)

I've been following the article by Ken Getz
(http://msdn.microsoft.com/library/de...-us/odc_vsto20 03_ta/html/ExcelObj.asp) which suggests the code should work. Ken's article refers to Excel 2003, but all the objects he references are also in the
Microsoft Excel 10 COM object. I wondered whether this was a bug in the 2002 COM or I've missed something in my code.

Nov 20 '05 #2
Rob,
DirectCast will call QueryInterface to find the COM interface!

I will be very curious if CType works, where DirectCast fails.

Hope this helps
Jay

"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Try CType instead of DirectCast.
In this case, you're dealing with COM wrappers, and I have the nagging
feeling you're using a newer interface and trying to cast directly to an
older one. CType should call QueryInterface under the hood, so that if the
interface exists, it will get you a proper reference.

Hope that helps,

-Rob Teixeira [MVP]

"Mark Nethercott" <Ma************ *@iFabre.com> wrote in message
news:up******** *****@TK2MSFTNG P09.phx.gbl...
I get the following failure when trying to access the builtin properties;
An unhandled exception of type 'System.Invalid CastException' occurred in
resultsoutput.d ll
Additional information: Specified cast is not valid.

In general terms the code is constructed as follows;

ModuleA
Public xlWb As Excel.Workbook

ModuleB
SubA
Dim prps As Microsoft.Offic e.Core.Document Properties
prps = DirectCast(xlWb .BuiltinDocumen tProperties,
Microsoft.Offic e.Core.Document Properties)

This is using;
Framework 1.1
Excel 2002 SP3
Reference to COM Microsoft Excel 10.0 Object Library (Version 1.4)

I've been following the article by Ken Getz

(http://msdn.microsoft.com/library/de...-us/odc_vsto20
03_ta/html/ExcelObj.asp) which suggests the code should work. Ken's

article
refers to Excel 2003, but all the objects he references are also in the
Microsoft Excel 10 COM object. I wondered whether this was a bug in the

2002
COM or I've missed something in my code.


Nov 20 '05 #3
Thanks for the suggestions. I'd already tried CType as an alternative, but
that fails as well.

Ken Getz's article is pretty clear on working with the DirectCast - I'll try
the code with the 2003 COM object

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e7******** ******@TK2MSFTN GP09.phx.gbl...
Rob,
DirectCast will call QueryInterface to find the COM interface!

I will be very curious if CType works, where DirectCast fails.

Hope this helps
Jay

"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Try CType instead of DirectCast.
In this case, you're dealing with COM wrappers, and I have the nagging
feeling you're using a newer interface and trying to cast directly to an
older one. CType should call QueryInterface under the hood, so that if the
interface exists, it will get you a proper reference.

Hope that helps,

-Rob Teixeira [MVP]

"Mark Nethercott" <Ma************ *@iFabre.com> wrote in message
news:up******** *****@TK2MSFTNG P09.phx.gbl...
I get the following failure when trying to access the builtin properties;
An unhandled exception of type 'System.Invalid CastException' occurred in resultsoutput.d ll
Additional information: Specified cast is not valid.

In general terms the code is constructed as follows;

ModuleA
Public xlWb As Excel.Workbook

ModuleB
SubA
Dim prps As Microsoft.Offic e.Core.Document Properties
prps = DirectCast(xlWb .BuiltinDocumen tProperties,
Microsoft.Offic e.Core.Document Properties)

This is using;
Framework 1.1
Excel 2002 SP3
Reference to COM Microsoft Excel 10.0 Object Library (Version 1.4)

I've been following the article by Ken Getz

(http://msdn.microsoft.com/library/de...-us/odc_vsto20
03_ta/html/ExcelObj.asp) which suggests the code should work. Ken's

article
refers to Excel 2003, but all the objects he references are also in the Microsoft Excel 10 COM object. I wondered whether this was a bug in

the 2002
COM or I've missed something in my code.



Nov 20 '05 #4
Yes, you're absolutely right. That's what I get for posting while
multi-tasking *sigh*

OK, this seems like a similar issue to a co-worker had.
Try this Mark - Use reflection on the object reference you do have to get
all implemented interfaces.
Also use Reflection to get the GuidAttribute from each (including the
GuidAttribute on the object ref's class).

Next, check to see that these are in fact the registered interfaces for
Excel in the system registry (based on the GUIDs).
I believe the problem may in fact be that you are trying to cast to a newer
version of the interface, which an older version of Excel might not provide
(or another such scenario). It would also help to know what version of the
PIAs you are using vs. what version of Office (and which PIAs have you
installed on that system).

-Rob Teixeira [MVP]

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e7******** ******@TK2MSFTN GP09.phx.gbl...
Rob,
DirectCast will call QueryInterface to find the COM interface!

I will be very curious if CType works, where DirectCast fails.

Hope this helps
Jay

"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Try CType instead of DirectCast.
In this case, you're dealing with COM wrappers, and I have the nagging
feeling you're using a newer interface and trying to cast directly to an
older one. CType should call QueryInterface under the hood, so that if the
interface exists, it will get you a proper reference.

Hope that helps,

-Rob Teixeira [MVP]

"Mark Nethercott" <Ma************ *@iFabre.com> wrote in message
news:up******** *****@TK2MSFTNG P09.phx.gbl...
I get the following failure when trying to access the builtin properties;
An unhandled exception of type 'System.Invalid CastException' occurred in resultsoutput.d ll
Additional information: Specified cast is not valid.

In general terms the code is constructed as follows;

ModuleA
Public xlWb As Excel.Workbook

ModuleB
SubA
Dim prps As Microsoft.Offic e.Core.Document Properties
prps = DirectCast(xlWb .BuiltinDocumen tProperties,
Microsoft.Offic e.Core.Document Properties)

This is using;
Framework 1.1
Excel 2002 SP3
Reference to COM Microsoft Excel 10.0 Object Library (Version 1.4)

I've been following the article by Ken Getz

(http://msdn.microsoft.com/library/de...-us/odc_vsto20
03_ta/html/ExcelObj.asp) which suggests the code should work. Ken's

article
refers to Excel 2003, but all the objects he references are also in the Microsoft Excel 10 COM object. I wondered whether this was a bug in

the 2002
COM or I've missed something in my code.



Nov 20 '05 #5
Yes, you're absolutely right. That's what I get for posting while
multi-tasking *sigh*

OK, this seems like a similar issue to a co-worker had.
Try this Mark - Use reflection on the object reference you do have to get
all implemented interfaces.
Also use Reflection to get the GuidAttribute from each (including the
GuidAttribute on the object ref's class).

Next, check to see that these are in fact the registered interfaces for
Excel in the system registry (based on the GUIDs).
I believe the problem may in fact be that you are trying to cast to a newer
version of the interface, which an older version of Excel might not provide
(or another such scenario). It would also help to know what version of the
PIAs you are using vs. what version of Office (and which PIAs have you
installed on that system).

-Rob Teixeira [MVP]

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e7******** ******@TK2MSFTN GP09.phx.gbl...
Rob,
DirectCast will call QueryInterface to find the COM interface!

I will be very curious if CType works, where DirectCast fails.

Hope this helps
Jay

"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Try CType instead of DirectCast.
In this case, you're dealing with COM wrappers, and I have the nagging
feeling you're using a newer interface and trying to cast directly to an
older one. CType should call QueryInterface under the hood, so that if the
interface exists, it will get you a proper reference.

Hope that helps,

-Rob Teixeira [MVP]

"Mark Nethercott" <Ma************ *@iFabre.com> wrote in message
news:up******** *****@TK2MSFTNG P09.phx.gbl...
I get the following failure when trying to access the builtin properties;
An unhandled exception of type 'System.Invalid CastException' occurred in resultsoutput.d ll
Additional information: Specified cast is not valid.

In general terms the code is constructed as follows;

ModuleA
Public xlWb As Excel.Workbook

ModuleB
SubA
Dim prps As Microsoft.Offic e.Core.Document Properties
prps = DirectCast(xlWb .BuiltinDocumen tProperties,
Microsoft.Offic e.Core.Document Properties)

This is using;
Framework 1.1
Excel 2002 SP3
Reference to COM Microsoft Excel 10.0 Object Library (Version 1.4)

I've been following the article by Ken Getz

(http://msdn.microsoft.com/library/de...-us/odc_vsto20
03_ta/html/ExcelObj.asp) which suggests the code should work. Ken's

article
refers to Excel 2003, but all the objects he references are also in the Microsoft Excel 10 COM object. I wondered whether this was a bug in

the 2002
COM or I've missed something in my code.



Nov 20 '05 #6
Thanks Rob for the input.

You were right, in the end I had to use reflection to contort my way through
the COM interface.
It took a while to figure out & I relied heavily on the help of Tom Annetts
@ Experts-Exchange.

The full thread can be found at;

http://www.experts-exchange.com/Prog..._20973995.html

I've included the final function here for completeness. I've only tested it
under XP SP3 & Excel 2002, where it works fine;

Sub SetProperty(ByV al xlWB As Excel.Workbook, ByVal PName As String, ByVal
PValue As Object, _
ByVal isPropCustom As Boolean)

'This function sets the value of a property.
'
'The parameters passed to the function are as follows:
'
'xlWB A reference to the the workbook whose property is to
be set.
'PName A string containing the name of the property
'PValue A variant containing the value of the property
'isPropCustom A boolean indicating whether the property is a
Custom Document Property.

Dim result As Object
Dim PType As Long
Dim isLinkToContent As Boolean = False
Dim LinkSource As Object = Nothing

Select Case VarType(PValue)
Case vbBoolean
PType =
Microsoft.Offic e.Core.MsoDocPr operties.msoPro pertyTypeBoolea n
Case vbDate
PType = Microsoft.Offic e.Core.MsoDocPr operties.msoPro pertyTypeDate
Case vbDouble, vbLong, vbSingle, vbCurrency
PType = Microsoft.Offic e.Core.MsoDocPr operties.msoPro pertyTypeFloat
Case vbInteger
PType = Microsoft.Offic e.Core.MsoDocPr operties.msoPro pertyTypeNumber
Case vbString
PType = Microsoft.Offic e.Core.MsoDocPr operties.msoPro pertyTypeString
Case Else
PType = Microsoft.Offic e.Core.MsoDocPr operties.msoPro pertyTypeString
End Select

If isPropCustom Then
xlWB.CustomDocu mentProperties. GetType().Invok eMember("Add",
BindingFlags.In vokeMethod, _
Nothing, xlWB.CustomDocu mentProperties, New Object() {PName,
isLinkToContent , PType, PValue, LinkSource})
Else
result = xlWB.BuiltinDoc umentProperties .GetType().Invo keMember("Item" ,
BindingFlags.Ge tProperty, _
Nothing, xlWB.BuiltinDoc umentProperties , New Object() {PName})
result.GetType( ).InvokeMember( "Value", BindingFlags.Se tProperty,
Nothing, result, New Object() {PValue})
End If

End Sub
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Yes, you're absolutely right. That's what I get for posting while
multi-tasking *sigh*

OK, this seems like a similar issue to a co-worker had.
Try this Mark - Use reflection on the object reference you do have to get
all implemented interfaces.
Also use Reflection to get the GuidAttribute from each (including the
GuidAttribute on the object ref's class).

Next, check to see that these are in fact the registered interfaces for
Excel in the system registry (based on the GUIDs).
I believe the problem may in fact be that you are trying to cast to a newer version of the interface, which an older version of Excel might not provide (or another such scenario). It would also help to know what version of the
PIAs you are using vs. what version of Office (and which PIAs have you
installed on that system).

-Rob Teixeira [MVP]

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e7******** ******@TK2MSFTN GP09.phx.gbl...
Rob,
DirectCast will call QueryInterface to find the COM interface!

I will be very curious if CType works, where DirectCast fails.

Hope this helps
Jay

"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Try CType instead of DirectCast.
In this case, you're dealing with COM wrappers, and I have the nagging
feeling you're using a newer interface and trying to cast directly to an older one. CType should call QueryInterface under the hood, so that if the interface exists, it will get you a proper reference.

Hope that helps,

-Rob Teixeira [MVP]

"Mark Nethercott" <Ma************ *@iFabre.com> wrote in message
news:up******** *****@TK2MSFTNG P09.phx.gbl...
> I get the following failure when trying to access the builtin properties;
>
> An unhandled exception of type 'System.Invalid CastException' occurred in
> resultsoutput.d ll
> Additional information: Specified cast is not valid.
>
> In general terms the code is constructed as follows;
>
> ModuleA
> Public xlWb As Excel.Workbook
>
> ModuleB
> SubA
> Dim prps As Microsoft.Offic e.Core.Document Properties
> prps = DirectCast(xlWb .BuiltinDocumen tProperties,
> Microsoft.Offic e.Core.Document Properties)
>
> This is using;
> Framework 1.1
> Excel 2002 SP3
> Reference to COM Microsoft Excel 10.0 Object Library (Version 1.4)
>
> I've been following the article by Ken Getz
>

(http://msdn.microsoft.com/library/de...-us/odc_vsto20 > 03_ta/html/ExcelObj.asp) which suggests the code should work. Ken's
article
> refers to Excel 2003, but all the objects he references are also in the > Microsoft Excel 10 COM object. I wondered whether this was a bug in the 2002
> COM or I've missed something in my code.
>
>
>



Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7271
by: Andreas Klemt | last post by:
Hello, what has the better performance and what are you using? Dim myObj As Object = 70 a) Dim myInt As Integer = DirectCast(myObj, Integer) b) Dim myInt As Integer = Convert.ToInt32(myObj) Thanks, Andreas
11
7757
by: Tubs | last post by:
i am attempting to write something which can morph itself to whatever comes in and get the value property from it but i don't know what type it is until runtime. I am therefore trying to use reflection and get the type from there and then cast it to that type so i can use it. When i use DirectCast, it won't let me do something like DirectCast(myBinding.Control,myBinding.Control.GetType()), in fact intellisense doesn't work at all because...
6
10825
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I found this in the VB Language reference: <quote> The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword.... Both keywords take an expression to be converted as the first argument, and
5
5057
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
7
2711
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
1
4100
by: iwdu15 | last post by:
can anyone explain the directcast code...ive tried using it and lookin it up but im lookin for an easy definition and how it works...ive tried using it before byut it throws errors saying it can convert it -- -iwdu15
4
1613
by: Vagabond Software | last post by:
Here is the code: Dim amountDue As Double amountDue = Math.Round(DirectCast(strOrderTotal, Double), 2) This code produces an invalid cast exception. The String variable strOrderTotal evaluates to "226.27". Is it not valid to cast a string to a double? I don't want to use CType because it is my understanding that CType is not CLS compliant. Is that
2
3413
by: lothar.behrens | last post by:
Hi, I am trying to load an assembly at runtime (plugin). Having the object (as Object), I like to cast it to the interface. The interface is defined as follows: Public Interface IVoimDescriptor Function getVendorName() As String Function getVoimDescription() As String '...
9
1197
BezerkRogue
by: BezerkRogue | last post by:
I am using this code to enter records into a SQL DB. It fails after 11 records. Can anyone tell me what's wrong? Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click 'Create Date variable Dim d0 As Date d0 = occDate.SelectedDate 'Create frequency control Dim Freq2 Freq2 = Freq.Text
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.