Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 检查outlook中是否存在附件并显示数据_Excel_Vba_Outlook - Fatal编程技术网

Excel 检查outlook中是否存在附件并显示数据

Excel 检查outlook中是否存在附件并显示数据,excel,vba,outlook,Excel,Vba,Outlook,下面的代码用于从Outlook中的任何文件夹提取电子邮件数据,并将这些数据显示在Excel文件中 数据将显示发件人姓名、发件人电子邮件地址、主题和接收时间 但是,代码是否有办法检测电子邮件是否有附件,并在另一个Excel列中显示电子邮件中是否有附件 以下为代码附件: Option Explicit Sub ExportDataToExcel() Dim xlApp As Object Dim xlWB As Object Dim xlSheet As Object Dim rC

下面的代码用于从Outlook中的任何文件夹提取电子邮件数据,并将这些数据显示在Excel文件中

数据将显示发件人姓名、发件人电子邮件地址、主题和接收时间

但是,代码是否有办法检测电子邮件是否有附件,并在另一个Excel列中显示电子邮件中是否有附件

以下为代码附件:

    Option Explicit
 Sub ExportDataToExcel()
 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 olItem As Outlook.MailItem
 Dim obj As Object
 Dim strColA, strColB, strColC, strColD As String


Dim currentExplorer As Outlook.NameSpace
Dim Selection As Outlook.MAPIFolder
' Get Excel set up
     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 a specific workbook to input the data ============
'the path of the workbook under the windows user account
enviro = CStr(Environ("USERPROFILE"))
 strPath = enviro & "\Desktop\New folder\OutlookItems.xlsx"
     Set xlWB = xlApp.Workbooks.Open(strPath)
     Set xlSheet = xlWB.Sheets("Sheet1")
'================== End Specific workbook ====================

'=================== Use New Workbook ========================
'Set xlWB = xlApp.Workbooks.Add
'Set xlSheet = xlWB.Sheets("Sheet1")
'================== end use new workbook =====================

' Add column names
  xlSheet.Range("A1") = "SENDER"
  xlSheet.Range("B1") = "SENDER ADDRESS"
  xlSheet.Range("C1") = "MESSAGE SUBJECT"
  xlSheet.Range("D1") = "RECEIVED TIME"
  xlSheet.Range("A1").Interior.Color = RGB(0, 255, 255)
  xlSheet.Range("B1").Interior.Color = RGB(0, 255, 255)
  xlSheet.Range("C1").Interior.Color = RGB(0, 255, 255)
  xlSheet.Range("D1").Interior.Color = RGB(0, 255, 255)


' Process the message record

  On Error Resume Next
'Find the next empty line of the worksheet
rCount = xlSheet.Range("A" & xlSheet.Rows.Count).End(-4162).Row
'needed for Exchange 2016. Remove if causing blank lines.
rCount = rCount + 1

' get the values from outlook
Set currentExplorer = Application.GetNamespace("MAPI")
Set Selection = currentExplorer.PickFolder
  For Each obj In Selection.Items

    Set olItem = obj

 'collect the fields
    strColA = olItem.SenderName
    strColB = olItem.SenderEmailAddress
    strColC = olItem.Subject
    strColD = olItem.ReceivedTime

'================== Get all recipient addresses ===================
' instead of To names
Dim strRecipients As String
Dim Recipient As Outlook.Recipient
For Each Recipient In olItem.Recipients
 strRecipients = Recipient.Address & "; " & strRecipients
 Next Recipient



'================== end all recipients addresses ==================

'==================== Get the Exchange address ====================
' if not using Exchange, this block can be removed
 Dim olEU As Outlook.ExchangeUser
 Dim oEDL As Outlook.ExchangeDistributionList
 Dim recip As Outlook.Recipient
 Set recip = Application.Session.CreateRecipient(strColB)

