UInt64 - Automation Type Not SUpported - vba

I'm trying to implement a SHA512 algorithm on VBA, Excel 2003.
This is what i'm using as a basis: http://www.saphir2.com/sphlib/
I'm facing a problem with the use of UInt64 (unsigned long) in VBA. The following code throws the error: Variable Uses an Automation Type Not Supported.
Sub Main()
Dim notworking As UInt64
End Sub
How do I resolve this?

Unsigned integer datatype isn't supported in vba. I think you might have a library for vb.net.

Related

Declare Struct for DLL in OpenOffice Basic

I've got some (32 bit) Office VBA I'd like to port to Libre Office or similar:
Type wsaData
wVersion As Integer
wHighVersion As Integer
szDescription As String * WSA_DescriptionSize
szSystemStatus As String * WSA_SysStatusSize
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As String * 200
End Type
Public Declare Function WSAStartup Lib "ws2_32.dll" (wVersionRequested As Integer, lpWSAData As wsaData) As Long
However, the packing and type declarations for Libre Office Basic are different than MS Office VBA, and the Libre/Open office version crashes irretrievably. (Unlike MS Office for Windows, LO doesn't provide stack protection to DLL/dylib calls)
So far I haven't found any documentation for 'Declare Function', (which must be somewhere), but I also specifically need guidelines for passing strings and structs to declared dlls (which FAIK may not even be possible)
If anyone's done that specific struct before I'll be pleased :), but I also need guidelines.

VBA cannot dim a UInteger

I try to dim an unsigned integer using the line:
dim curStart as UInteger
and on running, I receive the error:
Compile Error
"User-defined type not defined"
Is there a reference library that is required in VBA to allow unsigned integers?
I'm using Visual Basic for Applications 7.1.1068
You’re out of luck - VBA does not support unsigned types.
Use a Long instead, which is a superset of a 16 bit unsigned type.

VBA: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to a late-bound functions

Compile Error:
Compile Error: Only user-defined types defined in public object
modules can be coerced to or from a variant or passed to a late-bound
functions.
I'm new to VBA and I was tasked with debugging some code for a custom screen in Dynamics SL. The first thing I did was to see if it compiled and I got the above message.
When I read the built-in help reference I found the following for the above error:
You attempted to use a public user defined type as a parameter or
return type for a public procedure of a class module, or as a field of
a public user defined type. Only public user defined types that are
defined in a public object module can be used in this manner.
I also went through these similar questions:
How to put user defined datatype into a Dictionary
Only user-defined type defined in public object modules can be coerced when trying to call an external VBA function
They have the same error but I don't see a collection object that the above two questions focused on.
If you may have any idea what may be causing this error please don't hesitate to suggest it.
Code:
Private Sub cpjt_entity_Chk(ChkStrg As String, retval As Integer)
Dim ldDate As Sdate
Dim xStrDailyPost As Sdate
ldDate.val = GetObjectValue("cpe_date")
'xStrDailyPost = DateToStr(ldDate)
'Call MsgBox("Daily Post Date: " & xStrDailyPost, vbOKOnly, "TEST")
serr1 = SetObjectValue("cld_id08", xStrDailyPost) <- Error highlights "xStrDailyPost"
End Sub
Definition for SetObjectValue:
Declare Function SetObjectValue Lib "swimapi.dll" Alias "VBA_SetObjectValue" (ByVal ctlname$, newval As Variant) As Integer
Thank you in advance!
You are probably working with code that was originally written with the Dynamics SL (actually it was Solomon IV at the time) Basic Script Language (BSL) macro language instead of VBA.
Regardless... the fix is, pass results of the "val" method of your xStrDailyPost instance of SDate. SO the code should look like:
serr1 = SetObjectValue("cld_id08", xStrDailyPost.val)
I've not actually tested this but I'm pretty sure this will address your issue.
If you want a little more background, "Sdate" is really just a very thin wrapper of an integer (actually I think it's a short, but I've never found I really needed to know for sure). the "Val" method returns the underlying integer in the SDate variable.

