【发布时间】:2014-10-16 18:54:03
【问题描述】:
我编写了一个代码来将数据从 Outlook 提取到 excel,并且它 80% 的工作:) 它确实提取信息但不是从整个电子邮件中提取。
我收到格式相同的电子邮件,其中包含价格和其他信息。这些用于通常有超过 1 行的采购订单。它们采用以下格式:
货品编号:00001
供应商销售订单号:
供应商物料编号:
SAP 物料编号:
供应商描述:
SAP 描述:
供应商数量:30.000 EA
SAP 数量:30.000 EA
数量计量单位:EA
供应商交货日期:20.09.2014
SAP 交付日期 : 20.09.2014
行动请求:
以下详细信息与采购订单行项目 00001 不匹配
供应商价格:1 个 EA 0.00 美元
SAP 价格:1 个 EA 0.01 美元
货品编号:00002
供应商销售订单号:
供应商物料编号:
SAP 物料编号:
供应商描述:
SAP 描述:
供应商数量:70.000 EA
SAP 数量:70.000 EA
数量计量单位:EA
供应商价格:1 个 EA 3.90 美元
SAP 价格:1 个 EA 3.90 美元
供应商交货日期:20.09.2014
SAP 交付日期 : 20.09.2014
行动请求:
数量和请求日期都与采购订单匹配。项目 00002
从代码中可以看出,我从这些电子邮件中提取了多个具有相同开头字符串的内容。在它拉出第 1 行后,代码将移至下一封电子邮件,而无需搜索整个电子邮件正文以查找更多匹配项。我怎样才能解决这个问题?卡住了:)
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 = "Excel filepath here" '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
For Each olItem In Application.ActiveWindow.Selection
sText = olItem.Body
vText = Split(sText, Chr(13))
'Find the next empty line of the worksheet
rCount = xlSheet.UsedRange.Rows.Count + 1
'Check each line of text in the message body
For i = UBound(vText) To 0 Step -1
rCount = rCount
If InStr(1, vText(i), "Purchase Order :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("A" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Vendor :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("B" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Item Number :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("C" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Vendor Quantity :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("D" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "SAP Quantity :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("E" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Quantity UOM :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("F" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Vendor Price :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("G" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "SAP Price :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("H" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Vendor Delivery Date :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("I" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "SAP Delivery Date :") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("J" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Text here:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("K" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Text here:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("L" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Text here:") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("M" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Text here") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("N" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Text here") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("O" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Text here") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("P" & rCount) = Trim(vItem(1))
End If
If InStr(1, vText(i), "Text here") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("Q" & rCount) = Trim(vItem(1))
End If
Next i
xlWB.Save
Next olItem
xlWB.Close SaveChanges:=True
If bXStarted Then
End If
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
Set olItem = Nothing
End Sub
【问题讨论】:
-
你确定它只是拉第一个还是最后一个?
-
它会拉出第 1 行项目,然后不会在电子邮件的其余部分中搜索与该字符串匹配的其他可能匹配项。其中一些有 10 多个行项目,这仅将第 1 行导出到 excel
标签: excel vba email outlook extract