If InStr(1, strColB, "/") > 0 Then
' if exchange, get smtp address
    Select Case recip.AddressEntry.AddressEntryUserType
       Case OlAddressEntryUserType.olExchangeUserAddressEntry
         Set olEU = recip.AddressEntry.GetExchangeUser
         If Not (olEU Is Nothing) Then
             strColB = olEU.PrimarySmtpAddress
         End If
       Case OlAddressEntryUserType.olOutlookContactAddressEntry
         Set olEU = recip.AddressEntry.GetExchangeUser
         If Not (olEU Is Nothing) Then
            strColB = olEU.PrimarySmtpAddress
         End If
       Case OlAddressEntryUserType.olExchangeDistributionListAddressEntry
         Set oEDL = recip.AddressEntry.GetExchangeDistributionList
         If Not (oEDL Is Nothing) Then
            strColB = olEU.PrimarySmtpAddress
         End If
     End Select
End If
' ==================== End Exchange section =====================

'write them in the excel sheet
  xlSheet.Range("A" & rCount) = strColA ' sender name
  xlSheet.Range("B" & rCount) = strColB ' sender address
  xlSheet.Range("C" & rCount) = strColC ' message subject
  xlSheet.Range("D" & rCount) = strColD ' recieved time

'Next row
  rCount = rCount + 1

' size the cells
    xlSheet.Columns("A:D").EntireColumn.AutoFit
    xlSheet.Columns("C:C").ColumnWidth = 100
    xlSheet.Range("A2").Select
    xlSheet.Columns("A:D").VerticalAlignment = xlTop

 Next
 xlApp.Visible = True

' to save but not close
'xlWB.Save

' to save and close
'     xlWB.Close 1
'     If bXStarted Then
'         xlApp.Quit
'     End If
' end save and close

     Set olItem = Nothing
     Set obj = Nothing
     Set currentExplorer = Nothing
     Set xlSheet = Nothing
     Set xlWB = Nothing
     Set xlApp = Nothing
 End Sub
