1

当我解析一封看起来像这样的电子邮件时:

包括_po~301993959 'VBCRLF

po_no~vendor~part_no~class~type_code~buyer~qty_ordered~measure~balance_on_order~cost~amt_ordered~order_date~delivery_date~ship_date~receive_date~open~wo_no 'VBCRLF

301993959~100000~88008K~PROBE 800~F~VAX~4.0~EA~4.0~100.3300~401.32000~011513~012313~012313~000000~Y~STOCK'VBCRLF 301993959~100000~SPTEFCI~010WIRE ~VAX~1.0~SP~1.0~12.6400~12.64000~011513~012313~012313~000000~Y~STOCK 'VBCRLF 301993959~100000~TFIR-010-50~WIRE SPTEF~F~VAX~1.0~SP~1.0~12.6400 ~12.64000~011513~012313~012313~000000~Y~STOCK 'VBCRLF

使用代码:

Public Sub AddItems()

Dim aLineItem() As String
Dim aItem() As String
Dim i As Integer
Dim j As Integer
Dim iCnt As Double
Dim msg
Dim Item As Items

ReDim sWo(0)
iCnt = 0
For i = 1 To UBound(sMsg())

aLineItem = Split(sMsg(i), vbCrLf)
For j = 1 To UBound(aLineItem)
    If aLineItem(j) <> "" Then
        If blah = 1 Then
            Debug.Print ("...." & aLineItem(j))
        End If
        aItem = Split(aLineItem(j), "~")
        If (aItem(0) <> "") And (aItem(0) <> "included_po") And (aItem(0) <> "po_no") Then
          Item.PO_num = GetWo(aItem(1))
            If Item.PO_num <> "0" Then
                Item.Company = aItem(1)
                Item.Delivery_date = aItem(12)
                Item.pn = aItem(2)
                Item.QTY_ordered = aItem(6)
                Item.Unit_Price = aItem(9)
            End If 'If Item.PO_num <> "0" Then
        End If 'If aItem(1) <> "" Then
    End If ' If aLineItem(j) <> "" Then
Next j
Next i
End Sub

电子邮件解析如下所示:

....301993959~100000~88008K~探针

....8800~F~VAX~4.0~EA~4.0~100.3300~401.32000~011513~012313~012313~000000~Y~S

....托克

有没有更好的方法来解析这封电子邮件?

编辑#1:

Public Sub GetMailMsg()
Dim pop3 As jmail.pop3
Dim iCount As Integer
Dim i As Integer
Dim mailID As Integer
Dim j As Integer
Dim sSubject As String

j = 0

ReDim sMsg(0)

'connect to the mail box
Set pop3 = New pop3
pop3.Connect "REMOVED FOR SECURITY"


'Get message count
iCount = pop3.Count


'Read Messages
For i = 1 To iCount
 sSubject = pop3.Messages.Item(i).Subject
Label1.Caption = "Reading message.." & sSubject
DoEvents
If InStr(sSubject, "China Purchase Orders") <> 0 Then 'email test
j = j + 1
ReDim Preserve sMsg(j)
sMsg(j) = pop3.Messages.Item(i).Body
Label1.Caption = "Reading mail message for order " & sSubject
If blah = 1 Then
    Debug.Print ("Reading mail message for order " & sSubject)
    Debug.Print ("Reading mail message for order " & sMsg(j))
End If
End If
Next

pop3.Disconnect
Set pop3 = Nothing
End SUb

--编辑#2

调试输出:阅读订单中国采购订单的邮件消息阅读订单包含的邮件_po~301993959

po_no~vendor~part_no~class~type_code~buyer~qty_ordered~measure~balance_o 'VBCRLF

n_order~cost~amt_ordered~order_date~delivery_date~ship_Date~receive_date 'VBCRLF

~open~wo_no 'VBCRLF

301993959~100000~88008K~探针'VBCRLF

8800~F~VAX~4.0~EA~4.0~100.3300~401.32000~011513~012313~012313~000000~Y~S'VBCRLF

托克'VBCRLF

301993959~100000~TFCI-010-50~WIRE 'VBCRLF

SPTEF~F~VAX~1.0~SP~1.0~12.6400~12.64000~011513~012313~012313~000000~Y~ST 'VBCRLF

奥克'VBCRLF

301993959~100000~TFIR-010-50~WIRE 'VBCRLF

SPTEF~F~VAX~1.0~SP~1.0~12.6400~12.64000~011513~012313~012313~000000~Y~ST 'VBCRLF

奥克'VBCRLF

4

2 回答 2

3

你的台词没有正确分解让我觉得台词有问题aLineItem = Split(sMsg(i), vbCrLf)。请检查您vbCrLF是否没有正确的行中的任何内容。

那条线是一条连续的线吗?

编辑 #1 @ 2013 年 1 月 16 日美国东部标准时间下午 1:16:

我创建了一个文件并粘贴了提供的输入。我创建了一个简单的程序并将您的代码合并到其中。请参阅下面生成的 DEBUG,如预期的那样,由 3 行组成,以 301993959 开头,以 STOCK 结尾。这是正确且理想的输出,对吗?

