Solved

Pass String Between VB.NET and VBA

Posted on 2010-11-29
8
870 Views
Last Modified: 2012-06-27
I am trying to create a VB.NET DLL that does functions for Excel VBA.  I am trying to pass strings back and forth between the VB.NET functions and VBA.  I am having a bunch of problems.  I am very experienced with VBA but new to VB.

I am exposing through COM.  First, I created a structure in the VB DLL that was to be used by both the VB functions and the VBA.  When I add a string variable type into the structure, VBA doesn't recognize the structure at all.  When I take out the string, the structure works fine.  I then tried to create a VB function that returns a string, and VBA didn't like that.  The function in VB is:

    Public Function GetString() As String

        Return "That's nice"

    End Function

When I try to call it from VBA I get "Object doesn't support this property or method".

So my questions are: how do I pass a string between VB and VBA?  How should I define the string in both languages so that they can communicate?
0
Comment
Question by:BladdyK
  • 4
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34233332
Perhaps the following discussion will be of use to you. It's not specific to strings, but does show how to incorporate .NET DLLs for use in VBA.

    http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26450356.html
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34233871
There should not be a problem returning a string. Is the method marked for COM interop?

http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/
0
 

Assisted Solution

by:BladdyK
BladdyK earned 0 total points
ID: 34234703
The issue is that VBA is not a .NET environment, so the data size is different.  In VB, the data size is 16 bits per character where in VBA it's 8 bits.  I converted the string in VBA into an array of unicode bytes and then converted it back into a string in VB.  The proper functions are:

VBA:
To Bytes:  Set Byte array equal to string
From Bytes: StrConv([bytearray], vbUnicode)

VB
Imports System.Text
To Bytes = Unicode.GetBytes([string])
From Bytes = Encoding.Unicode.GetString([bytearray])

This seems to work, but you have to be careful putting an excel range into bytes. You have to use Range.Value.  I

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 34235519
Perhaps I'm missing something, but this worked for me. Admittedly, it's in Access 2007, but I can test in Excel tomorrow.
'''''''''''''''''''''''''''''''''''''''''''''
'   Interface Def.
'''''''''''''''''''''''''''''''''''''''''''''
Public Interface IDotNet
  Function GetString() As String
End Interface


'''''''''''''''''''''''''''''''''''''''''''''
'   Class Def.
'''''''''''''''''''''''''''''''''''''''''''''
<System.Runtime.InteropServices.ClassInterface(Runtime.InteropServices.ClassInterfaceType.AutoDual)> _
Public Class DotNetClass
  Implements IDotNet

  Public Function GetString() As String Implements IDotNet.GetString
    Return "That's nice"
  End Function
End Class

Open in new window

Untitled.png
Untitled1.png
Untitled2.png
0
 

Author Comment

by:BladdyK
ID: 34235771
I will check that out tomorrow as well.  I am using Excel 2003, which could be the issue.  I have Access 2007 and can get my hands on Excel 2007, so I will try that out there, too. It wouldn't surprise me if they corrected this issue so that they facilitate people migrating to VSTO.

Ironically, I was using Excel 2003 instead of Excel 2007 because it is much faster with VBA.  
0
 

Assisted Solution

by:BladdyK
BladdyK earned 0 total points
ID: 34251726
I tried passing a string between Access 2007 and a DLL.  It worked.  I tried passing a string between Excel 2003 and a DLL it worked.  But when I tried passing a string through a structure from the DLL to VBA, that's when it doesn't out.  So it has something to do with the Interop.

I don't know if it works between Excel 2007 and the DLL, but in reading through MSFTs site, it says that for applications that are not written in .NET, including automation, to be wary of strings.  

In the mean time, converting to a byte array seems to work OK, so I am going to stick with that solution.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34256784
Sorry I could not be of more assistance. I have not done much COM programming.   :\

In any event, I am happy you at least have something functional. If I provided any assistance or insight, then I was glad to do so  :)
0
 

Author Closing Comment

by:BladdyK
ID: 34281133
I figured out what the underlying issue was in addition to a solution.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question