我对此很陌生。我正在尝试设置它,每当将新行项目添加到 Excel 时,它都会向 Outlook 中的一组人发送一封电子邮件

问题描述 投票:0回答:1

我尝试了 ChatGPT,但缺少重要步骤。我对此非常陌生,我公司的 IT 部门无法帮助我,因为它不是公司应用程序。在 ChatGPT 中,它说使用 VBA,但我不熟悉该语言,因为我是一名职业生物学家,略高于基本的 Excel 知识。请帮忙。

我尝试在 Excel 中使用 VBA 选项。我不明白我应该填写什么。我观看了 YouTube 视频并阅读了文章。我再次尝试了 ChatGPT。我不理解。这是他们在 ChatGPT 上给我的内容:

  1. 创建宏:编写一个在电子表格中添加新项目时触发的 VBA 宏。该宏应包含发送电子邮件的代码。当工作表中发生更改时,您可以使用

    Workbook_SheetChange
    事件触发宏。

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        ' Define your conditions for triggering the email
        If Target.Column = [Column Number] And Target.Row > [Starting Row] Then
            ' Call the function to send email
            SendEmail
        End If
    End Sub
    
    Sub SendEmail()
        ' Add your code to send email here
        ' This could involve creating an instance of Outlook and sending an email
    End Sub
    

    [Column Number]
    [Starting Row]
    替换为添加新项目的相应列号和行号。

  2. 配置 Outlook:如果您使用 Outlook 发送电子邮件,请确保在您的系统上正确配置 Outlook。

  3. 启用宏:在 Excel 中,转至

    File
    >
    Options
    >
    Trust Center
    >
    Trust Center Settings
    >
    Macro Settings
    ,然后根据您的情况选择“启用所有宏”或“为此会话启用宏”安全要求。

  4. 测试:测试宏以确保其按预期工作。您可以通过向指定的列和行添加新项目来手动触发它。

  5. 错误处理:向您的 VBA 代码添加错误处理,以处理电子邮件发送过程中可能出现的任何问题。

  6. 安全:请记住,启用宏可能会带来安全风险。确保您了解其含义并且仅启用来自可信来源的宏。

请记住将 Excel 工作簿另存为启用宏的工作簿 (.xlsm) 以保留 VBA 代码。

这些我都不懂。

excel vba outlook
1个回答
0
投票
  1. 好的,如果您在 Excel 中没有“开发人员”菜单,首先要做的就是启用“开发人员”菜单,以便您可以创建 VBA 函数:

    • 为此,您需要选择“文件”菜单

      enter image description here

    • 然后选择底部的“选项”

      enter image description here

    • 然后选择左侧的“自定义功能区”

      enter image description here

    • 然后单击“开发人员”旁边的框

      enter image description here

    • 最后选择底部的“确定”

      enter image description here

  2. 现在,您需要告诉 Visual Basic 编辑器有关 Microsoft Outlook 的信息:

    • 要执行此操作,您需要选择“开发人员”菜单

      enter image description here

    • 选择“Visual Basic”

      enter image description here

    • 然后您需要选择“工具”菜单

      enter image description here

    • 然后选择参考菜单项

      enter image description here

    • 单击 Microsoft Outlook 对象库旁边的框

      enter image description here

    • 最后点击“确定”

      enter image description here

  3. 现在将脚本添加到 Visual Basic 编辑器

    • 双击树中的“ThisWorkbook”项

      enter image description here

    • 然后在右侧窗口中输入以下脚本

      enter image description here

      脚本仅检查列,因为您说的是行项目,这意味着必须将某些内容添加到列中

      A

        Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
      
            Dim OutlookApp As Outlook.Application
            Dim OutlookMail As Outlook.MailItem
      
            If Target.Column = 1 Then
      
                Set OutlookApp = New Outlook.Application
                Set OutlookMail = OutlookApp.CreateItem(olMailItem)
      
                With OutlookMail
                    .BodyFormat = olFormatHTML
                    .HTMLBody = "Hello," & "<br>" & _
                                "<br>" & _
                                "The new data entered is: " & Target.Value & "<br>" & _
                                "<br>"
                    .To = "[email protected];[email protected]"
                    .CC = ""
                    .BCC = ""
                    .Subject = "Test mail"
                    .Send
                End With
            End If
      
            Set OutlookApp = Nothing
            Set OutlookMail = Nothing
      
        End Sub
      
  4. 现在您需要保存 Visual Basic 内容并关闭 Visual Basic 编辑器:

    • 为此,请单击“文件”菜单

      enter image description here

    • 选择顶部的“保存”选项

      enter image description here

    • 然后单击“文件”菜单

      enter image description here

    • 关闭 Visual Basic 编辑器

      enter image description here

现在,当您向电子表格的 A 列添加内容时,它会将其作为电子邮件发送。

您可能需要编辑 VBA 脚本才能获得正确的权限:

  • 电子邮件地址
  • 正文消息
  • 主题
© www.soinside.com 2019 - 2024. All rights reserved.