3

我不确定我在这里的代码有什么不喜欢的地方,我在 .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
4

1 回答 1

5

您尝试将对象引用分配给数据类型字符串是 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

[...]
于 2013-05-07T13:11:07.997 回答