我每次收到特定電子郵件時都必須運行一些java代碼。 我只是在電子郵件中下載附件,運行jar並回復執行該jar附件的響應。在Outlook中爲特定電子郵件運行jar
是否有可能以某種方式自動執行此操作?
我已經檢出了可以使用Outlook規則調用的VBA例程,但我不確定是否可以用此操作執行我的jar文件。
任何想法?
我每次收到特定電子郵件時都必須運行一些java代碼。 我只是在電子郵件中下載附件,運行jar並回復執行該jar附件的響應。在Outlook中爲特定電子郵件運行jar
是否有可能以某種方式自動執行此操作?
我已經檢出了可以使用Outlook規則調用的VBA例程,但我不確定是否可以用此操作執行我的jar文件。
任何想法?
這裏是結構,我會讓你調整它!
您可以設置規則使用SaveToDiskAndReply
這是主程序。
在您的Outlook模塊的起始粘貼此:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub RunSleep(_
exec As WshExec, _
Optional timeSegment As Long = 800 _
)
Do While exec.Status = WshRunning
Sleep timeSegment
Loop
End Sub
Private Function RunProgram(_
program As String, _
Optional command As String = "" _
) As WshExec
Dim wsh As New WshShell
Dim exec As WshExec
Set exec = wsh.exec(program)
Call exec.StdIn.WriteLine(command)
Call RunSleep(exec)
Set RunProgram = exec
End Function
Public Function Run_Jar() As String
Dim program As WshExec
Dim value As String
'''Set the path (jar and log)
Set program = RunProgram("java -jar ""D:\\Demo.jar"" 8861ccd621")
DoEvents
Run_Jar = program.StdOut.ReadAll
End Function
,並使用由規則推出的腳本:
Public Sub SaveToDiskAndReply(ItM As Outlook.MailItem)
Dim oAttS As Outlook.Attachments
Dim objAtt As Outlook.Attachment
Dim oItM As Outlook.MailItem
Dim saveFolder As String
Dim dateFormat As String
Dim JarReturn As String
dateFormat = Format(Now, "yyyy-mm-dd")
saveFolder = "c:\temp\"
Set oAttS = ItM.Attachments
'''Save the attachements
For Each objAtt In oAttS
objAtt.SaveAsFile saveFolder & objAtt.FileName
Next objAtt
'''Run your jar
JarReturn = Run_Jar
'''Fill the email
Set oItM = OutApp.CreateItem(0)
'''Decomment the next line when you're done testing
'On Error Resume Next
With oItM
.To = ItM.SenderEmailAddress
.CC = ""
.BCC = ""
.Subject = ItM.Subject
.Body = JarReturn
For Each objAtt In oAttS
.Attachments.Add saveFolder & objAtt.FileName
Next objAtt
.Send 'or use .Display
End With
On Error GoTo 0
Set oAttS = Nothing
Set objAtt = Nothing
End Sub
非常感謝。需要一些時間來理解它。 –
代碼的兩個部分都會出現在單個模塊中嗎? –
@adirohan:由於'Public Function Run_Jar'是'Public',它是唯一從主程序中調用的,你可以將它們放在兩個獨立的模塊中! ;) – R3uK
http://stackoverflow.com/questions/30289444/ how-to-call-java-jar-file-from-excel-vba and https://www.mrexcel.com/forum/excel-questions/775383-running-jar-visual-basic-applications.html 還有耶規則似乎是一個很好的觸發器,它只依賴於你想要觸發代碼的參數! ;)我只是不知道是否/如何將jar的結果再次傳遞給VBA以填充響應 – R3uK
進一步說明,顯然,您可以將jar的輸出重定向到日誌文件,然後您可以讀取它來自VBA!看看我已經鏈接的SO問題的評論! ;) – R3uK
謝謝,它看起來像可能只是做我想做的。現在學習VB的時間:) –