Volker
Volker

Reputation: 465

How to cast a VBA String as Object type

I am programming VBA. An interface I need to use here accepts arguments only if they are type "object". To use it I need to fill a string like "abc" into that variable. This always brings something like typemismatch.

dim varName as Object 
Set varName = "My Text"

same problem when running

dim varName as Object 
varName = "My Text"

I haven't found any conversion and have no idea how to solve

Upvotes: 1

Views: 8157

Answers (2)

David Zemens
David Zemens

Reputation: 53623

It sounds like you're referencing a .NET library, in which case System.String inherits from the generic System.Object. In other words, a String is a .NET Object -- no cast is necessary.

You should be able to pass a variable reference to the string, rather than trying to cast it to an Object type, which I don't think is possible in pure VBA.

Dim arg1 as String, arg2 as String
arg1 = "foo"
arg2 = "bar"
AddVariable(arg1, arg2)

Upvotes: 4

Wouter
Wouter

Reputation: 383

try to use variant instead: pt your objects into a variant. also do not use "set" with strings.

Upvotes: 0

Related Questions