多数の項目を実行しているときに一見無作為な点で機能するマクロがあります。マクロは、エラーログを受け取る受信ボックスフォルダをループするために使用され、エラーログテキストファイルを保存し、添付ファイルからテキストの特定の行をコピーし(エラー操作名など)、これらの文字列をExcelファイルに配置して追跡し、一度処理された別の受信トレイフォルダに電子メールアイテムを移動します。それは、それが100以上の電子メールを通過するときにうまくいくが、それは奇妙になる。テストでは、第122回反復、648,350などで失敗しました。一般的な構造は以下の通りです。大きな項目セットでマクロが失敗する
Sub ErrorLogAuto()
Dim FileName As String
Dim Path As String
Dim TimeInfo As String
Dim SubjectInfo As String
Dim IdNumber As String
Dim Dataline As String
Dim oItem As Object
Dim Item As Outlook.Items
Dim myAttachment(1000) As Outlook.Attachments
Dim myInspector As Outlook.Inspector
Dim appExcel As Object
Dim FileNum As Integer
Dim found As Integer
Dim found1 As Integer
Dim found2 As Integer
Dim i As Integer
Dim j As Integer
Dim op As Integer
Dim us As Integer
Dim cdata As Integer
i = 0
k = 1
'Returns proper SOURCE folder
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myNewFolder = myFolder.Folders("Test") '--> text between "" is the folder name, only change it here
'set path for attachments to be saved in
Path = "C:\test\"
'Set item = to all emails in test folder
Set Item = myNewFolder.Items
'If no emails...
If Item.Count = 0 Then
MsgBox "There are no error messages to sift through."
Exit Sub
End If
'Open an instance of excel to certain workbook
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = True
'appExcel.Workbooks.Open (Path & "test.xlsx")
appExcel.Workbooks.Open (Path & "SAMPLE FILE NAME.xlsx")
'Find first empty cell to write to --> based off of column D
While appExcel.Range("D" & k) <> ""
k = k + 1
Wend
'For every email in folder...here starts the big loop
For Each oItem In Item
'Save attachment and set filename
Set myAttachment(i) = oItem.Attachments
myAttachment(i).Item(1).SaveAsFile Path & myAttachment(i).Item(1).DisplayName & ".txt"
FileName = Path & myAttachment(i).Item(1).DisplayName & ".txt"
'Subject and time info
SubjectInfo = oItem.Subject
TimeInfo = oItem.ReceivedTime
'Returns ID number from subject string after '@'
j = InStr(SubjectInfo, "@")
IdNumber = Mid(SubjectInfo, j + 1)
'Write IdNumber to cell and timestamp
appExcel.Range("A" & k) = TimeInfo
appExcel.Range("D" & k) = IdNumber
'Open the notepad file, read line by line until EOF, take user message, and take operation name
FileNum = FreeFile()
Open FileName For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, Dataline
'If string found these will <> 0
found = InStr(Dataline, "<OperationName>")
found1 = InStr(Dataline, "<UserMessage>")
found2 = InStr(Dataline, "<UserMessage><![CDATA[")
'Returns position right after where string is found
op = InStr(Dataline, "<OperationName>") + 15
us = InStr(Dataline, "<UserMessage>") + 13
cdata = InStr(Dataline, "<UserMessage><![CDATA[") + 22
'Found operation name line
If found <> 0 Then
'appExcel.Range("B1") = Dataline --> whole line
'appExcel.Range("C" & k) = Mid(Mid(Dataline, 20), 1, Len(Mid(Dataline, 20)) - 16) --> doesnt account for whitespace
appExcel.Range("N" & k) = Mid(Mid(Dataline, op), 1, Len(Mid(Dataline, op)) - 16) '--> accounts for whitespace and cuts out <OperationName> and <\OperationName>
'Found user message line and it includes cdata stuff
ElseIf found1 <> 0 And found2 <> 0 Then
'appExcel.Range("C1") = Dataline --> whole line
'appExcel.Range("D" & k) = Mid(Mid(Dataline, 20), 1, Len(Mid(Dataline, 20)) - 14) --> doesnt account for whitespace
'appExcel.Range("O" & k) = Mid(Mid(Dataline, us), 1, Len(Mid(Dataline, us)) - 14) --> accounts for whitespace and cuts out <UserMessage> and <\UserMessage>
appExcel.Range("O" & k) = Mid(Mid(Dataline, cdata), 1, Len(Mid(Dataline, cdata)) - 17) '--> accounts for whitespace and cuts out <UserMessage><![CDATA[ and ]]><\UserMessage>
'Found user message line WITHOUT cdata stuff
ElseIf found1 <> 0 Then
appExcel.Range("O" & k) = Mid(Mid(Dataline, us), 1, Len(Mid(Dataline, us)) - 14) '--> accounts for whitespace and cuts out <UserMessage> and <\UserMessage>
End If
Wend
Close #FileNum
i = i + 1
k = k + 1
Next
Call FolderMove
End Sub
Private Sub FolderMove()
Dim a As MailItem
Dim m As Integer
Dim Source As MAPIFolder
Dim Destination As MAPIFolder
Set Source = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set Source = Source.Folders("Test") '--> text between "" is the folder name, only change it here
Set Destination = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set Destination = Destination.Folders("Testing Done") '--> text between "" is the folder name, only change it here
For m = Source.Items.Count To 1 Step -1
Set a = Source.Items(m)
a.move Destination
Next
End Sub
コードは、EOFループではないファイルを読み取っている間に分解されます。これらのエラーはプログラミングの習慣が悪いために発生しますか?私は前に大きなセットで作業したことはありませんし、VBAに新しいですので、どんな助けも高く評価されます。
エラー情報:実行時エラー '50290':アプリケーション定義またはオブジェクト定義のエラー。 - > 363回目の繰り返しで発生しました
デバッグ時に再起動し、同じ方法で失敗する前に540に達しました。
その後、再起動してOKになりました。
これで私の質問はなぜこれが起こるのですか?
を、それは我々が持っていないとき、それは悪い習慣だ場合と言うのは難しいですあなたが本当に大きなインスタンスを持っていないか、またはすべてのアイテムに関するいくつかのデータを全体としてコンパイルしようとしているのであれば、それはおそらく悪いことではありません。 – litelite
excelファイルは共有フォルダにありますか? – litelite
そのローカルコピー – mmoschet