如果单元格包含特定文本,请删除Outlook约会

时间:2019-04-11 08:06:27

标签: excel vba outlook

如果Excel中的单元格包含单词“ No”,则我目前已设置了将约会添加到Outlook的代码。我想做的是,如果将同一单元格更改为“ N / A”,则删除现有约会。我试图为此修改一些我在其他地方找到的代码,但无法使其正常工作,目前它显示“编译错误:下一个没有for”

Sub DeleteCalendarItems()

Dim r As Long, i As Long, wb              As Workbook
Dim ws              As Worksheet
Dim objOutlook      As Outlook.Application
Dim objNamespace    As Outlook.Namespace
Dim objFolder       As Outlook.MAPIFolder
Dim objAppointment  As Outlook.AppointmentItem
Dim strSubject      As String

Set objOutlook = Outlook.Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
Set oItems = objFolder.Items
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Section 74")


r = ws.Cells(Rows.Count, 1).End(xlUp).Row 'Rows.Count should also have a reference to a wb & ws
For i = 2 To r

    If ws.Cells(i, 9) = "N/A" Then
                ws.Cells(i, 13) = "Yes"
        Set objAppointment = oItems.Item(i)
        With objAppointment
            If .Subject = strSubject Then
                objAppointment.Delete
            End If
        End With
    End If
Next i
End Sub

2 个答案:

答案 0 :(得分:0)

const availableShippingConditions = shippings.map( shipping => shipping.conditions.filter(condition => { return _.inRange( cost, condition.priceRange.min, condition.priceRange.max ); })[0] ).filter(item => { if (_.inRange(weight, item.weightRange.min, item.weightRange.max)) { return item; } else { item.cost = item.cost + (weight - item.weightRange.max) * (item.eachUnit * item.extraWeightPrice); return item; } }); WithIf语句(及更多)应始终关闭

For

答案 1 :(得分:0)

我已经设法解决了(以某种方式)-我需要添加一个嵌套的For循环

Sub DeleteNASec74()

Dim i As Long, j As Long
Dim wb              As Workbook
Dim ws              As Worksheet
Dim objOutlook      As Outlook.Application
Dim objNamespace    As Outlook.Namespace
Dim objFolder       As Outlook.MAPIFolder
Dim objAppointment  As Outlook.AppointmentItem

Set objOutlook = Outlook.Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
Set oItems = objFolder.Items
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Section 74")


r = ws.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To r
For j = oItems.Count To 1 Step -1
    If ws.Cells(i, 9).Value = "N/A" Then
    ws.Cells(i, 13) = "Yes"
        Set objAppointment = oItems.Item(j)
        With objAppointment
            If .Subject = "Send reminder email - " + ws.Cells(i, 2).Value Then
                objAppointment.Delete
            End If
        End With
    End If
Next j
Next i
End Sub