2017-08-20 127 views
-1

請參閱下面的代碼。該代碼將在Excel VBA中,然後運行它以便在Outlook中提取大量電子郵件。Excel vba提取Outlook電子郵件 - 運行錯誤438

我有3個問題,並需要您的幫助

  1. 比方說,我的「收件箱」大約有400多個電子郵件,當我運行下面的代碼,錯誤438出來時,該代碼被搜索最多到23封電子郵件。 我該如何解決它?

  2. 請參考下面的代碼:

    Dim olMail As Variant 
    

    我應該使用 「對象」 或 「變種」?哪個更好?爲什麼?

  3. 在不久的將來,我的Outlook將有超過1個電子郵件帳戶。假設第一個電子郵件帳戶是[email protected],第二個電子郵件帳戶是[email protected]。將來,我有時需要運行第一個電子郵件帳戶的代碼,有時還需要運行第二個電子郵件帳戶的代碼。

    我該如何切換它們?

這是我當前的代碼:

Sub GetFromInbox() 
     Dim olApp As Outlook.Application 
     Dim olNs As Namespace 
     Dim Fldr As MAPIFolder 
     Dim olMail As Variant 
     Dim i, ij As Integer 
     Dim tt As Date 
     Set olApp = New Outlook.Application 
     Set olNs = olApp.GetNamespace("MAPI") 

'<Questions 2---how can I change it in here to add more 1 email address????? >  

Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 

     i = 1 
     ij = 0 
     x = Date 
     For Each olMail In Fldr.Items 
     ij = ij + 1 

      'If IsNumeric((Format(olMail.ReceivedTime, "dd/mm/yy"))) Then 

      Sheets("test").Range("a1").Select 
      Sheets("test").Range("I1").Clear 
      Sheets("test").Range("I2") = ij 

'Question 1 : while ij was counting up to 23, the error 438 stopped in here?? I have no idea why !! ---> 

Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy"))  

      Sheets("test").Range("I1").NumberFormat = "dd/mm/yy" 

      tt = Sheets("test").Range("I1") 

     ' MsgBox ("Y-tt=" & tt & " receivedtime=" & olMail.ReceivedTime) 


      'Else 
      'tt = 0 
      'MsgBox ("N-tt=" & tt & " receivedtime=" & olMail.ReceivedTime) 
      'End If 

      ' tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy")) 

    If tt >= Range("H1") Then 

     'If InStr(olMail.Subject, "others") > 0 And tt >= Range("h1") Then 
     If InStr(olMail.Subject, "others") > 0 Then 

      ActiveSheet.Range("h2") = "y" 
      ActiveSheet.Cells(i, 1).Value = olMail.Subject 
      ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime 
      ActiveSheet.Cells(i, 3).Value = olMail.SenderName 
      tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy")) 
      ActiveSheet.Cells(i, 4).Value = CDate(Format(olMail.ReceivedTime, "dd/mm/yy")) 
             ' tt = ActiveSheet.Cells(i, 4).Value 
      ActiveSheet.Cells(i, 5).Value = (Format(olMail.ReceivedTime, "hh:mm")) 

      MsgBox ("tt=" & tt) 

      i = i + 1 
     End If 

    Else 
    Sheets("test").Range("h2") = "N" 

    End If 
    Next olMail 

    Set Fldr = Nothing 
    Set olNs = Nothing 
    Set olApp = Nothing 
    'tt = "" 
    End Sub 
+1

一個問題在這個Q&A格式的時間,因爲你只能接受一個答案。未來的搜索者可能更容易找到主題相關的答案。 – niton

回答

0

運行時錯誤:438對象不支持此屬性或方法

可能的原因是olMail.ReceivedTime其中olMail不是一個對象,支持ReceivedTime。

if olMail.Class = olMail then 
    Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy")) 
end if 

這相當於:

if TypeName(olMail) = "MailItem" Then 
    Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy")) 
end if 
+0

Niton:我跟着你的代碼,但不能工作 – Bnf

+0

Niton:是否有可能告訴我如何改變它?另外,我改爲仍然不起作用!那麼如何改變它呢? – Bnf

+0

如果允許處理Sheets(「test」)。Range(「I1」)。Value =(Format(olMail.ReceivedTime,「dd/mm/yy」))以前有錯誤438,則使用If語句由於對象沒有請求mailitem屬性,繞過其他線路生成錯誤438。 – niton

相關問題