If the ID is in the subject and includes the colon as indicated then it is reasonably straightforward to extract the code from the subject, create the path, save the message with a unique name using the code below run from a rule to identify the incoming messages. I have included a test macro so that you can test it with an existing message. You will of course have to include all your country codes in the GetPath function - I have included Australia and a few other suggestions. The only part I am confused about is the ID number itself i.e. AU1612345: Which is Country (AU), Year (2016) and a four digit code, presumably '2345' - What's the extra 1 for? I have not used it in the example. I assume you can modify the code if necessary? You also had the folder separators as / rather than \. I have changed that so it will work.
The code goes in an ordinary new module in Outlook.
Option Explicit
Sub Test()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)
SaveMessage olMsg
lbl_Exit:
Exit Sub
End Sub
Sub SaveMessage(olItem As MailItem)
Dim fName As String
Dim fPath As String
fPath = GetPath(olItem.Subject)
fName = Right(fPath, 9)
fPath = Left(fPath, (Len(fPath) - 9))
fName = Replace(fName, Chr(58) & Chr(41), "")
fName = Replace(fName, Chr(58) & Chr(40), "")
fName = Replace(fName, Chr(34), "-")
fName = Replace(fName, Chr(42), "-")
fName = Replace(fName, Chr(47), "-")
fName = Replace(fName, Chr(58), "-")
fName = Replace(fName, Chr(60), "-")
fName = Replace(fName, Chr(62), "-")
fName = Replace(fName, Chr(63), "-")
fName = Replace(fName, Chr(124), "-")
CreateFolders fPath
SaveUnique olItem, fPath, fName
lbl_Exit:
Exit Sub
End Sub
Function GetPath(strSubject As String)
Dim strID As String
Dim strPath As String
Dim strCountry As String
Dim strYear As String
Dim strRange As String
Dim i As Integer
If InStr(1, strSubject, Chr(58)) > 0 Then
strID = Left(strSubject, InStr(1, strSubject, Chr(58)) - 1)
strID = Right(strID, 9)
Select Case Left(strID, 2)
Case "AU": strCountry = "Australia"
Case "US": strCountry = "USA"
Case "UK": strCountry = "United Kingdom"
Case Else: strCountry = "Unlisted"
End Select
i = Mid(strID, 6, 1)
strPath = "C:\" & strCountry & "\20" & _
Mid(strID, 3, 2) & "\" & i & "000-" & _
(i + 1) & "000\" & strID
GetPath = strPath
Else
GetPath = ""
End If
lbl_Exit:
Exit Function
End Function
Private Function SaveUnique(oItem As Object, _
strPath As String, _
strFileName As String)
'An Outlook macro by Graham Mayor
Dim lngF As Long
Dim lngName As Long
lngF = 1
lngName = Len(strFileName)
Do While FileExists(strPath & strFileName & ".msg") = True
strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
lngF = lngF + 1
Loop
oItem.SaveAs strPath & strFileName & ".msg"
lbl_Exit:
Exit Function
End Function
Private Function FileExists(filespec) As Boolean
'An Outlook macro by Graham Mayor
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filespec) Then
FileExists = True
Else
FileExists = False
End If
lbl_Exit:
Exit Function
End Function
Private Function FolderExists(fldr) As Boolean
'An Outlook macro by Graham Mayor
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(fldr)) Then
FolderExists = True
Else
FolderExists = False
End If
lbl_Exit:
Exit Function
End Function
Private Function CreateFolders(strPath As String)
'An Outlook macro by Graham Mayor
Dim strTempPath As String
Dim lngPath As Long
Dim vPath As Variant
vPath = Split(strPath, "\")
strPath = vPath(0) & "\"
For lngPath = 1 To UBound(vPath)
strPath = strPath & vPath(lngPath) & "\"
If Not FolderExists(strPath) Then MkDir strPath
Next lngPath
lbl_Exit:
Exit Function
End Function