joflo
New Member
- Outlook version
- Outlook 2013 32 bit
- Email Account
- Exchange Server
Hi All,
I'm very green with all this vba coding but ive managed to cobble some script together after reading a lot on this forum.
I have some questions.
1. is it possible to enter the current date and time in one of the case pattern elements?
(so that after it fills up all the relevant columns it enters a date stamp in the last column eg "F")
2. is it possible that if the pattern returns a null reading it will use a default text string ie. "no data"
3. how do I gather the information from the email information ie received date, sender, etc.
thanks in advance, loving all the information.
#######code#######
Option Explicit
Sub ACKtoExcel(olItem As Outlook.MailItem)
'Dim olItem As Outlook.MailItem
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim vText, vText2, vText3, vText4, vText5 As Variant
Dim sText As String
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object
Dim i As Long
Dim strCell As String
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Documents\Test123456.xlsx"
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("Sheet2")
' use this line if your using and active view
'Set olItem = Application.ActiveExplorer().Selection(1)
'Find the next empty line of the worksheet
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
rCount = rCount + 1
sText = olItem.Body
Set Reg1 = CreateObject("VBScript.RegExp")
For i = 1 To 6
With Reg1
Select Case i
Case 1
.Pattern = "(Incident Number\s*[:](.*))\n"
.Global = False
strCell = ("B")
Case 2
.Pattern = "(Engineer\s*Assigned\s*[:](.*))\n"
.Global = False
strCell = ("C")
Case 3
.Pattern = "(ETA\s*[:](.*))\n"
.Global = False
strCell = ("D")
Case 4
.Pattern = "(Part Assigned\s*[:](.*))\n"
.Global = False
strCell = ("E")
Case 5
.Pattern = "(Serial number\s*[:](.*))\n"
.Global = False
strCell = ("F")
Case 6
.Pattern = "(Part\s*to\s*site\s*via\s*[:](.*))\n"
.Global = False
strCell = ("G")
End Select
End With
If Reg1.Test(sText) Then
' each "(\w*)" and the "(\d)" are assigned a vText variable
Set M1 = Reg1.Execute(sText)
For Each M In M1
vText = Trim(M.SubMatches(1))
'vText2 = Trim(M.SubMatches(2))
'vText3 = Trim(M.SubMatches(3))
xlSheet.Range(strCell & rCount) = vText
'xlSheet.Range(strCell & rCount) = vText2
'xlSheet.Range(strCell & rCount) = vText3
Next
End If
Next i
xlWB.Close 1
If bXStarted Then
xlApp.Quit
End If
Set M = Nothing
Set M1 = Nothing
Set Reg1 = Nothing
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
End Sub
I'm very green with all this vba coding but ive managed to cobble some script together after reading a lot on this forum.
I have some questions.
1. is it possible to enter the current date and time in one of the case pattern elements?
(so that after it fills up all the relevant columns it enters a date stamp in the last column eg "F")
2. is it possible that if the pattern returns a null reading it will use a default text string ie. "no data"
3. how do I gather the information from the email information ie received date, sender, etc.
thanks in advance, loving all the information.
#######code#######
Option Explicit
Sub ACKtoExcel(olItem As Outlook.MailItem)
'Dim olItem As Outlook.MailItem
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim vText, vText2, vText3, vText4, vText5 As Variant
Dim sText As String
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object
Dim i As Long
Dim strCell As String
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Documents\Test123456.xlsx"
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("Sheet2")
' use this line if your using and active view
'Set olItem = Application.ActiveExplorer().Selection(1)
'Find the next empty line of the worksheet
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
rCount = rCount + 1
sText = olItem.Body
Set Reg1 = CreateObject("VBScript.RegExp")
For i = 1 To 6
With Reg1
Select Case i
Case 1
.Pattern = "(Incident Number\s*[:](.*))\n"
.Global = False
strCell = ("B")
Case 2
.Pattern = "(Engineer\s*Assigned\s*[:](.*))\n"
.Global = False
strCell = ("C")
Case 3
.Pattern = "(ETA\s*[:](.*))\n"
.Global = False
strCell = ("D")
Case 4
.Pattern = "(Part Assigned\s*[:](.*))\n"
.Global = False
strCell = ("E")
Case 5
.Pattern = "(Serial number\s*[:](.*))\n"
.Global = False
strCell = ("F")
Case 6
.Pattern = "(Part\s*to\s*site\s*via\s*[:](.*))\n"
.Global = False
strCell = ("G")
End Select
End With
If Reg1.Test(sText) Then
' each "(\w*)" and the "(\d)" are assigned a vText variable
Set M1 = Reg1.Execute(sText)
For Each M In M1
vText = Trim(M.SubMatches(1))
'vText2 = Trim(M.SubMatches(2))
'vText3 = Trim(M.SubMatches(3))
xlSheet.Range(strCell & rCount) = vText
'xlSheet.Range(strCell & rCount) = vText2
'xlSheet.Range(strCell & rCount) = vText3
Next
End If
Next i
xlWB.Close 1
If bXStarted Then
xlApp.Quit
End If
Set M = Nothing
Set M1 = Nothing
Set Reg1 = Nothing
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
End Sub