0

我的函数用于替换一个撇号,即' 在任何对象的属性中。该函数使用 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 中运行时不会出现任何错误,但在编译时会出现错误?有人可以指出我做错了什么吗?

4

1 回答 1

2

首先,您并没有通过错误处理来帮助自己。在不进行某种形式的检查实际错误的情况下进行简历下一步是一种混乱和错误城市的一站式方式。您的标题具有误导性-导致问题的不是“ For Each”语句。这是:

TLI.InterfaceInfoFromObject(obj).Members

准确地说,是成员属性失败了。(哦,你显然没有直接复制和粘贴,因为我认为“TLI”应该是“tTLI”。)。其原因可能是您尝试使用的对象没有在类型库中注册的公共接口。

我的猜测是您正在尝试使用内部 VB 类来执行此操作,例如 Form 或私有类。在运行时,VB IDE 即时创建运行时类型库信息(您认为如何在运行时使用未编译的 DLL 项目?)。在 IDE 中,VB 动态地为您的类创建类型库信息。但是编译的时候,这个信息不存在,所以报错。

如果是这种情况,您将不得不通过创建一个公开您要使用的接口的 DLL 手动为您的私有类创建接口。然后这个接口将被实现到私有类中。可悲的是,您不能对 Form 实例执行此操作。

于 2013-04-17T10:54:47.100 回答