2013-09-11 36 views
0

我在Microsoft Outlook VBA中創建了一個子例程來創建一個csv文件,其中列出了我的收件箱及其子文件夾中的所有消息。當我看着我的收件箱時,我得到了這個宏,但是當我試着循環瀏覽子文件夾時,它返回了一個「錯誤的文件名錯誤」,引用了我正在嘗試寫入的文件。所以基本上processFolder函數中的「Write #ff」行會導致錯誤。有什麼我錯過了文件名#ff的範圍嗎?感謝Outlook VBA中錯誤的文件名錯誤

子Response_Log()

Dim myItem As Object 
Dim msg As MailItem 
Dim myFolder As Folder 
Dim mysubfolder As Outlook.Folder 
Dim myNamespace As NameSpace 
Set myNamespace = Application.GetNamespace("MAPI") 
Set myFolder = myNamespace.Folders("My mailbox").Folders("Inbox") 

Dim resFile As String 
    resFile = "myfilepath\Response_Log.csv" 
Dim ff As Byte 
    ff = FreeFile() 
Open resFile For Append As #ff 
Write #ff, "Sender Name"; "Sender Email Address"; "Received Time"; "Subject"; "Response Status and Response Time" 
processFolder myFolder 

Close #ff 

結束子

功能processFolder(oParent作爲Outlook.MAPIFolder)

Dim oFolder As Outlook.MAPIFolder 
    Dim oMail As Object 
    Dim Respond As String 
    Dim NoRespond As String 
     NoRespond = "Not Yet Replied" 


    For Each oMail In oParent.Items 
     If TypeOf oMail Is MailItem Then 
      If Mid(oMail.SenderEmailAddress, 1, 3) <> "/O=" Then 
       If oMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003") = 102 Or oMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003") = 103 Then 
        Respond = "Replied on " & oMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10820040") 
        Write #ff, oMail.SenderName; oMail.SenderEmailAddress; oMail.ReceivedTime; oMail.Subject; Respond 
       Else       
        Write #ff, oMail.SenderName; oMail.SenderEmailAddress; oMail.ReceivedTime; oMail.Subject; NoRespond 
       End If 
      End If 
     End If 
    Next 

    If (oParent.Folders.Count > 0) Then 
     For Each oFolder In oParent.Folders 
      processFolder oFolder 
     Next 
    End If 

端功能

回答

0

文件編號ff似乎定義爲0123,即在Response_Log,您正在嘗試使用它在processFolder。這是不會有好下場:-)

你可能最好關閉改變processFolder所以它接受一個額外的參數,文件編號寫入。

然後確保您通過它,從Response_Log和從processFolder本身的遞歸調用。例如:

Sub Response_Log() 
    : 
    processFolder myFolder,ff 
: 
Function processFolder(oParent As Outlook.MAPIFolder,ff as Byte) 
    : 
    processFolder oFolder,ff 

您可能還需要檢查這些調用的語法。自從我做VBA以來已經有一段時間了,但使用可選的call語句和括號可能會出現一些問題。它可能需要更多的東西一樣:

call processFolder (oFolder,ff)