1
我想將一個String作爲in/out參數傳遞給一個過程。我知道這不像VBA通常的作品,但這是由於一個特殊情況。我有一個已經存在的代碼生成器工具 - 爲文件格式解析器生成代碼。我不想破解這些代碼。生成的語法可以很容易地轉換爲VBA使用文本替換這沒什麼大不了的(我已經這樣做了)。但是很難將職能轉換爲職能。Excel VBA:如何將字符串作爲指針傳遞給附加字符串的過程?
我已經知道了我如何通過傳遞指針來擴展字符串。但是我怎樣才能將字符附加到字符串?
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal length As Long)
Private Declare Function lstrlenA Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Declare Function lstrlenW Lib _
"kernel32" (ByVal lpString As Long) As Long
Sub AppendString(i_pt As Long, i_what As String)
Dim strLentgh As Long ' variable to hold the length of string
Dim ptrLengthField As Long ' pointer to 4-byte length field
' get the length of the string
ptrLengthField = i_pt - 4 ' length field is 4 bytes behind
CopyMemory strLentgh, ByVal ptrLengthField, 4
' extend the String length
strLentgh = strLentgh + (Len(i_what) * 2)
CopyMemory ByVal ptrLengthField, strLentgh&, 4
' How to apped the string?
' CopyMemory ByVal i_pt, ????
End Sub
Sub test2()
Dim str As String
Dim sPtr As Long
str = "hello"
sPtr = strPtr(str)
Debug.Print Len(str)
Call AppendString(sPtr, " there")
Debug.Print Len(str)
Debug.Print str
End Sub
哦..我想我已經嘗試了這一點,但... .. AAA HM ... :-)謝謝克里斯! – christian 2011-05-19 06:54:13
是否知道'AppendString(s,「World」)'不像AppendString s,World或'Call AppenString(s,「World」)''那樣工作?僅僅因爲它很有趣,爲什麼這是不同的行爲? – christian 2011-05-19 07:09:14