Vba 将字符串添加到电子邮件主题(如果尚未存在)
我尝试了下面的代码,得到了一个编译错误:olMail上的限定符无效。 我附上了一张错误的照片。好的,它说我需要添加问题的更多细节,所以在细节上,我想在outlook中主题行的开头添加一个字符串,如果该字符串尚未出现。如果字符串已经存在,我不想更改它:Vba 将字符串添加到电子邮件主题(如果尚未存在),vba,outlook,Vba,Outlook,我尝试了下面的代码,得到了一个编译错误:olMail上的限定符无效。 我附上了一张错误的照片。好的,它说我需要添加问题的更多细节,所以在细节上,我想在outlook中主题行的开头添加一个字符串,如果该字符串尚未出现。如果字符串已经存在,我不想更改它: Sub Addstring() Dim myolApp As Outlook.Application Dim aItem As Object Set myolApp = CreateObject("Outlook.Application") Se
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
Sub Addstring()
将myOLAP设置为Outlook.Application
作为对象的Dim aItem
设置myolApp=CreateObject(“Outlook.Application”)
设置邮件=myolApp.ActiveExplorer.CurrentFolder
Dim IITEMSUPPATED为整数
将strTemp设置为字符串
作为字符串的Dim strFilenum
strname=InputBox(“输入要添加到主题的字符串,即John”)
IITEMSUPPATED=0
对于mail.Items中的每个aItem
如果左(LCase(olMail.Subject),10)”(strname)”则
'编辑主题
strTemp=“[”&strFilenum&“]”和aItem.Subject
aItem.Subject=strTemp
IITEMSUPPATED=IITEMSUPPATED+1
艾特姆,救命
如果结束
下一个艾特姆
MsgBox IITEMSUPPATED&“of”&mail.Items.Count&“已更新邮件”
设置myolApp=Nothing
端接头
错误:
您需要更正变量的名称。它被声明为
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
Sub Addstring()
将myOLAP设置为Outlook.Application
作为对象的Dim aItem
设置myolApp=CreateObject(“Outlook.Application”)
设置邮件=myolApp.ActiveExplorer.CurrentFolder
Dim IITEMSUPPATED为整数
将strTemp设置为字符串
作为字符串的Dim strFilenum
strname=InputBox(“输入要添加到主题的字符串,即John”)
IITEMSUPPATED=0
对于mail.Items中的每个aItem
如果左(LCase(aItem.Subject),10)“strname”,则
'编辑主题
strTemp=“[”&strFilenum&“]”和aItem.Subject
aItem.Subject=strTemp
IITEMSUPPATED=IITEMSUPPATED+1
艾特姆,救命
如果结束
下一个艾特姆
MsgBox IITEMSUPPATED&“of”&mail.Items.Count&“已更新邮件”
设置myolApp=Nothing
端接头
感谢您的回复!我的下一个问题是“如果”测试似乎没有正确测试。如果(Thisword)已经在主题的开头,即…(Thisword)这是一个测试主题,那么主题变成(Thisword)(Thisword)这是一个测试主题。我尝试了转义(没有更改)。我尝试了更改(如果parens是问题的话,则更改为),结果相同。我正在考虑只检查(作为第一个字符)。当我在if之前添加Dim firstChar=str.first()时,if是红色的,而在for循环中,如果firstChar是红色的,则它们是红色的Dim firstChar=str.first()(那么这是另一个问题。我建议在另一个线程中分别提问,以便读者能够轻松识别问题和答案。同时,您可能会发现和页面很有帮助。