Hi guys,
I got the following code which will check the mailbox and extract data from them into .pdf and .txt file.
I have to amend the script so it will check the subject line for each email and it should extract data from only those emails whose subject line start with 'FORMIMAGE' and ignore the rest.
Can anyone please help me in this ? Thanks
I got the following code which will check the mailbox and extract data from them into .pdf and .txt file.
I have to amend the script so it will check the subject line for each email and it should extract data from only those emails whose subject line start with 'FORMIMAGE' and ignore the rest.
Can anyone please help me in this ? Thanks
Code:
Public Declare Function GetPrivateProfileStringA Lib "Kernel32" (ByVal strSection As String, _
ByVal strKey As String, ByVal strDefault As String, ByVal strReturnedString As String, _
ByVal lngSize As Long, ByVal strFileNameName As String) As Long
Public Sub Extract()
On Error Resume Next
Set myOlApp = Outlook.Application
Set mynamespace = myOlApp.GetNamespace("mapi")
Dim objFS As New Scripting.FileSystemObject
Dim objFile As Scripting.TextStream
Dim FilePath As String
Dim sFilePath As String
Dim fileNumber As Integer
Dim strRowData As String
Dim strDelimiter As String
Dim myDestFolder As Outlook.Folder
Dim olRecip As Outlook.Recipient
Dim ShareInbox As Outlook.MAPIFolder
Dim SubFolder As Object
Dim j As Integer
Dim m As String
Dim InputF As String
Dim OutputP As String
Dim TestMail As String
Dim strSource As String
Dim strUserID As String
Dim sFileName As String
Dim sAccountNo As String
Dim strDocType As String
Dim strSubject As String
m = GetPrivateProfileString32("C:\test.ini", "SaveFolder", "FolderName")
TestMail = GetPrivateProfileString32("C:\test.ini", "TestMailbox", "Mailbox")
InputP = GetPrivateProfileString32("C:\test.ini", TestMail, "InputFolder")
OutputP = GetPrivateProfileString32("C:\test.ini", TestMail, "CompleteFolder")
strRowData = ""
' Code to extract emails from specific subfolder within shared folder
Set olRecip = mynamespace.CreateRecipient(TestMail)
Set ShareInbox = mynamespace.GetSharedDefaultFolder(olRecip, olFolderInbox) ' Look into Inbox emails
Set SubFolder = ShareInbox.Folders(InputP) 'Change this line to specify folder
Set myDestFolder = ShareInbox.Folders(OutputP)
strUserID = Environ$("username")
For I = 1 To SubFolder.Items.Count
messageArray = ""
strRowData = ""
Set myItem = SubFolder.Items(1)
msgtext = Trim(myItem.Body)
'search for specific text
delimtedMessage = Replace(Trim(msgtext), "Name of Borrower(s)", "###")
delimtedMessage = Replace(Trim(delimtedMessage), "Account Number(s)", "###")
delimtedMessage = Replace(Trim(delimtedMessage), "Property Address", "###")
delimtedMessage = Replace(delimtedMessage, "Contact Telephone Number", "###")
delimtedMessage = Replace(delimtedMessage, "Requested start date of payment break", "###")
delimtedMessage = Replace(delimtedMessage, "My employment status is", "###")
messageArray = Split(delimtedMessage, "###")
For j = 1 To 11
strRowData = Replace(Replace(Replace(Trim(strRowData & Trim(messageArray(j)) & "|"), vbCr, ""), vbLf, " "), vbTab, "")
Next j
strRowData = Replace(strRowData, " " & vbCrLf, vbCrLf)
strNo = Split(strRowData, "|")
strAccNo = strNo(1)
sFileName = "abc"
sFilePath = m & sFileName & ".txt"
' Create .txt file
Set objFile = objFS.CreateTextFile(sFilePath, False)
With objFile
.WriteLine strRowData
End With
' Create .pdf file of the selected email item
Dim objDoc As Object, objInspector As Object
Set objInspector = myItem.GetInspector
Set objDoc = objInspector.WordEditor
objDoc.ExportAsFixedFormat m & sFileName & ".pdf", 17
Set objInspector = Nothing
Set objDoc = Nothing
' move email to complete folder after processing
myItem.Move myDestFolder
Next I
objFile.Close
End Sub
Public Function GetPrivateProfileString32(ByVal strFileName As String, ByVal strSection As String, ByVal strKey As String, Optional strDefault) As String
Dim strReturnString As String, lngSize As Long, lngValid As Long
On Error Resume Next
If IsMissing(strDefault) Then strDefault = ""
strReturnString = Space(2048)
lngSize = Len(strReturnString)
lngValid = GetPrivateProfileStringA(strSection, strKey, strDefault, strReturnString, lngSize, strFileName)
GetPrivateProfileString32 = Left(strReturnString, lngValid)
' On Error GoTo 0
End Function