VBA - Convert string into Arithmetic Operator

Using VBA, I am trying to convert a string such as "/10" into an arithmetic operation, so that if I somehow connect it (depending on how it gets converted) after the number 200, the number 20 would be returned.
Thanks for any help.
What you're looking for is called a Math Parser. Look around for a library that you can use in VBA. If you're working in excel specific stuff - I'm sure excel already has a math parser built in - though I have no idea how you can gain access to it as the programmer. Maybe stick the expression in a cell as a string and call Eval().
EDIT
Microsoft intentionally removed this feature from function calls in excel, however it can be reinstated by creating the following function:
Function Eval(Ref As String)
Application.Volatile
Eval = Evaluate(Ref)
End Function
Then just call Eval("200" & "/10")
EDIT2
As noted in the comments below, modern versions of VBA support
Application.Evaluate("200" & "/10")
the below example provides a way of accomplishing what you are looking for.
Dim s As String
s = "/10"
Dim i As Integer
i = 200
Dim v
v = Evaluate(CStr(i) & s)
MsgBox v

Excel UDF 'not a valid addin' error

I am trying to create a custom vb.net Excel 2007 function (UDF) using VS 2010 and have gotten to this stage (borrowing heavily from Eric Carter's example at http://blogs.msdn.com/b/eric_carter/archive/2004/12/01/273127.aspx):
Namespace AutomationAddin
<Guid("1aeeb1b5-e099-4f7f-aeb0-3e9f19b64f62")>
<ClassInterface(ClassInterfaceType.AutoDual)>
<ComVisible(True)>
Public Class MyFunctions
Public MyFunctions()
Public Function MultiplyNTimes(ByVal number1 As Double, ByVal number2 As Double, ByVal timesToMultiply As Double) As Double
Dim result As Double = number1
For i As Integer = 0 To timesToMultiply - 1
result = result * number2
Next
Return result
End Function
<ComRegisterFunctionAttribute()>
Public Shared Sub RegisterFunction(ByVal type As Type)
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"))
Dim key As RegistryKey = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), True)
key.SetValue("", (System.Environment.SystemDirectory + "\mscoree.dll"), RegistryValueKind.String)
End Sub
<ComUnregisterFunctionAttribute()>
Public Shared Sub UnregisterFunction(ByVal type As Type)
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), False)
End Sub
Private Shared Function GetSubKeyName(ByVal type As Type, ByVal subKeyName As String) As String
Dim s As System.Text.StringBuilder = New System.Text.StringBuilder
s.Append("CLSID\{")
s.Append(type.GUID.ToString.ToUpper)
s.Append("}\")
s.Append(subKeyName)
Return s.ToString
End Function
End Class
End Namespace
However, when I build it using VS 2010 and try to load it in Excel 2007 using the Addin Manager>Automation I find it listed as AutomationAddin.AutomationAddin.MyFunctions and click OK only to get the error "AutomationAddin.AutomationAddin.MyFunctions is not a valid add-in." I have set the Build settings to Register for COM interop.
I've had a look online and tried following this article How to get COM Server for Excel written in VB.NET installed and registered in Automation Servers list? but to no avail. I checked my registry (after I built my project) and under CLSID/{myGuid}/InprocServer32/Default the data is set to C:\WINDOWS\system32\mscoree.dll and CLSID/{myGuid}/Programmable already exists.
I am not quite sure what I am doing wrong and would appreciate any guidance or suggestions on the topic.
Cheers,
Ben
You might want to check out this article (Build and Deploy a .NET COM Assembly) which might be helpful in your case.
I don't know if this is relevant to the question (especially after all this time) but I originally started with a COM add-in (created using Visual Studio 2010's Excel 2010 add-in project builder). I then added an automation add-in (for UDFs) by hand in the same project using the Eric Carter blog and other examples. The two worked fine independently. It was only after combining the two in the same namespace (for some obsessively tidy reason) that I started getting the "... is not a valid add-in" error. Spent a day tearing my hair out and then separated the namespaces again - problem went away.