Excel VBA收件人添加基于相关单元格

Excel VBA收件人添加基于相关单元格,vba,excel,Vba,Excel,我接受了这段代码()并自己添加了strRecipient字段。我是一个彻头彻尾的VBA noob,很明显,它不起作用。有没有人能给我一个建议,比如如何添加一个从A4单元格自动反馈的收件人部分 谢谢 Option Explicit Dim bWeStartedOutlook As Boolean Function AddToTasks(strDate As String, strText As String, DaysOut As Integer, strRecipient As String

我接受了这段代码()并自己添加了strRecipient字段。我是一个彻头彻尾的VBA noob,很明显,它不起作用。有没有人能给我一个建议,比如如何添加一个从A4单元格自动反馈的收件人部分

谢谢

Option Explicit

Dim bWeStartedOutlook As Boolean

Function AddToTasks(strDate As String, strText As String, DaysOut As Integer, strRecipient As String) As Boolean
    ' Adds a task reminder to Outlook Tasks a specific number of days before the date specified
    ' Returns TRUE if successful
    ' Will not trigger OMG because no protected properties are accessed
    ' by Jimmy Pena, http://www.jpsoftwaretech.com, 10/30/2008
    '
    ' Usage:
    ' =AddToTasks("12/31/2008", "Something to remember", 30)
    ' or:
    ' =AddToTasks(A1, A2, A3)
    ' where A1 contains valid date, A2 contains task information, A3 contains number of days before A1 date to trigger task reminder
    '
    ' can also be used in VBA :
    'If AddToTasks("12/31/2008", "Christmas shopping", 30) Then
    '  MsgBox "ok!"
    'End If

Dim intDaysBack As Integer
Dim dteDate As Date
Dim olApp As Object ' Outlook.Application
Dim objTask As Object ' Outlook.TaskItem

' make sure all fields were filled in
If (Not IsDate(strDate)) Or (strText = "") Or (DaysOut <= 0) Or (strRecipient = "") Then
  AddToTasks = False
  GoTo ExitProc
End If

' We want the task reminder a certain number of days BEFORE the due date
' ex: if DaysOut = 120, then we want the due date to be -120 before the date specified
' we need to pass -120 to the NextBusinessDay function, so to go from 120 to -120,
' we subtract double the number (240) from the number provided (120).
' 120 - (120 * 2); 120 - 240 = -120

intDaysBack = DaysOut - (DaysOut * 2)

dteDate = CDate(strDate) + intDaysBack

On Error Resume Next
  Set olApp = GetOutlookApp
On Error GoTo 0

If Not olApp Is Nothing Then
  Set objTask = olApp.CreateItem(3)  ' task item

    With objTask
        .StartDate = dteDate
        .Subject = strText & ", due on: " & strDate
        .ReminderSet = True
        .Recipients.Add = strRecipient
        .Save
        .Assign
        .Send
    End With

Else
  AddToTasks = False
  GoTo ExitProc
End If

' if we got this far, it must have worked
AddToTasks = True

ExitProc:
If bWeStartedOutlook Then
  olApp.Quit
End If
Set olApp = Nothing
Set objTask = Nothing
End Function

Function GetOutlookApp() As Object

On Error Resume Next
  Set GetOutlookApp = GetObject(, "Outlook.Application")
  If Err.Number <> 0 Then
    Set GetOutlookApp = CreateObject("Outlook.Application")
    bWeStartedOutlook = True
  End If
On Error GoTo 0

End Function
选项显式
Dim bWeStartedOutlook为布尔值
函数AddToTasks(strDate作为字符串,strText作为字符串,DaysOut作为整数,strRecipient作为字符串)作为布尔值
'在指定日期之前的特定天数向Outlook任务添加任务提醒
'如果成功,则返回TRUE
'将不会触发OMG,因为未访问受保护的属性
吉米·佩纳,http://www.jpsoftwaretech.com, 10/30/2008
'
'用法:
“=AddToTasks(“12/31/2008”,“需要记住的东西”,30)
“或:
'=附加任务(A1、A2、A3)
'其中A1包含有效日期,A2包含任务信息,A3包含A1日期之前触发任务提醒的天数
'
'也可在VBA中使用:
“如果AddTotask(“12/31/2008”,“圣诞购物”,30)那么
“MsgBox”好的
"完"
Dim intDaysBack为整数
标注日期
Dim olApp作为对象的Outlook.Application
将对象任务设置为对象的Outlook.TaskItem
'确保所有字段都已填写

如果(不是IsDate(strDate))或(strText=“”)或(DaysOut在对象任务的
之前添加以下内容

strRecipient = Sheets("sheet name here").Range("A4").Value


您如何调用此函数?这是一个按钮吗?
A4
中的收件人是否总是在该单元格中,或者您引用的单元格是否会更改?由于您没有将任何内容返回到调用过程中,将其作为子例程而不是函数是否更有意义?现在,我使用公式调用它(=附加任务(A1、A2、A3、A4)),最终它很可能是一个按钮。我将引用的单元格将发生更改。不幸的是,这并没有解决我的问题,因为它仍然给我相同的#值!错误。还有其他想法吗?@tgarafa已经更改了
工作表名称
?Hi@Omar我确实这样做了。我现在正在虚拟工作表上使用这一切,所以我将其更改为“Sheet1”仍然不走运。换成
.CC=strRecipient
strRecipient = Sheets("sheet name here").Range("A4").Value
With objTask
    .startdate = dteDate
    .CC = strRecipient
    .Subject = strText & ", due on: " & strDate
    .ReminderSet = True
    .Save
    .Assign
    .Send
End With