2014-09-25 118 views
0

當電子郵件進入Outlook中的某個子文件夾時,我在ThisOutlookSession的以下代碼可以保存來自電子郵件的PDF附件。Outlook 2010 VBA宏保存附件

我以爲我沒有正確使用Initialize Handler,但我試圖改變它,無濟於事。

Public WithEvents myOlItem As Outlook.Items 

Dim myOlApp As New Outlook.Application 

Public Sub Initialize_handler() 
Set myOlItem = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("WAM").Folders("UNPROCESSED").Items 
End Sub 

Private Sub myOlItem_ItemAdd(ByVal Item As Object) 
Dim myOlMItem As Outlook.MailItem 
Dim myOlAtts As Outlook.Attachments 
Set myOlAtts = myOlMItem.Attachments 

Call CallMyProcedure(Item) 

End Sub 

Sub CallMyProcedure() 

Dim itms As Outlook.Items 
Dim Itm As Object 

' loop through default Inbox items 
Set itms = myOlMItem 'Session.GetDefaultFolder(olFolderInbox).Folders("WAM").Folders("UNPROCESSED").Items 

For Each Itm In itms 
    If TypeName(Itm) = "MailItem" Then 
     ' your code is called here 
     savePDFtoDisk Itm 
    End If 
Next Itm 
Set objEmail = Nothing 
End Sub 

Sub savePDFtoDisk(Itm As Outlook.MailItem) 

Dim dateFormat 'Dateiname mit Datum. 
Dim objAtt As Outlook.Attachment 
Dim saveFolder As String 

dateFormat = Format(Now, "mm_yyyy") 
saveFolder = "\\marnv006\#marnv006\Bm\Master Scheduling\PC 2.2.11 Work Authorizing Memorandum (WAMs)\WAMS added to WAM Track\" 

For Each objAtt In Itm.Attachments 

    If (InStr(1, objAtt.DisplayName, "WAM", vbTextCompare) > 0) Then 

     If LCase(Right(objAtt.FileName, 4)) = ".pdf" Then 
      objAtt.SaveAsFile saveFolder & objAtt.DisplayName 

      Set objAtt = Nothing 

     End If 'Nach PDF filtern. 
    End If 
Next 

End Sub 
+1

我不確定還有什麼可能是錯的,但乍一看我看到你正在傳遞一個參數到CallMyProcedure中,沒有人期待。 – Dave 2014-09-25 15:22:48

+0

*我不明白爲什麼它不起作用,請幫忙!*如果您能詳細說明這意味着什麼,對我們會有所幫助。你有錯誤嗎?如果是這樣,哪一行會引發錯誤,錯誤信息是什麼? – 2014-09-25 15:32:56

回答

0

替換子Application_Startup()

行子Initialize_handler()或使用此格式

Sub Application_Startup() 
    Initialize_handler 
End Sub 

編輯2015 11 16

代碼太費解。重新確定受影響的項目,而不是將它們傳遞給它們。

Option Explicit 

' In ThisOutlookSession 
Private WithEvents myOlItem As Items 

' Not needed if in Outlook 
'Dim myOlApp As New Outlook.Application 

'Public Sub Initialize_handler() 
Private Sub application_Startup() 

Dim myNS As Namespace 
Dim myFolder As Folder 

Set myNS = GetNamespace("MAPI") 

Set myFolder = myNS.GetDefaultFolder(olFolderInbox) 
Set myFolder = myFolder.Folders("WAM") 
Set myFolder = myFolder.Folders("UNPROCESSED") 

Set myOlItem = myFolder.Items 

ExitRoutine: 
    Set myNS = Nothing 
    Set myFolder = Nothing 

End Sub 

' No need to redetermine items, ItemAdd already knows. 

' Note itm to match the savePDFtoDisk code, not item. 
Private Sub myOlItem_ItemAdd(ByVal Itm As Object) 
'Sub savePDFtoDisk(Itm As Outlook.mailItem) 

Dim dateFormat 'Dateiname mit Datum. 
Dim objAtt As Outlook.attachment 
Dim saveFolder As String 

dateFormat = Format(Now, "mm_yyyy") 
saveFolder = "\\marnv006\#marnv006\Bm\Master Scheduling\PC 2.2.11 Work Authorizing Memorandum (WAMs)\WAMS added to WAM Track\" 

For Each objAtt In Itm.Attachments 

    If (InStr(1, objAtt.DisplayName, "WAM", vbTextCompare) > 0) Then 

     If LCase(Right(objAtt.Filename, 4)) = ".pdf" Then 
      objAtt.SaveAsFile saveFolder & objAtt.DisplayName 

      Set objAtt = Nothing 

     End If 'Nach PDF filtern. 
    End If 
Next 

End Sub