下面这段代码,在本机执行,好用。
log文件中,会打印出"OK"。打包成exe,
放到服务器上,用telnet命令登录服务器,再执行,就会在log文件中会出现automation error。
不通过telnet访问,直接在服务器上操作(运行环境是服务器),双击,也正常打印出了“OK”。开始怀疑是GetObject的问题,后来代码改了下GetObject("c:\test.xls")跑了遍,又打印出“OK”了,所以觉得是TELNET和LDAP混合使用造成的问题。
Option ExplicitPrivate Sub Main()    'Dim MyXL As Object 'OK例
        'Dim objCon As Object 'ERROR例
        Dim objCon As IADsContainer
        
On Error GoTo Errhandle
     'Set MyXL = GetObject("c:\test.xls")'OK例
        Set objCon = GetObject("LDAP://OU=address,DC=thai,DC=ithd,DC=local")
        
     Call WriteLog("C:\GetObject.log", "OK")
Errhandle:
    Call WriteLog("C:\GetObject.log", Err.Description)
End SubPublic Sub WriteLog(ByVal strLogFileName As String, ByVal strLog As String)
        
    Dim intFileNo         As Integer
    Dim strTime           As String
    
    strTime = Format(Now, "YY/MM/DD HH:MM:SS")
    intFileNo = FreeFile()
    Open strLogFileName For Append Access Write As #intFileNo
    strLog = strTime & vbTab & strLog
    Write #intFileNo, strLog
    Close #intFileNo
        
End Sub