Solved

Pass String Between VB.NET and VBA

Posted on 2010-11-29
8
864 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

777 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