PB DLL accessed by Excel VBA & Formulae

Share your advanced PureBasic knowledge/code with the community.
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

PB DLL accessed by Excel VBA & Formulae

Post by naw »

How to extend MS Excel formulae functionailty using VBA to access PB compiled DLLs.

In this example we create a new formula that passes 2 parameters "Hello" and "World" from Excel to VBA to PB, the PB code Capitilises the first parameter, displays both parameters individually, then returns a single (modified) string back to VBA which in turn passes the "HELLO world value back to Excel.

PB2VB2XLdll can be called as an Excel function directly, but produces unexpected and undesirable results (sometimes crashy)

The trick to getting it working seems to be by calling PB2VB2XLdll via the proxy function PB2VB2XLvba

Anyway, I hope this proves useful to others & thanks to srod for help in working this out. If anyone knows why the DLL cant be called directly from Excel (and instead needs to be called via a intermediary VBA function) I'd be interested...

Excel Formula:

Code: Select all

=PB2VB2XLvba("Hello","World")


Excel VBA in Module1

Code: Select all

Public Declare Function PB2VB2XLdll Lib "C:\PB2VB2XL.dll" (ByVal PARAM1 As String, ByVal PARAM2 As String) As String

Public Function PB2VB2XLvba(PARAM1 As String, PARAM2 As String) As String
   PB2VB2XLvba = PB2VB2XLdll(PARAM1, PARAM2)
End Function


PB Code Compiled as a DLL with UTF-8 coding

Code: Select all

ProcedureDLL.l PB2VB2XLdll(PARAM1$, PARAM2$)
   PARAM1$ = UCase(PARAM1$)
   MessageRequester("PB INFO", PARAM1$)
   MessageRequester("PB INFO", PARAM2$)
   ProcedureReturn SysAllocString_(PARAM1$ + " " + PARAM2$)
 EndProcedure
Ta - N
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

If anyone knows why the DLL cant be called directly from Excel (and instead needs to be called via a intermediary VBA function) I'd be interested...
Well, the dll function needs to be declared before Excel can call it with the correct parameters etc. The only sensible place for that really is within a VBA module. Can't stick it in a single cell! :wink:

Thanks for sharing.
I may look like a mule, but I'm not a complete ass.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Search for xll addin
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

@Edwin

couldnt find anything for xll addin do you mean dll addin or xls addin?
Ta - N
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

@Edwin
Ah! sorry - I only searched in the forums :oops: thanks for this - so you think it is possible to create Excel XLL Add ins from PB Compiled DLLs?
Interesting and if possible, really useful...
Ta - N
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Yes i think so.
I do this with another language though.
(long time ago)
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

There could be a nice little *cottage industry* for PB developers, extending the functionality of Excel with PB Code...
Ta - N
Post Reply