The VBScript you've written seems to be intended to create an email using Microsoft Outlook, but there are a few issues that could prevent it from working correctly. Below are some potential problems and their solutions:
-
Date Variables (!dd!, !mm!, !yyyy!):
-
File paths:
- Ensure that the paths to your template file and attachments are correct. If they don't exist, the script will fail.
-
Use of CreateItemFromTemplate:
- Verify that your template file is valid and accessible without errors.
-
Email Sending Instead of Display:
- If you want to send the email directly instead of displaying it, you can change
.Display to .Send. However, since you're just testing, it's better to keep it as .Display.
-
Proper Syntax for Multi-line Strings:
- Make sure your concatenation of strings is correct. For example, using
_ at the end of a line is correct for continuing the line in VBScript.
Here’s a revised version of your script, incorporating the suggestions:
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim currentDate
echo Dim dd, mm, yyyy
echo currentDate = Date
echo dd = Day(currentDate)
echo mm = Month(currentDate)
echo yyyy = Year(currentDate)
echo Set OutlookApp = CreateObject^("Outlook.Application"^)
echo Set OutlookMail = OutlookApp.CreateItemFromTemplate^("D:\Prapun\Email_Trigger\0900_GoldV5_Referential\0900_GoldV5_Referential_dd-mm-yyyy.oft"^)
echo With OutlookMail
echo .Subject = "Test Auto: GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_" ^& mm ^& dd ^& ".zip"
echo .HTMLBody = "<div style=""font-family: Tahoma; font-size: 11pt;"">" ^& _
echo "Dear All,<br><br>" ^& _
echo "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy ^& " as Attachments" ^& "<br><br>" ^& _
echo "Best Regards," ^& "<br>" ^& _
echo "IT Servicedesk" ^& _
echo "</div>"
echo .Display
echo End With
echo Set OutlookMail = Nothing
echo Set OutlookApp = Nothing
) > %cd%\temp.vbs
cscript /nologo %cd%\temp.vbs
del %cd%\temp.vbs
With these adjustments, the script should work correctly if all paths and the email template are valid.
The main issue with your script lies in the way the date variables (!dd!, !mm!, !yyyy!) are being used. In this context, they are not defined; instead, they look like they are intended to be Windows batch variables but are being treated as string literals in the VBScript context. You need to define these values within your VBScript, rather than trying to access them as if they were defined in the command line.
Here's a corrected version of your script that sets and uses those date variables correctly within VBScript:
:: Mail
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim currentDate
echo Dim dd, mm, yyyy
echo currentDate = Date
echo dd = Day(currentDate)
echo mm = Month(currentDate)
echo yyyy = Year(currentDate)
echo Set OutlookApp = CreateObject^("Outlook.Application"^)
echo Set OutlookMail = OutlookApp.CreateItemFromTemplate^("D:\Prapun\Email_Trigger\0900_GoldV5_Referential\0900_GoldV5_Referential_dd-mm-yyyy.oft"^)
echo With OutlookMail
echo .Subject = "Test Auto: GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_" ^& mm ^& dd ^& ".zip"
echo .HTMLBody = "<div style=""font-family: Tahoma; font-size: 11pt;"">" ^& _
echo "Dear All,<br><br>" ^& _
echo "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy ^& " as Attachments" ^& "<br><br>" ^& _
echo "Best Regards," ^& "<br>" ^& _
echo "IT Servicedesk" ^& _
echo "</div>"
echo .Display
echo End With
echo Set OutlookMail = Nothing
echo Set OutlookApp = Nothing
) > %cd%\temp.vbs
cscript /nologo %cd%\temp.vbs
del %cd%\temp.vbs
Changes Made:
-
Defined Date Variables in VBScript:
- Added code to retrieve the current date and split it into day (
dd), month (mm), and year (yyyy) using VBScript's built-in Date(), Day(), Month(), and Year() functions.
-
Removed the exclamation marks:
- Changed the usage of
!dd!, !mm!, !yyyy! to dd, mm, yyyy since these variables are now defined in the VBScript context, not the command-line context.
-
Simplified the attachment file name formatting:
- Used the concatenation operator in VBScript correctly to create the file name for the attachment.
Note:
Make sure that the specified paths for the templates and attachments are valid and accessible. If the paths are wrong or the files don't exist, the script would fail during execution.