对象必需的VBA Outlook

时间:2013-05-07 13:00:44

标签: vba outlook outlook-vba

我不确定它在这里对我的代码不喜欢什么,我在.NET方面很有经验,但VBA对我来说是新手。我知道调用函数时不要执行myFunction('args')而是执行myFunction args,但我没有这个问题。任何帮助表示赞赏。谢谢!

Public Sub LogMeIn()
Dim item As Outlook.MailItem
Dim body As String
Dim subject As String
Dim oFld As Outlook.Folder
Dim oNS As Outlook.NameSpace
Dim oMails As Outlook.items
Dim oProp As Outlook.PropertyPage
Dim mySelection As Word.Selection
Dim strItem As String
Dim omailitem As Variant

Set oNS = Application.GetNamespace("MAPI")
Set oFld = oNS.GetDefaultFolder(olFolderInbox)
Set oMails = oFld.items

For Each omailitem In oMails
Set body = omailitem.body
Set subject = omailitem.subject
Dim pos As Integer
Set pos = 0
Dim copyText As String
If InStr(omailitem.subject, "Your LogMeIn Security Code:") > 0 Then
Set copystr = Mid(omailitem.body, pos + 28, 9)
Dim dataToSave As New DataObject
dataToSave.SetText copystr
dataToSave.putinclipboard

'MsgBox ("subject true")
End If

'MsgBox ("subject true")
'If omailitem.subject.Find("Your LogMeIn Security Code:") Then
'MsgBox ("subject true")
'End If

Next

End Sub

Private Sub Application_NewMail()
Call LogMeIn
End Sub

1 个答案:

答案 0 :(得分:4)

您尝试将对象引用分配给数据类型字符串是VBA中的数据类型,而不是对象。关键字Set与对象一起使用。在数据类型中删除此关键字,否则您将收到错误。

Dim body As String
Dim subject As String

Set body = omailitem.body
Set subject = omailitem.subject

Dim pos As Integer
Set pos = 0

[...]