Outlook和VBA之间的追溯链接 [英] Retroactive link between outlook and vba

查看:187
本文介绍了Outlook和VBA之间的追溯链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Access Vba程序,以便自动向人们发送邮件.但是,我们还是选择在Outlook中手动按发送"(以防可能出现问题,因此我们可以事先控制邮件).

有没有一种方法可以建立另一个方向的链接,例如在Outlook中按发送"按钮时,使该人的电子邮件地址恢复为excel? (目标是制作一个历史记录"表,以便跟踪实际发送了哪些邮件以及向谁发送了邮件)

谢谢!

解决方案

是.一个简单的情况如下所示.这是简单的骨头,展示了您请求的操作.

公共变量addressSent保留收件人"地址.对(由@Rory发送的)邮件进行布尔测试,测试已发送的邮件,并由@Dwipayan Das调用该函数,该函数将打开指定的Excel文件,并将addressSent写入工作表1中的单元格A1.

您可以对此进行修改以适合您的目的.例如.修改该函数以接受文件名作为参数.....

从@ashleedawg的书中记笔记:记住要包含xlApp.Quit行,以免Excel悬空.

我相信您的问题想从Outlook转到Excel,因此这是您将创建的需要关闭的应用程序.

因此在Outlook中运行以下代码:

将其放入标准模块中

Option Explicit
Public addressSent As String
Dim itmevt As New CMailItemEvents

Public Sub CreateNewMessage()

    Dim objMsg As MailItem

    Set objMsg = Application.CreateItem(olMailItem)

    Set itmevt.itm = objMsg

    With objMsg

        .Display
        .To = "somebody@mail.com"
        .Subject = "Blah"

        addressSent = .To

        .Send

    End With

End Sub

Public Function openExcel() As Boolean 'Adapted from @Dwipayan Das

    Dim xlApp As Object
    Dim sourceWB As Object
    Dim sourceWS As Object

    Set xlApp = CreateObject("Excel.Application")

    With xlApp

        .Visible = True
        .EnableEvents = False

    End With

    Dim strFile As String

    strFile = "C:\Users\User\Desktop\Delete.xlsb" 'Put your file path.

    Set sourceWB = xlApp.Workbooks.Open(strFile, , False, , , , , , , True)

    sourceWB.Activate

    sourceWB.Worksheets(1).Range("A1") = addressSent

End Function

然后在名为CMailItemEvents的类模块中,从

参考文献:

  1. 检查是否从Excel VBA发送了Outlook电子邮件
  2. 如何我可以使用Outlook将电子邮件发送到Excel VBA中的多个收件人吗?
  3. 使用VBA创建新的Outlook消息
  4. 项目发送后运行代码

I’m currently working on an access Vba program in order to automatically write mails to people. However we chose to still press ‘Send’ in Outlook manually (in case there are possible issues, so we can control the mail beforehand).

Is there a way to have a link in the other direction, as in, when pressing the Send button in Outlook, getting the email address of the person back in excel? (The goal would be to make a ‘history’ sheet in order to keep track of which mails were actually sent and to whom)

Thank you!

解决方案

Yes. A simple case is shown below. This is bare bones demonstrating the actions you requested.

Public variable, addressSent, holds the To address. A boolean test on mail sent (by @Rory) tests for the mail item having been sent and calls a function, by @Dwipayan Das, that opens a specified Excel file, and writes the addressSent to cell A1 in sheet1.

You can tinker with this to fit your purposes. E.g. Adapt the function to accept a file name as parameter.....

Taking a note from @ashleedawg's book: remember to include a xlApp.Quit line so Excel is not left hanging.

I believe your question wanted to go from Outlook to Excel so this is the application that you will have created that needs closing.

So in Outlook goes the following code:

Put this in a standard module:

Option Explicit
Public addressSent As String
Dim itmevt As New CMailItemEvents

Public Sub CreateNewMessage()

    Dim objMsg As MailItem

    Set objMsg = Application.CreateItem(olMailItem)

    Set itmevt.itm = objMsg

    With objMsg

        .Display
        .To = "somebody@mail.com"
        .Subject = "Blah"

        addressSent = .To

        .Send

    End With

End Sub

Public Function openExcel() As Boolean 'Adapted from @Dwipayan Das

    Dim xlApp As Object
    Dim sourceWB As Object
    Dim sourceWS As Object

    Set xlApp = CreateObject("Excel.Application")

    With xlApp

        .Visible = True
        .EnableEvents = False

    End With

    Dim strFile As String

    strFile = "C:\Users\User\Desktop\Delete.xlsb" 'Put your file path.

    Set sourceWB = xlApp.Workbooks.Open(strFile, , False, , , , , , , True)

    sourceWB.Activate

    sourceWB.Worksheets(1).Range("A1") = addressSent

End Function

Then in a class module called CMailItemEvents, code from @Rory, put the following:

Option Explicit

Public WithEvents itm As Outlook.MailItem
Private Sub itm_Close(Cancel As Boolean)
   Dim blnSent As Boolean
   On Error Resume Next
   blnSent = itm.Sent
   If Err.Number = 0 Then
      Debug.Print "not sent"
   Else
      openExcel
   End If
End Sub

References:

  1. Check to see if an Outlook Email was sent from Excel VBA
  2. How can I use Outlook to send email to multiple recipients in Excel VBA
  3. How to open an excel file in Outlook vba code
  4. Create a new Outlook message using VBA
  5. Run code after item sent

这篇关于Outlook和VBA之间的追溯链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