【问题标题】:Add bcc address if checkbox True如果复选框为真,则添加密件抄送地址
【发布时间】:2017-07-22 18:47:39
【问题描述】:

我正在使用以下代码在 Excel for Outlook 中构建电子邮件:

Sub Mail_Selection_Range_Outlook_Body()
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    Set rng = Sheets("Volume Template").Range("K4:L14").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

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

    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC =
        .Subject = "UTS VOLUME QUOTE REQUEST"
        .HTMLBody = RangetoHTML(rng)
        .Display   'or use .Send
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

我想使用复选框(使用表单控件)来决定是否使用电子邮件。

如果复选框引用单元格为真,则将单元格(电子邮件地址)的值输入密件抄送字段。

如果单元格 H4=True,则返回单元格 F4。

向下查看大约 26 个条目的列表,因此这将需要循环,直到没有更多复选框

如何编写这段代码?

【问题讨论】:

  • 你有什么问题?
  • 如何将 VBA 代码写入 .BCC = 字段中,如果参考单元格为真,我想参考将电子邮件单元格(电子邮件地址)的值输入到密件抄送字段中.因此,如果 Cell H4=True 返回 Cell F4 并且如果 Cell H4=False 不要输入任何内容

标签: excel vba


【解决方案1】:

在你的 On Error GoTo 0 行之后添加这个

Dim bccEmail As String
If Sheets("Volume Template").Range("H4").Value = True Then
    bccEmail = Sheets("Volume Template").Range("F4").Value
Else
    bccEmail = ""
End If

然后在您的 With...End With 中将 .BCC = 替换为 .BCC = bccEmail

【讨论】:

  • 当我输入代码时,当 H4 为真时它没有返回电子邮件地址。此外,这是否适用于多个真实字段,因此如果 H5=True 返回 F5 H7=True 返回 F7 等等。
  • 我的代码已经过测试并且工作正常。你是如何在你的 SS 中写上True 的?要使其适用于多行,您需要将其放入Loop。我不知道你有多少行。
  • 这是一个复选框,如果选中,则引用单元格并输入 true 或 false。
  • 那么您在单元格H4 中有一个复选框?哪种复选框?你需要用适当的细节来问你的问题。这里的人会相应地回答。
  • 这是一个在 Insert>Form Contrls 下使用 Developer 的复选框。谢谢你,我会尝试用更好的细节来问我的问题。我对此很陌生,并且不断收到我老板的额外要求
猜你喜欢
  • 2023-03-20
  • 2018-07-14
  • 2013-10-08
  • 2017-08-28
  • 2014-07-09
  • 2014-10-28
  • 2014-04-10
  • 2018-04-07
  • 2017-01-03
相关资源
最近更新 更多