【发布时间】:2013-01-31 00:26:49
【问题描述】:
我在 Windows 7 上的 Excel 2010 中制作并测试了此宏,还使用另一台 Windows 7 计算机进行了测试,但使用的是 Excel 2007。两者都可以使用,但是当我尝试在我的工作计算机上使用它时(Windows 7、Excel 2007)我在第一个“下一个”语句中得到“类型不匹配错误”。查找并发现我可以使用“Exit For”而不是“next”,但它只是抱怨包含“End If”的下一行。现在它声称“End If without block If”。我想我只是无法理解这在一台 Win7\Excel 2007 计算机上是如何工作的,而在另一台计算机上却不是。
该宏只是在电子邮件主题中搜索选定单元格的值(如果该单元格尚未着色),如果匹配,它会更改该单元格的颜色。
Sub MultipleCellSubjectSearch()
'This macro searches for the selected cell values (if there is no cell color), when it finds a match it turns the cell color yellow
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olItem As MailItem
Dim olInbox As Outlook.MAPIFolder
Dim olFolder As Outlook.MAPIFolder
Dim oCell As Range
'The following sets the Outlook folder to search
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olInbox = olNamespace.GetDefaultFolder(olFolderInbox)
'The following searches for cell value string in subject
For Each oCell In Selection
If oCell.Interior.Pattern = xlNone Then
For Each olItem In olInbox.Items
If InStr(olItem.Subject, (oCell.Value)) <> 0 Then
oCell.Interior.ColorIndex = 6
End If
Next
End If
Next
Set olInbox = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
End Sub
如果有人有任何想法,将不胜感激。
【问题讨论】:
-
您在两台机器上使用的 Outlook 版本是否不同?您是否包含正确的参考资料?我怀疑这更像是一个“环境”问题,而不是 Excel 中的差异......
-
顺便说一句...
Exit For是提早跳出循环的好方法——但您仍然需要“常规”结束循环。所以这不是“用Exit For替换Next的情况,而是更多:For... do stuff... if something then exit for... do more stuff... Next。有意义吗? -
两台 Excel 2007 机器都在 Windows 7 Pro 上运行 Outlook 2007。两者都包含完全相同的参考资料。我想详细说明一下,我需要找到一种方法让它在我的“公司”计算机上工作。我想这是我可以忽略的一些简单的改变。
标签: excel vba types foreach mismatch