PDA

View Full Version : Copy contents of email as .bat and execute



Ryan_Mika
04-09-2013, 12:18 PM
I have searched all over and have not found a solution. I'm really hope you guys can help. I am trying to copy the body of an email, in outlook as a rule, as a batch file and execute the batch file.

I have successfully created the .bat or .cmd from the body of an email.

I can create a .cmd or .bat as a stand alone VBS script, runs fine and executes the file.

When the email copies to the .bat it does not execute the file, doesn't give me an error or anything.

This run just fine as a stand alone (Not in outlook) VBS script:

Dim objFSO 'As FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim strDirectory 'As String
strDirectory = "C:\NewDirectory"

Dim objDirectory 'As Object
If objFSO.FolderExists(strDirectory) Then
Set objDirectory = objFSO.GetFolder(strDirectory)
Else
Set objDirectory = objFSO.CreateFolder(strDirectory)
End If

Dim strFile 'As String
strFile = "NewFile.bat"

Dim objTextFile 'As Object
Dim blnOverwrite 'As Boolean
blnOverwrite = True
Set objTextFile = objFSO.CreateTextFile(strDirectory & "\" & strFile, blnOverwrite)

objTextFile.Write ("ipconfig >> c:\ipconfig.txt")

' This results in the string "This is a new text file"

objTextFile.Close
Set objTextFile = Nothing
Set objDirectory = Nothing
Set objFSO = Nothing

Dim objShell 'As String
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /K CD C:\newdirectory & newfile.bat & exit"
Set objShell = Nothing


This one copies the body of the email(as a rule in outlook) as a .bat file but fails to execute:

Public Sub SaveMessageToFile(vMsg As MailItem)

Dim objFSO 'As FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim strDirectory 'As String
strDirectory = "C:\NewDirectory"

Dim objDirectory 'As Object
If objFSO.FolderExists(strDirectory) Then
Set objDirectory = objFSO.GetFolder(strDirectory)
Else
Set objDirectory = objFSO.CreateFolder(strDirectory)
End If

Dim strFile 'As String
strFile = "NewFile.bat"

Dim objTextFile 'As Object
Dim blnOverwrite 'As Boolean
blnOverwrite = True
Set objTextFile = objFSO.CreateTextFile(strDirectory & "\" & strFile, blnOverwrite)

objTextFile.Write vMsg.Body

' This results in the string "This is a new text file"

objTextFile.Close
Set objTextFile = Nothing
Set objDirectory = Nothing
Set objFSO = Nothing

Dim objShell 'As String
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cmd /K CD C:\newdirectory & newfile.bat & exit"
Set objShell = Nothing


End Sub





Any help would be greatly appreciated, I'm not all that great with VBS.

Thanks in advance.