我不确定我的代码有什么不喜欢的地方,我在 .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
最佳答案
您尝试将对象 引用分配给数据类型。 String 是 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
[...]
关于vba - 对象所需的 VBA Outlook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16419727/