简体   繁体   English

如何将代码添加到Mail_Workbook vba中以每天打开,刷新,发送然后关闭?

[英]How do I add code to my Mail_Workbook vba to open everyday, refresh, send, then close?

I need to add vba to open this workbook, refresh the data, automatically, send, then close. 我需要添加vba才能打开此工作簿,自动刷新数据,发送,然后关闭。

Here is my code which works fine on it's own but I need to automate this daily. 这是我的代码,它本身可以很好地工作,但是我需要每天自动执行此操作。

Sub Mail_Workbook()
Dim OutApp As Object
Dim OutMail As Object
Dim EmailAddr As String
Dim Subj As String



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

With OutMail
.To = "me.meeee@company.com"
.CC = ""
.BCC = ""
.Subject = "***TEST*** " & Subj
.Body = Subj
.Attachments.Add ActiveWorkbook.FullName
.Display
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "%S"
End With
Set OutMail = Nothing
End Sub

You may try something like below. 您可以尝试以下类似方法。 On workbook open it calls a procedure RunMacro . 在工作簿上打开它会调用过程RunMacro

The RunMacro procedure reads the values from the ranges and sets the time when the MIS procedure has to be called. RunMacro过程从范围中读取值,并设置必须调用MIS过程的时间。

MIS procedure will open the workbook, Refresh it , get a path to save the file and finally send the mail. MIS程序将打开工作簿,刷新它,获取保存文件的路径,最后发送邮件。

In the mail it will send the link for the workbook and wont attach the workbook. 在邮件中,它将发送工作簿的链接,并且不会附加工作簿。 So you can save the workbook on any shared drive. 因此,您可以将工作簿保存在任何共享驱动器上。

Put this code on ThisWorkbook code section 将此代码放在ThisWorkbook代码部分

 Private Sub Workbook_Open()
    RunMacro
End Sub


Put this code in any Standard Module. 将此代码放在任何标准模块中。

Sub RunMacro()


    Dim a As String, b As String, c As String, d As String, e As String

    a = Format(Range("A3"), "hh:mm:ss")
    b = Format(Range("A4"), "hh:mm:ss")
    c = Format(Range("A5"), "hh:mm:ss")
    d = Format(Range("A6"), "hh:mm:ss")
    e = Format(Range("A7"), "hh:mm:ss")


    Application.OnTime TimeValue(a), "MIS"
    Application.OnTime TimeValue(b), "MIS"
    Application.OnTime TimeValue(c), "MIS"
    Application.OnTime TimeValue(d), "MIS"
    Application.OnTime TimeValue(e), "MIS"
End Sub

Sub MIS()

'open the workbook
    Dim wkb As Workbook
    Dim Path As String, strFile As String, strFilePath As String

    strFile = "file1.xlsx"
    Path = ThisWorkbook.Path & "\" & strFile

    If IsWorkBookOpen(Path) Then
        Set wkb = Workbooks(strFile)
    Else
        Set wkb = Workbooks.Open(Path)
    End If

    'Refresh the data
    wkb.RefreshAll

    'get new filePath
    strFilePath = getFileLink

    wkb.SaveAs Filename:=strFilePath 
    wkb.Close

    'send mail
    SendMail strFilePath


End Sub

Function IsWorkBookOpen(FileName As String)
'Check if workbooks is open
'IsOpen Return true

    Dim ff As Long, ErrNo As Long

    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff
    Close ff
    ErrNo = Err
    On Error GoTo 0

    Select Case ErrNo
    Case 0: IsWorkBookOpen = False
    Case 70: IsWorkBookOpen = True
    Case Else: Error ErrNo
    End Select
End Function

Sub SendMail(myDest As String)
'procedure to send mail
'you need to configure the server & port

    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant


    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1
    Set Flds = iConf.Fields

    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "test-svr-002"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With

    With iMsg

        Set .Configuration = iConf
        .To = "test@gmail.com"
        .From = "test@gmail.com"
        .Subject = "MIS Reports" & " " & Date & " " & Time
        .TextBody = "Link to Mis Report :" & vbNewLine & "<" & myDest & ">"
        .Send
    End With

    Set iMsg = Nothing
    Set iConf = Nothing

End Sub

Function getFileLink() As String

    Dim fso As Object, MyFolder As String
    Set fso = CreateObject("Scripting.FileSystemObject")

    MyFolder = ThisWorkbook.Path & "\Reports"


    If fso.FolderExists(MyFolder) = False Then
        fso.CreateFolder (MyFolder)
    End If

    MyFolder = MyFolder & "\" & Format(Now(), "MMM_YYYY")

    If fso.FolderExists(MyFolder) = False Then
        fso.CreateFolder (MyFolder)
    End If

    getFileLink = MyFolder & "\MIS " & Format(Now(), "DD-MM-YY hh.mm.ss") & ".xls"
    Set fso = Nothing

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Excel VBA,如何在第二个工作簿中的“ thisworkbook.close”事件之后使我的原始代码继续执行? - Using Excel VBA, how do I keep my original code executing after a 'thisworkbook.close' event in a 2nd workbook? 如何在vba中添加库引用,以便每次打开新工作簿时都会添加它? - How do I add a library reference in vba so that it remains added every time I open a new workbook? 如何在Excel中使用其目录路径而不是使用vba进行代码编码来关闭打开的工作簿? - How can I code to close an open workbook using its directory path instead of its name using vba in excel? 如何修改此VBA代码,使其在我的工作簿中的所有工作表上运行? - How do I modify this VBA code so that it runs on all worksheets in my workbook? 如何使用VBA打开新工作簿并添加图像? - How to open a new workbook and add images with VBA? 如何从VBA代码打开受保护的工作簿? - How to open a protected workbook from VBA code? VBA代码关闭无效的工作簿 - VBA code to close inactive workbook 如何更快地打开此VBA工作簿? - How can I open this VBA workbook faster? 如何在vba中关闭已经打开的cmd窗口? - How do I Close a already open cmd Window in vba? 我有一个宏来刷新工作簿中的所有数据透视表,我需要添加代码以删除(空白) - I have a macro to refresh all pivot tables in my workbook, I need to add code to remove (blanks)'s
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM