私はパブリックフォルダに受信した添付ファイルのファイル名を抽出し、簡単な分析のためにExcelファイルに抽出(貼り付け)します。選択した電子メールでエクスポートされたOutlook添付ファイルのファイル名(または拡張子のみ)がExcelに必要です
私は以下のコードを持っていますが、1つの電子メールの詳細しか選択していません。
どこが間違っているかを理解したいと思います。
Option Explicit
Sub CopyToExcel()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim currentExplorer As Explorer
Dim Selection As Selection
Dim olItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Dim j As Long
Dim i As Integer
Dim Report As String
Dim attachment As attachment
Dim obj As Object
Dim strColB, strColC, strColD, strColE, strColF As String
' Get Excel set up
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Documents\test.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
' Process the message record
On Error Resume Next
'Find the next empty line of the worksheet
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
' get the values from outlook
Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection
For Each obj In Selection
Set olItem = obj
Set myAttachments = olItem.Attachments
'collect the fields
Next
For Each Selection In Selection
If Selection.Class = olMail Then
End If
For Each attachment In olItem.Attachments
Report = strColC & GetAttachmentInfo(attachment)
strColB = olItem.Attachments.Count
strColD = olItem.SenderEmailAddress
strColE = olItem.Categories
strColF = olItem.ReceivedTime
'write them in the excel sheet
xlSheet.Range("B" & rCount) = strColB
xlSheet.Range("c" & rCount) = Report
xlSheet.Range("d" & rCount) = strColD
xlSheet.Range("e" & rCount) = strColE
xlSheet.Range("f" & rCount) = strColF
'Next row
rCount = rCount + 1
Next
Next
xlWB.Close 1
If bXStarted Then
xlApp.Quit
End If
Set olItem = Nothing
Set obj = Nothing
Set currentExplorer = Nothing
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
End Sub
Public Function GetAttachmentInfo(attachment As attachment)
On Error GoTo On_Error
Dim Report
Dim strColA, strColB, strColC, strColD, strColE, strColF As String
GetAttachmentInfo = ""
Report = strColA & "Display Name: " & attachment.DisplayName
Report = strColC & "File Name: " & attachment.filename
GetAttachmentInfo = Report
Exiting:
Exit Function
On_Error:
MsgBox "error=" & Err.Number & " " & Err.Description
Resume Exiting
End Function
(1)一貫してインデントした場合、マクロをデバッグする方がはるかに簡単です。 (2)myAttachmentsの添付ファイルを出力するループは、myAttachmentsを設定するループ内にないことがわかります。 'For each attachment in olItem.Attachments'を' Set myAttachments = olItem.Attachments'の下に移動する必要があります。 (3)添付ファイルにアクセスする前に、アイテムに添付ファイルがあるかどうかチェックしないでください。私はこのコードが 'olItem.Attachments.Count = 0'で失敗すると思います。 –
こんにちはトニー、あなたは素晴らしいです!私は多くのことをデバッグし解決することができます、問題は時間の不足です、私は操作から来て、多くの操作活動も管理しなければなりません。私は物事をより簡単にし、それでvbaマクロを試してみようとしています。本当にありがとうございました。私は再び問題に遭遇している、私は要求に応じて変更を加えたが、電子メールが1つ以上の添付ファイルを持っている場合、添付ファイルのリストの一番上にあるファイル名を1つだけ取得する。名前? –