明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2227|回复: 5

[求助]关于VBA

[复制链接]
发表于 2004-4-25 21:51:00 | 显示全部楼层 |阅读模式
各位前辈,我没学过VBA,但听说VBA能实现我这种功能,


请帮个忙


我想在打开CAD的时候,就装载该程序,


这个程序的功能是:在上午10点时,检测服务器上的一台电脑


也就是说,这个程序在开启之后,就一直在后台运行,直到10点,发生检测电脑的事件!
发表于 2004-4-27 09:42:00 | 显示全部楼层
加载可以在acad.dvb中,或者在acaddoc.lsp编写代码来实现。定时执行,可以使用定时器,也可以通过循环来操作。如:
  1. Sub Test()
  2.        If Hour(Now) > 10 Then Exit Sub
  3.        Do While Hour(Now) < 10
  4.                DoEvents '空执行
  5.        Loop
  6.        Ping "192.168.0.1"
  7. End Sub
检测网络中的某一台电脑,实际上是Ping的操作,如果Ping的通,说明是连通的。
  1. Const SOCKET_ERROR = 0
  2. Private Type WSAdata
  3.        wVersion As Integer
  4.        wHighVersion As Integer
  5.        szDescription(0 To 255) As Byte
  6.        szSystemStatus(0 To 128) As Byte
  7.        iMaxSockets As Integer
  8.        iMaxUdpDg As Integer
  9.        lpVendorInfo As Long
  10. End Type
  11. Private Type Hostent
  12.        h_name As Long
  13.        h_aliases As Long
  14.        h_addrtype As Integer
  15.        h_length As Integer
  16.        h_addr_list As Long
  17. End Type
  18. Private Type IP_OPTION_INFORMATION
  19.        TTL As Byte
  20.        Tos As Byte
  21.        Flags As Byte
  22.        OptionsSize As Long
  23.        OptionsData As String * 128
  24. End Type
  25. Private Type IP_ECHO_REPLY
  26.        Address(0 To 3) As Byte
  27.        Status As Long
  28.        RoundTripTime As Long
  29.        DataSize As Integer
  30.        Reserved As Integer
  31.        data As Long
  32.        Options As IP_OPTION_INFORMATION
  33. End Type
  34. Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long
  35. Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSAdata As WSAdata) As Long
  36. Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
  37. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  38. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
  39. Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
  40. Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, ByVal TimeOut As Long) As Boolean
  41. Private Sub Form_Load()
  42.        'KPD-Team 2000
  43.        'URL: http://www.allapi.net/
  44.        'E-Mail: KPDTeam@Allapi.net
  45.        Const HostName = "www.allapi.net"
  46.        Dim hFile As Long, lpWSAdata As WSAdata
  47.        Dim hHostent As Hostent, AddrList As Long
  48.        Dim Address As Long, rIP As String
  49.        Dim OptInfo As IP_OPTION_INFORMATION
  50.        Dim EchoReply As IP_ECHO_REPLY
  51.        Call WSAStartup(&H101, lpWSAdata)
  52.        If GetHostByName(HostName + String(64 - Len(HostName), 0)) <> SOCKET_ERROR Then
  53.                CopyMemory hHostent.h_name, ByVal GetHostByName(HostName + String(64 - Len(HostName), 0)), Len(hHostent)
  54.                CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
  55.                CopyMemory Address, ByVal AddrList, 4
  56.        End If
  57.        hFile = IcmpCreateFile()
  58.        If hFile = 0 Then
  59.                MsgBox "Unable to Create File Handle"
  60.                Exit Sub
  61.        End If
  62.        OptInfo.TTL = 255
  63.        If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then
  64.                rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
  65.        Else
  66.                MsgBox "Timeout"
  67.        End If
  68.        If EchoReply.Status = 0 Then
  69.                MsgBox "Reply from " + HostName + " (" + rIP + ") recieved after " + Trim$(CStr(EchoReply.RoundTripTime)) + "ms"
  70.        Else
  71.                MsgBox "Failure ..."
  72.        End If
  73.        Call IcmpCloseHandle(hFile)
  74.        Call WSACleanup
  75. End Sub
这是一个Ping操作的例子,可以参考一下。
 楼主| 发表于 2004-4-28 12:16:00 | 显示全部楼层
多谢了,如果是找一个文件夹是否会简单一些呢?


有关定时器的使用的简单程序有吗?
发表于 2004-4-28 12:38:00 | 显示全部楼层
用Shell调用Dos命令Ping并将结果输出到文件,再根据文件内容判断是否Ping通
 楼主| 发表于 2004-4-29 19:52:00 | 显示全部楼层
楼上的能说清楚点么?
发表于 2006-5-5 22:03:00 | 显示全部楼层

你们的VBA学得真让我偑服,面面俱到

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-27 04:25 , Processed in 0.184272 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表