由于所有的代码都可以不通过Excel,故最终把代码放到了Outlook里执行,对于Ontime在outlook里不能运行,故采用了API函数settimer来处理,参考, (同样在Outlook里应引用XML6.0和Redemption) Thisoutlooksession里代码: Private Sub Application_Startup() hjs MsgBox "OK, Run. -Long_III" End Sub Private Sub Application_Quit() Stop_hjs End Sub 模块里代码: Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElaspe As Long, ByVal lpTimerFunc As Long) As Long Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long Dim TID Const SendTime As Double = 30 '设置发送邮件的间隔时间,这里以30分钟发一次为例 Sub hjs() '访问路由器,取得目前状态的IP地址 Dim httpRequest As MSXML2.XMLHTTP30 Dim s$, s1$, m%, n% Set httpRequest = New MSXML2.XMLHTTP30 httpRequest.Open "GET", "http://192.168.10.10/Status_Router.asp", False, "Admin", "xxxxxx" httpRequest.Send "" s = httpRequest.responseText m = InStr(1, s, "var wan_ip =") n = InStr(m, s, ";") s1 = Mid(s, m, n - m) s2 = Split(s1, """")(1) '根据字符串的规律,取出具体的IP值 Set httpRequest = Nothing SendMail "hjsong_8116@163.com", "IP:=" & s2 & Format(Date, " yyyy-mm-dd"), "Jinsong, FYI", "" KillTimer 0, TID TID = SetTimer(0, 0, SendTime * 60 * 1000, AddressOf hjs) '到时间重复执行此程序 End Sub Sub SendMail(Email_Address$, Subject$, Body$, Attachment$) '发送邮件(收件人,标题,内容,附件) Dim ObjOL As Object, itmNewMail As Object, itmNewMail_Copy As Object Dim olNS As Object, dfrFdr As Object Set ObjOL = CreateObject("Outlook.Application") Set itmNewMail = ObjOL.CreateItem(olMailItem) Set itmNewMail_Copy = CreateObject("Redemption.SafeMailItem") Set olNS = ObjOL.GetNamespace("MAPI") '获取Namespace Set dftFdr = olNS.GetDefaultFolder(16) '获取草稿箱接口 With itmNewMail .Subject = Subject .Body = Body .To = Email_Address .Save End With For I = 1 To dftFdr.Items.Count '遍历文件夹下面的所有Item,把草稿箱里的文件直接发送 itmNewMail_Copy.Item = dftFdr.Items.Item(I) If InStr(1, itmNewMail.Subject, "IP:=") > 0 Then itmNewMail_Copy.Send End If Next Set ObjOL = Nothing Set itmNewMail = Nothing Set itmNewMail_Copy = Nothing Set olNS = Nothing Set dfrFdr = Nothing End Sub Sub Stop_hjs() '取消程序的运行 KillTimer 0, TID MsgBox "OK, Stop. -Long_III" End Sub
所有代码来源于网络,仅供学习使用。
[此贴子已经被作者于2008-7-29 18:33:02编辑过] |