【问题标题】:VBA Excel Email Automation every 30 min每 30 分钟 VBA Excel 电子邮件自动化
【发布时间】:2014-10-10 05:54:46
【问题描述】:

我想要一个 excel 文件每三十分钟保存一次(盖章时间),然后通过电子邮件发送给我。我已将文件保存并发送给我。但是,当我尝试连续执行此操作 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

【问题讨论】:

  • 您遇到了什么问题?它在做什么不是你期望它做的?编辑您的问题以添加信息。

标签: vba email automation recurring


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用 Application.OnTime 代替 Application.Wait 函数 例如

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

    【讨论】:

    • 将“EventMacro”替换为“CommandButton1_Click”,并在程序开头添加代码
    猜你喜欢
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    相关资源
    最近更新 更多