Solved

Pass String Between VB.NET and VBA

Posted on 2010-11-29
8
887 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

632 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