代码应该对文件执行一些ping和tracert命令,然后通过电子邮件发送文件。相反,它会创建一个BLANK文件。
我尝试从Objshell.EXEC重定向,但弹出的弹出窗口令人烦恼并且窃取焦点;我想让它在后台使用任务计划程序定期运行。
生成的语法如下所示(粘贴到命令行时有效):
%COMSPEC% /c ping speedtest.advance2000.com >>c:\temp\testforteresa2-foo@bar-2014-01-08__10-01.txt
结果命令字符串在粘贴到CMD时起作用>窗口,但excel和执行的VBS中的测试产生一个空白文件......
不介意等待状态检查要发送的电子邮件,以便删除txt文件。稍后会详细说明:)
'On Error Resume Next
Const ForReading = 1
Const ForAppending = 8
'PingSpeedTest
Sub PingSpeedTest()
Dim GetUserLoginID ''As String
Set objfso = CreateObject("Scripting.FileSystemObject")
Dim WSHNetwork
Set WSHNetwork = CreateObject("WScript.Network")
GetUserLoginID = CStr(WSHNetwork.UserName)
getuserdomain = CStr(WSHNetwork.UserDomain)
'''''''''''REPORT NAME''''''''''''''''''''''''''''''
ReportFileNAme = "c:\temp\testforteresa2-" & GetUserLoginID & "@" & getuserdomain & "-" & _
DatePart("yyyy", Now) & "-" & _
Right("0" & DatePart("m", Now), 2) & "-" & _
Right("0" & DatePart("d", Now), 2) & "__" & _
Right("0" & DatePart("h", Now), 2) & "-" & _
Right("0" & DatePart("m", Now), 2) & ".txt"
On Error Resume Next
objfso.DeleteFile (ReportFileNAme)
On Error GoTo 0
Set reportfile = objfso.OpenTextFile(ReportFileNAme, ForAppending, True)
Set objShell = CreateObject("WScript.Shell")
Set List = CreateObject("System.Collections.ArrayList")
List.Add "speedtest.advance2000.com"
List.Add "myphone.advance2000.com"
List.Add "vdesk.advance2000.com"
'''
For Each MyObj In List
MyCmd = "%COMSPEC% /c ping " & MyObj & " >>" & ReportFileNAme '''<<< Should work- creates correct syntax but no output
objShell.Run MyCmd, 3, True
MyCmd = "%COMSPEC% /c tracert " & MyObj & " >>" & ReportFileNAme
objShell.Run MyCmd, 3, True
Next ''MyObj
Dim olLook ''As Object 'Start MS Outlook
Dim olNewEmail ''As MailItem ' Object 'New email in Outlook
Dim strContactEmail ''As String 'Contact email address
Set olLook = CreateObject("Outlook.Application")
Set olNewEmail = olLook.createitem(0)
strEmailSubject = "TopSellers.accdb Application"
strEmailText = "PING AND TRACEROUTE RESULTS"
'strContactEmail = GetUserLoginID & "@" & getuserdomain & ".com"
With olNewEmail 'Attach template
.To = "Foo@BAR.com" 'strContactEmail<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'.CC = strCc
.body = strEmailText
.Subject = "RE:PING AND TRACERT RESULTS"
.display
.Attachments.Add (ReportFileNAme)
End With
'objfso.DeleteFile (ReportFileNAme)
End Sub
答案 0 :(得分:1)
您
Set reportfile = objfso.OpenTextFile(ReportFileNAme, ForAppending, True)
打开名为ReportFileNAme的文件。 .Run
"%COMSPEC% /c ping " & MyObj & " >>" & ReportFileNAme
然后要求操作系统写入该打开的文件。尝试跳过reportfile的创建。