Search code examples
vbaoutlookoutlook-2016

outlook2016 VBA rule run script error 'The script "" doesn't exist or is invalid'


as in title, i tried to run my script with a rule in outlook the program worked find on its own but once i linked it with a rule it keeps showing this error, what's wrong? i've tried evrything i could, re opening outlook, reset rules etcetc but it's still not working. it always just shows 'The script "" doesn't exist or is invalid'. here is my code below

Sub FolderCreation(oLookMail As Outlook.MailItem)

Const tFFolderPath As String = "R:\xxxxxxx"
Dim oLookItem As Object
Dim oLookFldr As Folder
Dim folderObj, newFldr As Outlook.Folder
Dim oLookName As NameSpace
Dim yearStr, dummyFolderName, tFSubFolderPath As String
Dim Quarter
mailDate = oLookMail.ReceivedTime
Quarter = CStr((Month(mailDate) + 2) \ 3)
yearStr = CStr(year(mailsDate))
dummyFolderName = yearStr & "Q" & Quarter
trailerFeeSubFolderPath = tFFolderPath & yearStr & "Q" & Quarter
Debug.Print dummyFolderName

'Create Folder On Drive
With CreateObject("Scripting.FileSystemObject")
    If Not .FolderExists(tFSubFolderPath) Then .CreateFolder tFSubFolderPath
End With

'Setup
Set oLookName = Application.GetNamespace("MAPI")
Set oLookFldr = oLookName.Folders("[email protected]")

'Create Folder in Outlook
existvalue = False
For Each folderObj In oLookFldr.Folders
    If folderObj.Name = "XYXY" Then existvalue = True
    Next
If existvalue = False Then
    Set newFldr = oLookFldr.Folders.Add("XYXY")
End If

Set oLookFldr = oLookName.Folders("[email protected]").Folders("XYXY")

'Create Subfolder under XYXY
existvalue = False
For Each folderObj In oLookFldr.Folders
    If folderObj.Name = dummyFolderName Then existvalue = True
    Next
If existvalue = False Then
    Set newFldr = oLookFldr.Folders.Add(dummyFolderName)
End If

'Create Folder in Drive (public function that works)
CreateFHFolders (dummyFolderName)
Dim kwFull As String
Dim keyWord() As String
kwFull = "aaa,bbb,ccc"
keyWord = Split(kwFull, ",")
Dim fHName As String
Dim saveAttPath As String
Dim fileName As String
If oLookMail.Attachments.Count > 0 Then
    For Each kw In keyWord
        If InStr(oLookMail.Subject, kw) > 0 Then
            Select Case kw
            'Mapping
            Case "aaa"
                fHName = "KKK"
            Case "bbb"
                fHName = "HHH"
            Case "ccc"
                fHName = "JJJ"
            End Select
            saveAttPath = tFFolderPath & dummyFolderName & "\" & fHName & "\"
            For Each Att In oLookMail.Attachments
                fileName = Att.fileName
                Att.SaveAsFile saveAttPath & fileName
            Next
        End If
    Next
End If

End Sub

it is a module.


Solution

  • Try to declare the sub as public in the following way:

    Public Sub FolderCreation(oLookMail As Outlook.MailItem)
    

    If it doesn't work you need to place the VBA macro to the ThisOutlookSession module to be discoverable.

    Read more about that in the Outlook's Rules and Alerts: Run a Script article.