我想包括在其中確定在的次數後,會出現一個句子中的所有文本的Excel VBA腳本行:在身體的「關鍵詞」多個電子郵件並將每個逗號分隔的單詞複製到單獨的Excel單元格中。短語可以是任何東西,總是一個單詞但不能被預定義。例如,電子郵件包含這樣一行:Excel的VBA - 從電子郵件複製逗號分隔句子單獨的Excel單元格
Keyword: phrase1, phrase2, phrase3, phrase4
結果,在Excel中:
First email: A1 phrase1 B1 phrase2 etc.
Second email: A2 phrase1 B2 phrase2 etc.
我試圖使用類似以下,但不知道從哪裏裏去:
CreateObject("VBScript.RegExp").Pattern = "((Keyword:)\s*(\w*),\s*(\w*),\s*(\w*),\s*(\w*),\s*(\w*))"
這是我到目前爲止有:
Option Compare Text
Sub Count_Emails()
Dim oNS As Outlook.Namespace
Dim oTaskFolder As Outlook.MAPIFolder
Dim oItems As Outlook.Items
Dim oFoldToSearch As Object
Dim intCounter As Integer
Dim oWS As Worksheet
Dim dStartDate, dEnddate As Date
Set oWS = Sheets("Sheet1")
Set oNS = GetNamespace("MAPI")
Set oTaskFolder = oNS.Folders("[email protected]")
Set oFoldToSearch = oTaskFolder.Folders("Inbox").Folders("New Folder")
Set oItems = oFoldToSearch.Items
intCounter = 1
dStartDate = oWS.Range("A1").Value
dEnddate = oWS.Range("B1").Value
Do
With oWS
If DateSerial(Year(oItems(intCounter).ReceivedTime), Month(oItems(intCounter).ReceivedTime), Day(oItems(intCounter).ReceivedTime)) >= dStartDate And _
DateSerial(Year(oItems(intCounter).ReceivedTime), Month(oItems(intCounter).ReceivedTime), Day(oItems(intCounter).ReceivedTime)) <= dEnddate And _
oItems(intCounter).Subject Like "*Keyword:*" Then
'Something needs to happen here? A VBScript.RegExp.Pattern maybe?
End If
End With
intCounter = intCounter + 1
Loop Until intCounter >= oItems.Count + 1
Set oNS = Nothing
Set oTaskFolder = Nothing
Set oItems = Nothing
End Sub
編輯:要澄清的是,短語未預先定義的,它們可以是任何東西。
EDIT2:澄清的是,郵件正文包含「關鍵詞:」接着逗號分隔單個單詞分別被複制到自己的Excel單元格。
我認爲你正在尋找oItems.body。將變量聲明爲variant,並使其等於消息正文。然後,您可以使用instr掃描它,找到您要查找的關鍵字,然後拔出分隔的字符串。 – Hrothgar