选项显式
子ExportDataToExcel()
将xlApp作为对象
作为对象的Dim xlWB
将图纸作为对象
暗计数等于长
Dim bx以布尔形式开始
Dim enviro As字符串
将strPath设置为字符串
将我设置为Outlook.MailItem
作为对象的Dim obj
尺寸strColA、strColB、strColC、strColD为字符串
将currentExplorer设置为Outlook.NameSpace
将所选内容暗显为Outlook.Mapi文件夹
'设置Excel
出错时继续下一步
Set xlApp=GetObject(,“Excel.Application”)
如果错误为0,则
Application.StatusBar=“正在打开Excel源,请稍候…”
设置xlApp=CreateObject(“Excel.Application”)
bXStarted=True
如果结束
错误转到0
'==========打开特定工作簿以输入数据============
'windows用户帐户下工作簿的路径
enviro=CStr(环境(“用户档案”))
strPath=enviro&“\Desktop\New folder\OutlookItems.xlsx”
设置xlWB=xlApp.Workbooks.Open(strPath)
设置xlSheet=xlWB.Sheets(“Sheet1”)
'============================特定于结束的工作簿====================
'============================使用新工作簿========================
'设置xlWB=xlApp.Workbooks.Add
'Set xlSheet=xlWB.Sheets(“Sheet1”)
'============================最终使用新工作簿=====================
'添加列名
xlSheet.Range(“A1”)=“发送方”
xlSheet.Range(“B1”)=“发件人地址”
xlSheet.Range(“C1”)=“消息主题”
xlSheet.Range(“D1”)=“接收时间”
xlSheet.Range(“A1”).Interior.Color=RGB(0,255,255)
xlSheet.Range(“B1”).Interior.Color=RGB(0,255,255)
xlSheet.Range(“C1”).Interior.Color=RGB(0,255,255)
xlSheet.Range(“D1”).Interior.Color=RGB(0,255,255)
'处理消息记录
出错时继续下一步
'查找工作表的下一个空行
rCount=xlSheet.Range(“A”&xlSheet.Rows.Count)。结束(-4162)。行
“2016年交易所需要。如果导致空行,请删除。
rCount=rCount+1
'从outlook获取值
设置currentExplorer=Application.GetNamespace(“MAPI”)
设置选择=currentExplorer.PickFolder
对于选择项中的每个对象
设置m=obj
"收田,
strColA=olItem.SenderName
strColB=olItem.SenderEmailAddress
strColC=受试者
strColD=olItem.ReceivedTime
'======================获取所有收件人地址===================
"而不是指名道姓,
作为字符串的Dim strRecipients
将收件人设置为Outlook。收件人
对于m.Recipients中的每个收件人
strRecipients=收件人。地址&“;”&strRecipients
下一个收件人
'=========================结束所有收件人地址==================
'============================获取交换地址====================
'如果不使用Exchange,则可以删除此块
Dim olEU作为Outlook.ExchangeUser
将oEDL设置为Outlook.ExchangeDistributionList
将recip设置为Outlook.Recipient
Set recip=Application.Session.CreateRecipient(strColB)
如果InStr(1,strColB,“/”)大于0,则
'如果是exchange,则获取smtp地址
选择Case recip.AddressEntry.AddressEntryUserType
案例OlAddressEntryUserType.olExchangeUserAddressEntry
设置olEU=recip.AddressEntry.GetExchangeUser
如果不是(olEU什么都不是),那么
strColB=olEU.PrimarySmtpAddress
如果结束
案例OlAddressEntryUserType.olOutlookContactAddressEntry
设置olEU=recip.AddressEntry.GetExchangeUser
如果不是(olEU什么都不是),那么
strColB=olEU.PrimarySmtpAddress
如果结束
案例OlAddressEntryUserType.OlexchangeDistributionListAddressessEntry
设置oEDL=recip.AddressEntry.GetExchangeDistributionList
如果不是(oEDL什么都不是),那么
strColB=olEU.PrimarySmtpAddress
如果结束
结束选择
如果结束
“=====================================结束交换部分=====================
'将它们写在excel表格中
xlSheet.Range(“A”&rCount)=strColA的发送方名称
xlSheet.Range(“B”&rCount)=strColB的发件人地址
xlSheet.Range(“C”&rCount)=strColC消息主题
xlSheet.Range(“D”和rCount)=strColD的接收时间
“下一排
rCount=rCount+1
“调整单元格大小
xlSheet.Columns(“A:D”).entireclumn.AutoFit
xlSheet.Columns(“C:C”)。ColumnWidth=100
xlSheet.范围(“A2”)。选择
xlSheet.Columns(“A:D”)。垂直对齐=xlTop
下一个
xlApp.Visible=True
“保存但不关闭
'xlWB.Save
“保存并关闭
'xlWB.Close 1
“如果BX启动了
'xlApp.退出
"完"
'结束保存并关闭
设置m=无
Set obj=无
设置currentExplorer=Nothing
Set xlSheet=无
设置xlWB=Nothing
设置xlApp=Nothing
端接头
如果m.Attachments.Count>0,只需使用
,然后strColE=“YES”

范例

然后将
xlSheet.Range(“E”&rCount)=strColE'附件添加到


当然,检查一下我正在寻找的thx人(如果olItem.Attachments.Count>0,那么strColE=“YES”),所有这些时间间隔几秒钟:-)
    '================== end all recipients addresses ==================

    ' check for attachment
    Dim strColE As String
    If olItem.Attachments.Count > 0 Then strColE = "YES"

    '==================== Get the Exchange address ====================
    'write them in the excel sheet
    xlSheet.Range("A" & rCount) = strColA ' sender name
    xlSheet.Range("B" & rCount) = strColB ' sender address
    xlSheet.Range("C" & rCount) = strColC ' message subject
    xlSheet.Range("D" & rCount) = strColD ' recieved time

    xlSheet.Range("E" & rCount) = strColE ' Attament