....301993959~100000~TFCI-010-50~WIRE SPTEF~F~VAX~1.0~SP~1.0~12.6400~12.64000~011513~012313~012313~000000~Y~STOCK ....301993959~100000~TFIR-010-50~WIRE SPTEF~F~VAX~1.0~SP~1.0~12.6400~12.64000~011513~012313~012313~000000~Y~STOCK ....301993959~100000~88008K~PROBE 800~F~VAX~4.0 ~EA~4.0~100.3300~401.32000~011513~012313~012313~000000~Y~STOCK

所以你提供的代码工作正常。如果您说输入文件也很好,那意味着问题在于您如何阅读该电子邮件,特别是您如何填充sMsg(). 您可以发布该代码吗?

编辑#2 @ 2013 年 1 月 16 日美国东部标准时间下午 2:32:

您在单个记录行中得到了一些不需要vbCrLf的 s,并且您无法按照您想要的方式对其进行拆分。这是我建议您执行的操作:

1)如果您可以修改GetMailMsg为而不是按原样加载Body,请逐行进行以避免生成不需要vbCrLf的s。我对流行对象的经验为零,所以我不知道这是否可能。

2)如果不需要cbCrLf的 s 的数量是一致的,例如:单个记录中的 2,那么您可以简单地调整代码以连接这样的拆分记录

DIM concatLine As String
For j = 1 To UBound(aLineItem) Step 3
    concatLine = aLineItem(j) & aLineItem(j+1) & aLineItem(j+2)
    If concatLine <> "" Then
        If blah = 1 Then
            Debug.Print ("...." & concatLine)
        End If
        aItem = Split(concatLine, "~")
        If (aItem(0) <> "") And (aItem(0) <> "included_po") And (aItem(0) <> "po_no") Then
          Item.PO_num = GetWo(aItem(1))
            If Item.PO_num <> "0" Then
                Item.Company = aItem(1)
                Item.Delivery_date = aItem(12)
                Item.pn = aItem(2)
                Item.QTY_ordered = aItem(6)
                Item.Unit_Price = aItem(9)
            End If 'If Item.PO_num <> "0" Then
        End If 'If aItem(1) <> "" Then
    End If ' If concatLine <> "" Then
Next j

3)或者更好的是,连接字符串直到到达记录的末尾,然后拆分该连接的字符串。

DIM concatLine As String
DIM detailsRecord as Integer

For detailsRecord  = 1 To UBound(aLineItem)
    if LCase(Right(aLineItem(detailsRecord),5)) = 'wo_no' Then Exit For ' when we find this tag, we know where details record begin
Next detailsRecord 
For j = detailsRecord + 1 To UBound(aLineItem) ' begin looping detail records
    concatLine = concatLine + aLineItem(j)

    If UCase(Right(aLineItem(j), 5)) = "STOCK" Then ' this is your end of the record indicator
        If blah = 1 Then
            Debug.Print ("...." & concatLine)
        End If
        aItem = Split(concatLine, "~")
        If (aItem(0) <> "") Then
          Item.PO_num = GetWo(aItem(1))
            If Item.PO_num <> "0" Then
                Item.Company = aItem(1)
                Item.Delivery_date = aItem(12)
                Item.pn = aItem(2)
                Item.QTY_ordered = aItem(6)
                Item.Unit_Price = aItem(9)
            End If 'If Item.PO_num <> "0" Then
        End If 'If aItem(1) <> "" Then
    End If ' If concatLine <> "" Then
    concatLine = ""
Next j

对于情况 2 和 3,不要忘记先处理标题 - 您必须移出 If (aItem(0) <> "") And (aItem(0) <> "included_po") And (aItem(0) <> "po_no")主循环以处理标题记录(我相信前 2 个或记录是标题)。

编辑#3: 我修复了场景#3以跳过标题记录(假设'wo_no'是该记录结束的指示符),然后通过搜索结束标签(“STOCK” )。此方法将足够灵活,可以处理vbCrLf电子邮件正文中的动态 s 数量,将单个记录拆分为不可预测数量的字符串。

代码是在浏览器中输入的,所以我不保证它会起作用:)

于 2013-01-16T17:02:18.170 回答
0

既然你知道每一行应该从什么开始,那么使用“301993959”使用拆分功能。

Dim LineItems(100) As String
x = Split(sMsg(i), "301993959")
For j = 1 To UBound(x) - 1 'Don't use 0 becuase you don't need that part
      LineItems(j - 1) = x(j)
Next i

当然,您可以更进一步,然后使用“~”作为第二个分隔符拆分字符串中的每个项目。

Dim LineItems(100, 15) As String
x = Split(Text1.Text, "301993959")
For j = 1 To UBound(x) - 1 'Don't use 0 becuase you don't need that part
  y = Split(x(j), "~")
  For k = 1 To 15
    LineItems(j - 1, k) = y(k)
  Next k
Next j
于 2013-01-19T07:06:21.920 回答