添加字符串到电子邮件主题IF不存在

时间:2018-04-26 20:09:08

标签: vba outlook-vba

我尝试了下面的代码并获得了编译错误:olMail上的无效限定符。 我附上了错误的图片。好吧所以它说我需要添加问题的更多细节,所以详细说明,如果该字符串尚未存在,我想在outlook的主题行的开头添加一个字符串。如果字符串已经存在,我不想更改它:

Sub Addstring()
Dim myolApp As Outlook.Application
Dim aItem As Object

Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder

Dim iItemsUpdated As Integer
Dim strTemp As String
Dim strFilenum As String

strname = InputBox("Enter the string to add to subject i.e John ")
iItemsUpdated = 0
For Each aItem In mail.Items
   If Left(LCase(olMail.Subject), 10) <> "(strname)" Then
    ' edit the subject
     strTemp = "[" & strFilenum & "] " & aItem.Subject
      aItem.Subject = strTemp
      iItemsUpdated = iItemsUpdated + 1
    aItem.Save
End If
Next aItem

MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"
Set myolApp = Nothing
End Sub

错误:

error

1 个答案:

答案 0 :(得分:-1)

您需要更正变量的名称。它被声明为aItem,但稍后您尝试使用未在任何地方定义的olMail对象。

Sub Addstring()
  Dim myolApp As Outlook.Application
  Dim aItem As Object

  Set myolApp = CreateObject("Outlook.Application")
  Set mail = myolApp.ActiveExplorer.CurrentFolder

  Dim iItemsUpdated As Integer
  Dim strTemp As String
  Dim strFilenum As String

  strname = InputBox("Enter the string to add to subject i.e John ")
  iItemsUpdated = 0
  For Each aItem In mail.Items
    If Left(LCase(aItem.Subject), 10) <> "(strname)" Then
      ' edit the subject
      strTemp = "[" & strFilenum & "] " & aItem.Subject
      aItem.Subject = strTemp
      iItemsUpdated = iItemsUpdated + 1
      aItem.Save
    End If
  Next aItem

  MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"
  Set myolApp = Nothing
End Sub