Pass xlworksheetId to VBA

A

Ai

Hi I'm passing this work sheet ID from a XLL (using Excel4(xlSheetId,
&xRet, 1, &xRef);) to VBA and trying to use it as a sheet id. Problem
is I always get garbage value behind the string value. Any advise?

Below is my VBA codes:

Private Declare Sub CopyMemory Lib "kernel32" Alias
"RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As
Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As
Long) As Long

Private Function StringFromPointer(lngPtr As Long) As String
Dim l As Long
Dim s As String

Dim strTemp As String
Dim lngLen As Long

If lngPtr Then
lngLen = lstrlenW(lngPtr) * 2
If lngLen Then
strTemp = Space(lngLen)
CopyMemory ByVal strTemp, ByVal lngPtr, lngLen
StringFromPointer = Replace(strTemp, Chr(0), "")
End If
End If
End Function

Sub XLLCallBack(ByVal workSheetId As Long)
Debug.Print "workSheetId: " & workSheetId
Dim sheetName As String
sheetName = StringFromPointer(workSheetId)
....
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top