Bob
Bob

Reputation: 85

Object Required VBA Outlook

I'm not sure what it doesn't like about my code here, I'm experienced in .NET, but VBA is new to me. I know when called functions not to do myFunction('args') and do myFunction args instead, but I don't have that issue here. Any help is appreciated. THanks!

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

Upvotes: 3

Views: 3811

Answers (1)

Adrien Lacroix
Adrien Lacroix

Reputation: 3612

You try to assign object reference to data type. String is a data type in VBA and not an object. The keyword Set is used with object. Remove this keyword when it's data type or you will get an error.

Dim body As String
Dim subject As String

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

Dim pos As Integer
Set pos = 0

[...]

Upvotes: 5

Related Questions