简体   繁体   English

VBA将某些数据从Outlook导出到正在运行的Excel,但什么都不产生?

[英]VBA export certain data from Outlook to Excel running but producing nothing?

Option Explicit 显式期权

Sub CopyToExcel()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim olItem As Outlook.MailItem
Dim vText As Variant
Dim sText As String
Dim vItem As Variant
Dim i As Long
Dim rCount As Long
Dim bXStarted As Boolean
Const strPath As String = "C:\Users\Rob\Documents\Excel\Excel.xlsx" 'the path of the workbook

If Application.ActiveExplorer.Selection.Count = 0 Then
    MsgBox "No Items selected!", vbCritical, "Error"
    Exit Sub
End If
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 each selected record
 rCount = xlSheet.UsedRange.Rows.Count
  For Each olItem In Application.ActiveExplorer.Selection
    sText = olItem.Body
    vText = Split(sText, Chr(13))
    'Find the next empty line of the worksheet
     rCount = rCount + 1
    'Check each line of text in the message body
    For i = UBound(vText) To 0 Step 1
      If InStr(1, vText(i), "Destination -") > 0 Then
            vItem = Split(vText(i), Chr(58))
            xlSheet.Range("a" & rCount) = Trim(vItem(1))
        End If


Next i
xlWB.Save
Next olItem
xlWB.Close SaveChanges:=True
If bXStarted Then
    xlApp.Quit
End If
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
Set olItem = Nothing
End Sub

This code is from online where I tried making it work for myself... 该代码来自在线,我曾尝试使其自己运行...

I need to extract Specific Data from emails (over 5000) and produce them on an Excel document. 我需要从电子邮件中提取特定数据(超过5000个),并在Excel文档中生成它们。 I've never touched VBA before only C#, Javascript & C++. 在C#,Javascript和C ++之前,我从未接触过VBA。

The code runs, the excel sheet updates to the current Date/time but nothing is produced? 代码运行,excel工作表更新为当前日期/时间,但是什么也没产生?

Any help please? 有什么帮助吗?

I also get an error "Subscript out of range" for this line: 我也收到此行的错误“下标超出范围”:

xlSheet.Range("A" & rCount) = Trim(vItem(1))

I think you need to change the second split delimiter to match the first one. 我认为您需要更改第二个分隔符以匹配第一个分隔符。 This will take care of the Subscript error 这将解决下标错误

Use this: 用这个:

vItem = Split(vText(i), "Destination -")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM