Hello!
I am using Microsoft Outlook 2013 and I have a rule created in it with a Visual Basic Script that runs every time I Send/Receive.
Recently, it started showing me an error
An unexpected error occurred
and I am unable to understand why.
I am pasting my VB Script for reference.
Please help.
Regards
Aman Chaurasia
What I have tried:
Function FileExists(FilePath As String) As Boolean
Dim TestStr As String
TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
If TestStr = "" Then
FileExists = False
Else
FileExists = True
End If
End Function
Sub Ftp_Download(MyMail As MailItem)
Dim objFS As New Scripting.FileSystemObject, objFile As Scripting.TextStream
Dim objItem As Object, strFile As String
Dim Folder As Folder
Dim sName As String
Dim source As String
Dim target As String
Dim script As String
Dim abody() As String
Dim j As Integer
Dim i As Integer
Dim Input_Date As String
Dim Title As String
Dim Publisher As String
Dim File_Name As String
Dim Issue As String
Dim Release_Date As String
Dim Pages As String
Dim Pubcode As String
Dim Priority As String
Dim Repub As String
Dim Amazon As String
Dim Kindle As String
Dim XML As String
Dim File_Name_1 As String
On Error GoTo Qlockwork_err
Dim NS As NameSpace
Set NS = GetNamespace("MAPI")
If InStr(MyMail.Subject, "[AUTOPUB]") > 0 Then
abody = Split(MyMail.Body, Chr(10))
For j = 0 To UBound(abody)
abody(j) = Trim(Replace(abody(j), Chr(13), "", 1))
If abody(j) = "" Then GoTo continue
If InStr(abody(j), "Sent e:\tmp\") > 0 Then
target = Mid(Trim(abody(j)), InStr(Trim(abody(j)), "on ") + 3, 10)
Input_Date = Mid(Trim(abody(j)), InStr(Trim(abody(j)), "on ") + 3, 10)
source = Mid(Trim(abody(j)), InStr(Trim(abody(j)), "to ") + 2, InStr(Trim(abody(j)), "on ") - InStr(Trim(abody(j)), "to ") - 3)
File_Name = Trim(Mid(Trim(abody(j)), InStr(Trim(abody(j)), "to ") + 2, InStr(Trim(abody(j)), "on ") - InStr(Trim(abody(j)), "to ") - 3))
target = "D:\Logistics\Projects\Google\Repub\Input\" + Trim(target) + "\" + Trim(Replace(source, "/", "\", 1, Count:=2))
End If
If InStrRev(File_Name, "/") > 0 Then
File_Name = Mid(File_Name, InStrRev(File_Name, "/") + 1)
End If
If InStr(abody(j), "Publicatie: ") > 0 Then
Title = abody(j)
Title = Trim(Replace(Title, "Publicatie: ", "", 1))
End If
If InStr(abody(j), "Publisher: ") > 0 Then
Publisher = abody(j)
Publisher = Trim(Replace(Publisher, "Publisher: ", "", 1))
End If
If InStr(abody(j), "Issue: ") > 0 Then
Issue = abody(j)
Issue = Trim(Replace(Issue, "Issue: ", "", 1))
End If
If InStr(abody(j), "Release date: ") > 0 Then
Release_Date = abody(j)
Release_Date = Trim(Replace(Release_Date, "Release date: ", "", 1))
End If
If InStr(abody(j), "Number of pages: ") > 0 Then
Pages = abody(j)
Pages = Trim(Replace(Pages, "Number of pages: ", "", 1))
End If
If InStr(abody(j), "Pubcode: ") > 0 Then
Pubcode = abody(j)
Pubcode = Trim(Replace(Pubcode, "Pubcode: ", "", 1))
End If
If InStr(abody(j), "Priority: ") > 0 Then
Priority = abody(j)
Priority = Trim(Replace(Priority, "Priority: ", "", 1))
End If
If InStr(abody(j), "Conversion to Repub: ") > 0 Then
Repub = abody(j)
Repub = Trim(Replace(Repub, "Conversion to Repub: ", "", 1))
End If
If InStr(abody(j), "Conversion to Amazon: ") > 0 Then
Amazon = abody(j)
Amazon = Trim(Replace(Amazon, "Conversion to Amazon: ", "", 1))
End If
If InStr(abody(j), "Conversion to Kindle: ") > 0 Then
Kindle = abody(j)
Kindle = Trim(Replace(Kindle, "Conversion to Kindle: ", "", 1))
End If
If InStr(abody(j), "Conversion to Extra XML: ") > 0 Then
XML = abody(j)
XML = Trim(Replace(XML, "Conversion to Extra XML: ", "", 1))
End If
If InStr(abody(j), "Received client") > 0 Then GoTo continue
If InStr(abody(j), "URL") > 0 Then GoTo continue
If InStr(abody(j), "Sent Contractor") > 0 Then GoTo continue
If InStr(abody(j), "Received Contractor") > 0 Then GoTo continue
continue:
Next
If InStr(File_Name, "_feed.zip") > 0 Then
File_Name_1 = Trim(Replace(File_Name, "_feed.zip", "", 1))
End If
If InStr(File_Name, "_feed.pdf") > 0 Then
File_Name_1 = Trim(Replace(File_Name, "_feed.pdf", "", 1))
End If
strFile = "D:\Logistics\Projects\Google\Repub\Input\Metadata\" & File_Name_1 & ".txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(strFile) Then FSO.DeleteFile ("" & strFile & "")
If FileExists(strFile) Then
MsgBox "File " & File_Name_1 & ".txt exists"
Else
Set objFile = objFS.CreateTextFile(strFile, False)
If objFile Is Nothing Then
MsgBox "Error creating file '" & strFile & "'.", vbOKOnly + vbExclamation _
, "Invalid File"
Exit Sub
End If
With objFile
.Write Input_Date & "|"
.Write Title & "|"
.Write Publisher & "|"
.Write File_Name & "|"
.Write Issue & "|"
.Write Release_Date & "|"
.Write Pages & "|"
.Write Pubcode & "|"
.Write Priority & "|"
.Write Repub & "|"
.Write Amazon & "|"
.Write Kindle & "|"
.Write XML
.Write vbCrLf
End With
objFile.Close
Set objFS = Nothing
Set objFile = Nothing
End If
script = "D:\Patelinfo\Development\Kindle_Download.bat " + source + " " + target
Shell (script)
End If
Qlockwork_exit:
Set NS = Nothing
Exit Sub
Qlockwork_err:
MsgBox "An unexpected error has occurred."
Resume Qlockwork_exit
End Sub