每30分钟VBA Excel电子邮件自动化

时间:2014-08-16 21:49:12

标签: vba email automation recurring

我想要一个excel文件每30分钟保存一次(邮票时间)并通过电子邮件发送给我。我已经将文件保存并发送给我。但是,当我连续30分钟尝试这样做时,我遇到了问题。下面是我的代码,任何人都有任何想法?我尝试做一个循环函数,所以当A1 = 0时它会继续运行,但是当我点击停止按钮时,它会改变A1 = 1,并且基本上停止。我也希望能够运行这个程序,仍然可以使用excel而不是被打扰,任何想法?

Option Explicit

Private Sub CommandButton1_Click()
Range("A1").Value = 0
Do While Range("A1").Value = 0
Loop

ActiveWorkbook.SaveAs "VBA Email " & Format(Date, "mm-dd-yyyy") & " " & Format(Time, "h.mm AM/PM") & ".xlsm"
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.to = "xxxx@gmail.com"
.Subject = "VBA Email"
.Attachments.Add (ActiveWorkbook.FullName)
.Display
Application.SendKeys "%s"
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

Application.Wait (Now + TimeValue("0:00:02"))


End Sub


Private Sub CommandButton2_Click()
Range("A1").Value = 1
End Sub

2 个答案:

答案 0 :(得分:0)

要使用循环,请在Do While行和Loop行之间输入您的对帐单:

Do While... 
  ' statements here to be executed while
  ' the loop is running
Loop

答案 1 :(得分:0)

而不是Application.Wait函数使用Application.OnTime e.g。

Public Sub EventMacro()    
    alertTime = Now + TimeValue("00:30:00")
    Application.OnTime alertTime, "EventMacro"
    'Save your file and email it
End Sub