Solved

Pass String Between VB.NET and VBA

Posted on 2010-11-29
8
846 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 74

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 74

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

13 Experts available now in Live!

Get 1:1 Help Now