2017-06-19 73 views
1

访问时坠毁,并没有错误调用此VBA:调用函数

Public IE As InternetExplorer 
Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long) 
Dim el As Object 

Public Function IEClick(Idstr As String) 
Do 
    Set el = Nothing 
    On Error Resume Next 
    Set el = IE.Document.getElementById(Idstr) 
    On Error GoTo 0 
    DoEvents 
    Sleep (100) 
Loop While el Is Nothing 
Sleep (1000) 
IE.Document.getElementById(Idstr).Click 
End Function 

其时,真正的循环,直到找到正确的elementid我有一个问题。当这个函数被调用像这样的时候,我在这条线上没有错误信息IEClick Str(Item)

For Each Item In coll 
    IEClick Str(Item) 
    p = p + 1 
Next 

这是完整的代码体。

Public Sub AutoExtract() 
Dim ProgressBar_Extract As Object 
Dim coll As Collection 
Set coll = New Collection 

'/////////////////controls in order/////////////// 
With coll 
    .Add "ctl00_cmdCustom" 
    .Add "chkAll" 
    .Add "ctl00_cmdContinue" 
End With 
Me.ProgressBar_Extract.Max = coll.Count 
'////////////////////start ie////////////////// 
Set IE = New InternetExplorerMedium 
With IE 
    '.AddressBar = False 
    '.MenuBar = False 
    .Navigate ("*****") 
    .Visible = True 
End With 
'//////////////////////Start Automation/////////////////// 
For Each Item In coll 
    IEClick Str(Item) 
    p = p + 1 
Next 
MsgBox "Extract Complete" 
End Sub 

我把范围缩小到IEClick Str(Item)。当调试,如果我制止它,一步下一个崩溃行。

+0

*“没有错误信息时崩溃”*是什么意思?它锁定?或者它关闭(然后Access进程仍然运行?)? – Andre

+0

@Andre关闭进程没有运行。 – Quint

+0

作为第一步,我推荐一个完整的[反编译](http://stackoverflow.com/a/3268188/3820271)。 – Andre

回答

0

1)不使用Item作为变量名,它是在VBA(Collection.Item的保留字)

2)将Option Explicit在每个模块的顶部。
它执行变量声明并在编译时报告未声明或拼写错误的变量/常量。 要在新模块中自动执行此操作,请在VBA编辑器中设置Require Variable Declaration选项。

3)我无法重现崩溃,但在使用字符串参数调用Str()时出现运行时错误。

试试这个:

Option Explicit 

Public Sub TestCrash() 

    Dim coll As Collection 
    Dim vItem As Variant 

    Set coll = New Collection 

    With coll 
     .Add "ctl00_cmdCustom" 
     .Add "chkAll" 
     .Add "ctl00_cmdContinue" 
    End With 

    For Each vItem In coll 
     ' Gives Runtime error 13 "Type mismatch" 
     ' Debug.Print Str(vItem) 

     ' This works 
     Debug.Print vItem 
    Next 

End Sub 

,如果它的工作原理,适应你的代码。

+0

谢谢你的帮助安德烈。我会放弃这一点并让你知道。 – Quint

+0

如果答案解决了您的问题,您可以[接受它](http://stackoverflow.com/help/someone-answers),这也标志着问题已解决。 @Quint – Andre