2016-04-05 132 views
-2

我有一個可以正常工作的vba代碼,但是我想創建與批處理文件相同的代碼,它可以執行與vba代碼相同的操作。vba程序的批處理文件代碼

我創建了將文件夾中的所有文件發送到指定電子郵件地址並在發送後刪除文件的代碼。

任何人都可以幫助我創建一個批處理文件,可以做同樣的事情相同的事情。

下面是VBA代碼:

Private Sub Click() 
Dim mess_body As String, StrFile As String, StrPath As String 
Dim appOutLook As Outlook.Application 
Dim MailOutLook As Outlook.MailItem 

Set appOutLook = CreateObject("Outlook.Application") 
Set MailOutLook = appOutLook.CreateItem(olMailItem) 

'~~> Change path here 
StrPath = "\Project\New folder\New folder\" 

With MailOutLook 
    .BodyFormat = olFormatRichText 
    .To = "[email protected]" 
    .Subject = "test" 
    .HTMLBody = "test" 

    '~~> *.* for all files 
    StrFile = Dir(StrPath & "*.*") 

    Do While Len(StrFile) > 0 
     .Attachments.Add StrPath & StrFile 
     StrFile = Dir 
    Loop 

    '.DeleteAfterSubmit = True 
    .Send 
End With 

Kill "\Project\New folder\New folder\*.*" 

MsgBox "Reports have been sent", vbOKOnly 
End Sub 
+0

這(COM自動化)不能與一個批處理文件來完成。 .VBS VBScript是最接近的選擇。 (或JScript) –

+0

哦!我認爲這也可以用批命令來完成。 – Striker

+0

你也可以幫我改變語法「Kill」\ Project \ New folder \ New folder \ *。*「,這樣它就可以從文本框1中取出路徑並刪除該文件夾中的所有文件。我不想更改每次從代碼中獲取路徑。 – Striker

回答

-1

U可以使用電池( 「A1」)的值作爲參照的文件夾。

Dim objFolder As Object 

Set appOutLook = CreateObject("Outlook.Application") 
Set MailOutLook = appOutLook.CreateItem(olMailItem) 

'~~> Change path here 

VAR1 = Range("A1").Value 
If VAR1 = False Then MsgBox "Cell is empty" 
If VAR1 = False Then Exit Sub 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFolder = objFSO.GetFolder(VAR1) 
With MailOutLook 
    .BodyFormat = olFormatRichText 
    .To = "[email protected]" 
    .Subject = "test" 
    .HTMLBody = "test" 

'~~> *.* for all files 
StrFile = Dir(objFolder & "*.*") 

...

'.DeleteAfterSubmit = True 
.Send 
End With 
'delete files 
Kill objFolder & "\*.*"