Solved

Pass String Between VB.NET and VBA

Posted on 2010-11-29
8
854 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now