我的函数用于替换一个撇号,即' 在任何对象的属性中。该函数使用 TypeLib 库来实现这一点,通过循环遍历所有对象的成员,即
Public Function EscapeWildCards(ByRef obj As Object, _
Optional ByVal bEscape As Boolean = True) As Boolean
On Error GoTo EscapeWildCards_Err
Dim tTLI As TLIApplication
Dim tMem As MemberInfo
Dim tInvoke As InvokeKinds
Dim tName As String 'used as lower case....
Dim tString As String
1 Set tTLI = New TLIApplication
'... if True, we are setting else we are getting
2 tInvoke = IIf(bEscape, VbGet, VbLet)
3 If obj Is Nothing Then Exit Function
4 For Each tMem In TLI.InterfaceInfoFromObject(obj).Members
5 'tName = LCase$(tMem.Name)
6 If tMem.InvokeKind = tInvoke Then 'And tMem.Parameters.Count = 0
'could be object/something else that can't handle
On Error Resume Next
' get the oobject property value
7 tString = CallByName(obj, tMem.Name, VbGet)
8 If tInvoke = VbGet Then
9 If IndexOf(tString, "'") > 0 Then
10 tString = Replace$(tString, "'", "`")
11 CallByName obj, tMem.Name, VbLet, tString
End If
Else
'... set data replacing aposthrophe
12 If IndexOf(tString, "'") > 0 Then
13 tString = Replace$(tString, "`", "'")
14 CallByName obj, tMem.Name, VbLet, tString
End If
End If
'Debug.Print tName, " = ", tString
On Error GoTo EscapeWildCards_Err
End If
Next
Exit Function
EscapeWildCards_Err:
ErrReport Err.Description, "modCommon.EscapeWildCards", Erl
Resume Next
End Function
当我在 IDE 中测试代码时,我没有收到任何错误。但是当我作为 EXE 编译和测试时,出现以下错误:
Object doesn't support this action. LineNo 4
Object variable or With block variable not set. LineNo 5
Object variable or With block variable not set. LineNo 6
For loop not initialized. LineNo 14
为什么应用程序在 IDE 中运行时不会出现任何错误,但在编译时会出现错误?有人可以指出我做错了什么吗?