Domanda

Il codice dovrebbe eseguire alcuni comandi Ping e Tracert in un file, quindi e-mail il file.Invece crea un file vuoto.

Ho provato a reindirizzare da objshell.exec, ma le finestre dei pop-up che pop-up sono fastidiose e rubano la messa a fuoco;E voglio che questo funzioni periodicamente sullo sfondo utilizzando Task Scheduler.

La sintassi generata è simile a questa (e funziona quando incollata in linea di comando):

%COMSPEC% /c ping speedtest.advance2000.com >>c:\temp\testforteresa2-foo@bar-2014-01-08__10-01.txt
.

La stringa di comando risultante funziona quando è incollato in una finestra CMD> Ma i test in Excel e nell'esecuzione VBS produce un file vuoto ...

Non ti dispiacerebbe avere uno stato di attesa per verificare che l'e-mail venga inviata in modo che potesse eliminare il file TXT.Lo capirà più tardi :)

'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
.

È stato utile?

Soluzione

Il tuo

Set reportfile = objfso.OpenTextFile(ReportFileNAme, ForAppending, True)
.

Apre il file denominato ReportFileName.Il .run

"%COMSPEC% /c ping " & MyObj & " >>" & ReportFileNAme  
.

Quindi chiede al sistema operativo di scrivere in quel file aperto.Prova a saltare la creazione del reportFile.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top