在VBA中执行Java命令,开始打开Excel执行始终不行,第二次运行就又可以了.
我用的是网上提供的执行命令方法,每次执行要等待程序完成才进行下一步.单独用Shell不行http://www.freevbcode.com/ShowCode.asp?ID=99
Public Function ShellandWait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0) As Boolean
    
    Dim lInst As Long
    Dim lStart As Long
    Dim lTimeToQuit As Long
    Dim sExeName As String
    Dim lProcessId As Long
    Dim lExitCode As Long
    Dim bPastMidnight As Boolean
    
    On Error GoTo ErrorHandler    lStart = CLng(Timer)
    sExeName = ExeFullPath    'Deal with timeout being reset at Midnight
    If TimeOutValue > 0 Then
        If lStart + TimeOutValue < 86400 Then
            lTimeToQuit = lStart + TimeOutValue
        Else
            lTimeToQuit = (lStart - 86400) + TimeOutValue
            bPastMidnight = True
        End If
    End If    lInst = Shell(sExeName, vbMinimizedNoFocus)
    
lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)    Do
        Call GetExitCodeProcess(lProcessId, lExitCode)
        DoEvents
        If TimeOutValue And Timer > lTimeToQuit Then
            If bPastMidnight Then
                 If Timer < lStart Then Exit Do
            Else
                 Exit Do
            End If
    End If
    Loop While lExitCode = STATUS_PENDING
    
    ShellandWait = True
   
ErrorHandler:
ShellandWait = False
Exit Function
End Function谁能提供另一种方法,我要每次执行外部命令都能完成,这样接下来的脚本就不会出错.
十分感谢 

解决方案 »

  1.   

    等待一条 Shell() 执行完成后,再执行后续的 Shell(),一般都是用这样的方法呀?
    这有什么问题……你的代码,那个 Exit Function 写错位置了吧!
    应该在行号 ErrorHandler: 之前啊。
    要不然,你的这个函数只有返回 False,没有返回 True 的。
      

  2.   

    使用ms script control控件做这个事情估计更好
      

  3.   

    请问vba中吧excel中一个sheet中的2000条数据,向oracle批量insert数据有什么好的方式么? 循环太慢了感觉。大家有什么好的办法么?谢谢。。