(
echo Dim OutlookApp
echo Dim OutlookMail
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 = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .Attachments.Add "D:\Demand\DEMAND_DF_NF_DC_MORNING_1230_%mm%%dd%.zip"
echo .Body = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^<br^> ^<br^> "Please find the attached file for GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^<br^> ^<br^> "This is an auto-generated email. Please do not reply to this email."
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
Assistant
The script you've provided is a VBScript that automates the process of creating and sending an email using Microsoft Outlook. It constructs an email based on a template and attaches a zipped file. Let's go through the essential parts of the script and clarify how it works:
Breakdown of VBScript
Creating Outlook Objects:
vb
Dim OutlookApp
Dim OutlookMail
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItemFromTemplate("D:\Prapun\Email_Trigger\0900_GoldV5_Referential\0900_GoldV5_Referential_dd-mm-yyyy.oft")
OutlookApp: This creates an instance of the Outlook application.
OutlookMail: This creates a new mail item from a predefined template (.oft file).
Setting Email Properties:
vb
With OutlookMail
.Subject = "GoldV5 Referential Data Checking Report as of " & "!dd!" & "/" & "!mm!" & "/" & "!yyyy!"
.Attachments.Add "D:\Demand\DEMAND_DF_NF_DC_MORNING_1230_%mm%%dd%.zip"
.Body = "GoldV5 Referential Data Checking Report as of " & "!dd!" & "/" & "!mm!" & "/" & "!yyyy!" & "<br>" & "<br>" & "Please find the attached file for GoldV5 Referential Data Checking Report as of " & "!dd!" & "/" & "!mm!" & "/" & "!yyyy!" & "<br>" & "<br>" & "This is an auto-generated email. Please do not reply to this email."
.Display
End With
Subject: Sets the subject line of the email.
Attachments: Attaches a file based on specific patterns for the day (dd) and month (mm).
Body: Sets the body of the email, with HTML line breaks.
Cleaning Up:
vb
Set OutlookMail = Nothing
Set OutlookApp = Nothing
This part releases the objects created to clean up memory.
Important Notes:
The placeholders !dd!, !mm!, and !yyyy! will need to be defined or replaced in the actual implementation for the script to work correctly. You might use environment variables or another method to assign the current day, month, and year.
Make sure that the file paths for the template and attachments are correct and that the files exist.
This script needs Outlook installed on the machine where it runs. Ensure that Outlook's security settings allow automation and scripting.
Test the script first to verify functionality, as scripted email sending can sometimes trigger security warnings or may not execute as expected.
How to Run the Script:
Save the script in a .bat or .cmd file and run it. The script will generate a temporary .vbs script in the current working directory, execute it to send the email, and then delete the script file.
If you need further customization or details on specific parts, feel free to ask!
User
(
echo Dim OutlookApp
echo Dim OutlookMail
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 = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .Body = "Dear All" ^& vbCrLf ^& vbCrLf ^& "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& vbCrLf ^& vbCrLf
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip"
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
if i need more word next to . Attachments.Add file
Assistant
If you want to add more context or textual information about the attached file next to the .Attachments.Add line in the VBScript, you can modify the email body to include a description of the attachment. Here's how you can do this:
Updated Script
In this version, I've added a description of the attachment right above the file attachment in the email body. Also, make sure to format and properly construct the body message to communicate clearly about the attachment.
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
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 = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .Body = "Dear All," ^& vbCrLf ^& vbCrLf ^& "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& vbCrLf ^& vbCrLf
echo .Body = .Body ^& "Please find attached the file named 'RCHK_!mm!!dd!.zip', which contains the data you need for review." ^& vbCrLf ^& vbCrLf
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip"
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
Explanation of Changes
Enhanced Email Body:
I have added the line:
vb
.Body = .Body & "Please find attached the file named 'RCHK_!mm!!dd!.zip', which contains the data you need for review." ^& vbCrLf ^& vbCrLf
This line extends the existing body content to include a description of the attachment.
Preserving Formatting:
The vbCrLf is used to ensure that there are new lines for proper formatting.
Final Note
Ensure that the placeholders (!dd!, !mm!, !yyyy!) are replaced with the actual date values (e.g., using a method to get today's date if necessary).
Also, check that the specified file paths to the template and attachments are correct and that these files exist. If you have any questions or need further modifications, feel free to ask!
User
it possible to make like this
.body xxxxx
.attachment
.body yyyy
Assistant
Yes, you can definitely format the body of the email and the attachments in such a way that you have multiple lines of body text before and after your attachment line. In VBScript, you can concatenate strings over multiple lines by using a combination of the & operator and vbCrLf for line breaks.
Here’s how you can structure your script to have body text before and after the attachment:
Revised Script Example
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
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 = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .Body = "Dear All," ^& vbCrLf ^& vbCrLf
echo .Body = .Body ^& "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& vbCrLf ^& vbCrLf
echo .Body = .Body ^& "Please find attached the file named 'RCHK_!mm!!dd!.zip'." ^& vbCrLf
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip"
echo .Body = .Body ^& vbCrLf ^& "If you have any questions, please feel free to reach out." ^& vbCrLf ^& vbCrLf ^& "Best Regards," ^& vbCrLf ^& "Your Name"
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
Breakdown of the Changes
Concatenating Body Text:
You can create the body text through multiple concatenation steps:
First, initialize your body with the greeting.
Next, append the date report information.
User
body type richtext
Assistant
If you want to set the body of your email in rich text format (RTF) using VBScript with Outlook, you'll need to use the HTMLBody property instead of the Body property. The HTMLBody allows you to use HTML syntax to format text, making it possible to apply styles like bold, italics, and custom colors.
Example Script Using HTMLBody
Here's how you can modify your original script to use HTMLBody for richer text formatting:
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
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 = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .HTMLBody = "<html><body>"
echo .HTMLBody = .HTMLBody ^& "<p>Dear All,</p>"
echo .HTMLBody = .HTMLBody ^& "<p>GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& "</p>"
echo .HTMLBody = .HTMLBody ^& "<p><b>Please find attached the file named 'RCHK_!mm!!dd!.zip'.</b></p>"
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip"
echo .HTMLBody = .HTMLBody ^& "<p>If you have any questions, please feel free to reach out.</p>"
echo .HTMLBody = .HTMLBody ^& "<p>Best Regards,<br>Your Name</p>"
echo .HTMLBody = .HTMLBody ^& "</body></html>"
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
Explanation
User
i need rich text body because i can embed attach file as file in body
Assistant
To embed an attachment as a file within the body of an email in Microsoft Outlook using VBScript, you typically need to set the email format to RTF (Rich Text Format). However, note that embedding files in an email body is not supported in plain HTML or text formats. Instead, RTF allows for more complex formatting, including embedding files as OLE (Object Linking and Embedding) objects.
Below, I'll show you how to create a VBScript that sets the email body to Rich Text Format and includes an attachment. However, please note that this method may not work with all Outlook versions or configurations, as embedding attachments can depend heavily on the email client's capabilities.
Example Script for Embedding an Attachment in Rich Text Format
Here’s how you can create a VBScript that prepares the email with an embedded attachment in RTF:
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Set OutlookApp = CreateObject^("Outlook.Application"^)
echo Set OutlookMail = OutlookApp.CreateItem^(-mailItem^)
echo With OutlookMail
echo .Subject = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .BodyFormat = 3 ^' 3 corresponds to RTF format
echo .Body = "Dear All," ^& vbCrLf ^& vbCrLf
echo .Body = .Body ^& "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& vbCrLf ^& vbCrLf
echo .Body = .Body ^& "Please find attached the file named 'RCHK_!mm!!dd!.zip'." ^& vbCrLf ^& vbCrLf
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip", olByValue, 0, "RCHK_!mm!!dd!.zip"
echo .Body = .Body ^& "If you have any questions, please feel free to reach out." ^& vbCrLf ^& "Best Regards," ^& vbCrLf ^& "Your Name"
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
User
option for "with outlookmail" .Subject, .Body, .Attachment.Add and any
Assistant
In VBScript, especially when automating Outlook, the With statement is used to avoid repeating the object name when referencing its properties and methods. You can use it effectively to set various properties such as .Subject, .Body, .HTMLBody, .Attachments.Add, and more. Below, I’ll provide a structured format for using the With OutlookMail block effectively, including common properties and methods that can be set within that block.
Example Script Using With OutlookMail
The following example demonstrates how to use the With statement to set multiple properties and methods on an Outlook mail item.
vbscript
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Set OutlookApp = CreateObject^("Outlook.Application"^)
echo Set OutlookMail = OutlookApp.CreateItem^(-mailItem^)
echo With OutlookMail
echo ' Set the subject of the email
echo .Subject = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo
echo ' Set body of the email in HTML format (for rich text capabilities)
echo .HTMLBody = "<html><body>"
echo .HTMLBody = .HTMLBody ^& "<p>Dear All,</p>"
echo .HTMLBody = .HTMLBody ^& "<p>GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& "</p>"
echo .HTMLBody = .HTMLBody ^& "<p><b>Please find attached the file named 'RCHK_!mm!!dd!.zip'.</b></p>"
echo .HTMLBody = .HTMLBody ^& "<p>If you have any questions, please feel free to reach out.</p>"
echo .HTMLBody = .HTMLBody ^& "<p>Best Regards,<br>Your Name</p>"
echo .HTMLBody = .HTMLBody ^& "</body></html>"
echo
echo ' Add an attachment (file to be sent)
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip", 1, 0, "RCHK_!mm!!dd!.zip"
echo
echo ' Optional: Display the email to review before sending
echo .Display
echo
echo ' If you want to send it directly uncomment the following line
echo ' .Send
echo End With
echo Set OutlookMail = Nothing
echo Set OutlookApp = Nothing
) > %cd%\temp.vbs
cscript /nologo %cd%\temp.vbs
del %cd%\temp.vbs
In VBScript, the syntax for adding attachments using the .Attachments.Add method is slightly different from what you would see in other programming languages such as Python or C#. In VBScript, you don't use named parameters or the Position parameter in the same way.
Instead, here’s how you can add an attachment to an Outlook email using the Attachments.Add method in VBScript.
Example for Adding Attachments
If you're looking to add an attachment with a specific file path, you can construct the Add method like so:
vbscript
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim reports
echo Dim r
echo ' Assume reports is an array with filenames
echo reports = Array("report1.zip", "report2.zip") ' Example filenames
' Create Outlook Application and Mail Item
echo Set OutlookApp = CreateObject^("Outlook.Application"^)
echo Set OutlookMail = OutlookApp.CreateItem^(-mailItem^)
echo With OutlookMail
echo .Subject = "QA Management Reports"
echo .Body = "Please find the attached QA Management Reports." ^& vbCrLf
' Loop through the reports array
echo For r = LBound(reports) To UBound(reports)
echo ' Add attachment from the specified directory
echo .Attachments.Add "H:\QA\QA Mgmt\Presentations\" ^& reports(r), 1
echo Next
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
Second Parameter: The attachment type (only is used here for , meaning that it sends the attachment as a standard attachment).
User
(
echo Dim OutlookApp
echo Dim OutlookMail
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 = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip", 1
echo .Body = "Dear All" ^& vbCrLf ^& vbCrLf ^& "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& vbCrLf ^& vbCrLf
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
Assistant
Your provided script is a VBScript that automates the process of creating and sending an email via Microsoft Outlook. It uses a predefined email template and aims to attach a ZIP file to the email. Below, I'm providing complete code with some minor enhancements, including how to implement the script correctly and with some best practices.
Enhanced VBScript Example
Here's a refined version of your script with comments for clarity:
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim dd, mm, yyyy
' You will need to initialize the date values for dd, mm, and yyyy appropriately.
echo ' Replace the following with actual values or calculations for the current date
echo dd = Day(Date)
echo mm = Month(Date)
echo yyyy = Year(Date)
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 = "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy
echo .Body = "Dear All," ^& vbCrLf ^& vbCrLf ^& "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy ^& vbCrLf ^& vbCrLf
' Attach the ZIP file
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_" ^& mm ^& dd ^& ".zip", 1
' Display the email for review before sending
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
Key Adjustments
Dynamic Date Handling:
I introduced variables (dd, mm, yyyy) to dynamically capture the current date. You can modify the initialization part if you want to use specific dates or calculation methods.
User
(
echo Dim OutlookApp
echo Dim OutlookMail
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", olByBalue, 0, "Test"
echo .Body = "Dear All" ^& vbCrLf ^& vbCrLf ^& "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& vbCrLf ^& vbCrLf
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
Assistant
Your provided VBScript code for sending an email using Outlook looks mostly correct but contains a couple of typos, particularly with the attachment method parameters. In addition, the placeholders for !dd!, !mm!, and !yyyy! should be properly defined as they won't work as intended in the context of VBScript and need to be replaced with actual date values (or variables).
Recommended Adjustments and Corrections
Here’s the updated VBScript code:
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim dd, mm, yyyy
' Get current date values for day, month, and year
echo dd = Day(Date)
echo mm = Month(Date)
echo yyyy = Year(Date)
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", 1, 0, "Test"
echo .Body = "Dear All," ^& vbCrLf ^& vbCrLf ^& "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy ^& vbCrLf ^& vbCrLf
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
Corrections and Enhancements
Dynamic Date Variables:
I have added code to capture the current day, month, and year into the variables dd, mm, and yyyy, respectively.
Correcting the Attachment Method:
You previously had olByBalue which likely was a typo for , which represents .
User
(
echo Dim OutlookApp
echo Dim OutlookMail
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 = "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .HTMLBody = ""
echo .HTMLBody = .HTMLBody ^& "Dear All,"
echo .HTMLBody = .HTMLBody ^& "GoldV5 Referential Data Checking Report as of " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& ""
echo .HTMLBody = .HTMLBody ^& "Please find attached the file named 'RCHK_!mm!!dd!.zip'."
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_!mm!!dd!.zip"
echo .HTMLBody = .HTMLBody ^& "If you have any questions, please feel free to reach out."
echo .HTMLBody = .HTMLBody ^& "Best Regards,Your Name"
echo .HTMLBody = .HTMLBody ^& ""
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
from code make hiden attach file and make link in htmlbody to open hidden attach file
Assistant
To automatically attach a file in Outlook and create a hyperlink in the email body (while keeping the attachment hidden), you typically need to follow a few steps. Unfortunately, direct hidden file attachments aren't natively supported in Outlook as a feature, but you can achieve a similar effect by creating a link that points to the file path.
Here's how to modify your script:
Include a link in the body of the email that points to the attached file.
Use HTML to create a clickable link in the body.
Updated Script Example
Below is an updated version of your VBScript that demonstrates how to create a hyperlink to the attached file in the body of the email:
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim dd, mm, yyyy
' Get the current date values for day, month, and year
echo dd = Day(Date)
echo mm = Month(Date)
echo yyyy = Year(Date)
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 = "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy
echo .HTMLBody = "<html><body>"
echo .HTMLBody = .HTMLBody ^& "<p>Dear All,</p>"
echo .HTMLBody = .HTMLBody ^& "<p>GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy ^& "</p>"
echo .HTMLBody = .HTMLBody ^& "<p><b>Please find attached the file named 'RCHK_" ^& mm ^& dd ^& ".zip'.</b></p>"
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_" ^& mm ^& dd ^& ".zip"
echo
echo ' Create hyperlink to the file location
echo .HTMLBody = .HTMLBody ^& "<p>Click <a href='file://H:/QA/QA Mgmt/Presentations/RCHK_" ^& mm ^& dd ^& ".zip'>here</a> to open the attached file directly.</p>"
echo
echo .HTMLBody = .HTMLBody ^& "<p>If you have any questions, please feel free to reach out.</p>"
echo .HTMLBody = .HTMLBody ^& "<p>Best Regards,<br>Your Name</p>"
echo .HTMLBody = .HTMLBody ^& "</body></html>"
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
User
echo .HTMLBody = .HTMLBody ^& "Click here to open the attached file directly."
in this email
Assistant
If you want to modify the email body to include a hyperlink that points to an embedded attachment, please note that the structure needs to facilitate proper linking, especially if the file is located on a shared network.
Updated Script with HTML Hyperlink
Here’s how the VBA script should look with the correct modifications to include a clickable hyperlink that references the attached file:
batch
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim dd, mm, yyyy
' Get current date values for day, month, and year
echo dd = Day(Date)
echo mm = Month(Date)
echo yyyy = Year(Date)
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 = "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy
echo .HTMLBody = "<html><body>"
echo .HTMLBody = .HTMLBody ^& "<p>Dear All,</p>"
echo .HTMLBody = .HTMLBody ^& "<p>GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy ^& "</p>"
echo .HTMLBody = .HTMLBody ^& "<p><b>Please find attached the file named 'RCHK_" ^& mm ^& dd ^& ".zip'.</b></p>"
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_" ^& mm ^& dd ^& ".zip"
echo
echo ' Create a hyperlink to the local file location (make sure this path is accessible to the recipient)
echo .HTMLBody = .HTMLBody ^& "<p>Click <a href='file://H:/QA/QAMgmt/Presentations/RCHK_" ^& mm ^& dd ^& ".zip'>here</a> to open the attached file directly.</p>"
echo
echo .HTMLBody = .HTMLBody ^& "<p>If you have any questions, please feel free to reach out.</p>"
echo .HTMLBody = .HTMLBody ^& "<p>Best Regards,<br>Your Name</p>"
echo .HTMLBody = .HTMLBody ^& "</body></html>"
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
User
คือไฟล์แนบมากับอีเมล และต้องการให้ link อ่านไฟล์จากไฟล์ที่แนบมากับอีเมล
Assistant
การสร้างลิงก์ในอีเมลที่เชื่อมโยงกับไฟล์แนบใน Microsoft Outlook โดยตรงไม่ได้รับการสนับสนุนแบบถูกต้องใน Outlook API เนื่องจาก Outlook จะเก็บไฟล์แนบไว้ในอีเมล และไม่สามารถเข้าถึงไฟล์แนบเหล่านี้ผ่านลิงก์แบบ file:// ได้
อย่างไรก็ตาม คุณสามารถเขียนข้อความในอีเมลเพื่อบอกผู้รับให้เปิดไฟล์แนบจากอีเมล รายละเอียดนี้สามารถใช้ใน HTML Body ของอีเมลได้ แต่ไม่สามารถสร้างลิงก์ที่ตรงไปยังไฟล์ที่แนบโอโดยตรงผ่าน Outlook API
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Dim dd, mm, yyyy
' Get current date values for day, month, and year
echo dd = Day(Date)
echo mm = Month(Date)
echo yyyy = Year(Date)
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 = "GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy
echo .HTMLBody = "<html><body>"
echo .HTMLBody = .HTMLBody ^& "<p>Dear All,</p>"
echo .HTMLBody = .HTMLBody ^& "<p>GoldV5 Referential Data Checking Report as of " ^& dd ^& "/" ^& mm ^& "/" ^& yyyy ^& "</p>"
echo .HTMLBody = .HTMLBody ^& "<p><b>Please find attached the file named 'RCHK_" ^& mm ^& dd ^& ".zip'.</b></p>"
echo .Attachments.Add "D:\Prapun\Email_Trigger\0900_GoldV5_Referential\RCHK_" ^& mm ^& dd ^& ".zip"
echo
echo ' Provide instructions to open the attachment
echo .HTMLBody = .HTMLBody ^& "<p>Please download and open the attached file titled 'RCHK_" ^& mm ^& dd ^& ".zip' to review the contents.</p>"
echo
echo .HTMLBody = .HTMLBody ^& "<p>If you have any questions, please feel free to reach out.</p>"
echo .HTMLBody = .HTMLBody ^& "<p>Best Regards,<br>Your Name</p>"
echo .HTMLBody = .HTMLBody ^& "</body></html>"
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
User
(
echo Dim OutlookApp
echo Dim OutlookMail
echo Set OutlookApp = CreateObject^("Outlook.Application"^)
echo Set OutlookMail = OutlookApp.CreateItemFromTemplate^("D:\Prapun\Email_Trigger\2000_CO_ERROR_STOCK\CO_ERROR_STOCK_Network1091_Round2000.oft"^)
echo With OutlookMail
echo .Subject = "Test Auto: CO ERROR STOCK Network:1091 Round:2000 as Date: " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!"
echo .Body = "เรียนผู้เกี่ยวข้อง" ^& vbCrLf ^& vbCrLf
echo .Body = .BOdy ^& "กรุณาตรวจสอบไฟล์ที่แนบดังต่อไปนี้" ^& vbCrLf ^& vbCrLf
echo .Body = .Body ^& "- CO ERROR STOCK Network:1091 Round:2000 as Date: " ^& "!dd!" ^& "/" ^& "!mm!" ^& "/" ^& "!yyyy!" ^& vbCrLf ^& vbCrLf
echo .Body = .Body ^& "- ตัวอย่าง CO ERROR และแนวทางการตรวจเช็คและแก้ไข" ^& vbCrLf ^& vbCrLf
echo .Attachments.Add "D:\Prapun\Email_Trigger\2000_CO_ERROR_STOCK\CO_ERROR_STOCK_1091_!mm!!dd!_2000.csv.gz"
echo .Attachments.Add "D:\Prapun\Email_Trigger\2000_CO_ERROR_STOCK\Sample_CO_ERROR_Update_24-Dec-2013.zip"
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
make new line for second attach file
Assistant
To ensure that the second attachment appears on a new line in the email body, you can add an additional line break before mentioning the second attachment in the body of the email text. You’ll want to concatenate vbCrLf (which represents a new line) before the text that references the second attachment.
Here's the modified script that includes a newline for the second attachment:
After the attachment is added, you can append additional information (e.g., a closing remark).
Improved Readability:
This structure makes it clear where each part of the body content belongs, allowing you to maintain a clean and organized message format.
Example Structure
In this structure, your email body effectively reads:
code
Dear All,
GoldV5 Referential Data Checking Report as of <date>.
Please find attached the file named 'RCHK_<mm><dd>.zip'.
If you have any questions, please feel free to reach out.
Best Regards,
Your Name
Note
Make sure that the date placeholders (!dd!, !mm!, !yyyy!) are appropriately replaced with valid date strings when you run the script. If you need further customization or assistance, please let me know!
Using HTMLBody:
Switched from .Body to .HTMLBody, which allows for HTML content.
HTML Structure:
The body starts with <html><body> and ends with </body></html> to define an HTML document.
Paragraphs (<p>) are used to separate sections of text.
The <b> tag is used to bolden text.
Formatted Messages:
You can easily add more HTML formatting (like colors, links, bullet points, etc.) by using additional HTML elements.
Rich Text Formatting Options:
You can enhance the email further, using additional HTML features, such as:
Fonts and Styles:
html
<span style="font-weight: bold;">Your Text Here</span>
Colors:
html
<span style="color: red;">Your Text Here</span>
Links:
html
<a href="http://example.com">This is a link</a>
Note
Always ensure that the Outlook security settings allow for the execution of scripts and the sending of rich-text emails. If you need any more advanced formatting or features, feel free to ask!
Explanation of the Code
Creating the Email:
The code creates an Outlook application instance and a new mail item.
Setting Subject:
The subject line is set to include the report date.
Body Format:
vb
.BodyFormat = 3 ' 3 corresponds to RTF format
This line sets the body format of the email to RTF.
Building the Body Text:
The body text is constructed and can include line breaks (vbCrLf).
This adds an attachment to the email with a specified name.
Important Notes
OLE Objects: If you wish to embed the file visibly like an object within the body of the email (i.e., showing the actual file in the body), you may need to use Outlook's OLE capabilities explicitly, which is much more complex and often involves using the Outlook Object Model in more advanced ways through COM programming or APIs.
Email Client Compatibility: Not all email clients handle embedded RTF emails well, and users may experience different behaviors depending on their email client settings.
Testing: Test the script thoroughly in your environment to confirm that the attachment behaves as expected.
If you have further requirements or need additional help, please let me know!
Explanation of Common Properties and Methods
.Subject: Sets the subject of the email.
.Body or .HTMLBody:
.Body: Use for plain text messages.
.HTMLBody: Use for richer formatting, allowing you to include HTML tags for styling.
.Attachments.Add: Adds an attachment to the email.
Parameters:
The first parameter is the file path.
The second parameter is the attachment type (1 for olByValue - sending the file as a regular attachment).
The third parameter can be a long that represents the position where the attachment will be inserted, usually set to 0.
The fourth parameter is the display name of the attachment.
.Display: Displays the email in the Outlook window for review before sending.
.Send: If uncommented, this command sends the email immediately.
.Importance: Set the importance of the email (low, normal, high).
Example:
vb
.Importance = 2 ' 2 means high importance
.ReadReceiptRequested: Request a read receipt for acknowledges.
Example:
vb
.ReadReceiptRequested = True
Additional Notes
Rich Text Format: As per your earlier request, to include files in the body or send rich text emails, always ensure that both the HTMLBody or Body properly encapsulate any necessary HTML or rich text formatting.
Testing: Always run and test your script in a safe environment before deploying it in production or in critical scenarios.
Using this structured way allows you to maintain clean, readable, and maintainable code while effectively utilizing Outlook's capabilities via VBScript. If you have further questions or need additional examples or explanations, please ask!
1
olByValue
Using a Loop:
A loop (For...Next) is used to iterate through the reports array to add multiple attachments dynamically.
File Path:
Make sure that the file paths in your code actually exist and that Outlook has permissions to access these directories.
Display or Send the Email:
.Display: Opens the email for review before sending.
You could replace .Display with .Send to send the email directly, but for testing, it’s often better to display first.
Important Notes
It’s a good practice to check if the file exists before attempting to attach it.
Adjust the path as per your environment since the example uses a specific directory on the H: drive.
Always test the script in a safe environment.
If you need further customization or have other requirements, feel free to ask!
Note: The lines for initializing day, month, and year need to be placed in their correct locations.
Setting the Body:
The email body string construction is done using the correct dynamic date parts.
Attachment Handling:
The attachment line has been updated to dynamically format the file path using the date variables.
The script assumes the ZIP file’s naming follows the format RCHK_ + month + day + .zip.
Display Email:
The .Display method is used to open the email for review before it is sent. If you want to send the email directly, you can replace .Display with .Send.
Testing and Error Handling
File Paths: Ensure the specified paths for the template and attachment exist on your system to avoid runtime errors.
Email Client Permissions: Make sure that Outlook is configured to allow VBScript automation and that the security settings permit sending emails without confirmation.
Final Note
You can run this script by saving it with a .bat extension and executing it. Make sure to test it in a controlled environment to ensure everything works as expected before using it in critical scenarios.
If you have any further questions or need additional modifications, feel free to ask!
1
olByValue
The second argument is the file path constructed dynamically based on the month and day.
Formatted Subject and Body:
The subject and body now correctly reference dd, mm, and yyyy instead of using the placeholders!dd!, !mm!, and !yyyy! which would not resolve in VBScript.
Attachment Best Practices:
Ensure that the full path to the ZIP file fits the naming convention established in your script.
Testing Your Script
Ensure you have:
Outlook configured on the machine where this script runs.
The paths you are trying to use are valid and accessible.
This script will display the email first; you may change .Display to .Send if you wish to send the email directly without displaying it.
Final Note
Save this script with a .bat extension and run it to automate the email creation process. If you have additional questions or need further changes, feel free to ask!
Key Adjustments
Dynamic Date: The script defines the values of dd, mm, and yyyy based on the current date.
Hyperlink in HTMLBody:
After adding the file as an attachment, the script creates a clickable HTML link using the <a> tag, which points to the file's location using the file:// protocol.
The path should match the location of where the ZIP file is stored on your system. Adjust the path in the href attribute accordingly.
Hidden File Attachments:
Directly hiding attachments are not supported, so instead, the hyperlink serves as the workaround to access the file quickly without requiring users to manually search for it in the email attachment section.
Important Considerations
File Accessibility: The link provided in the email will only work if the recipient has access to the specified network path (e.g., H: drive) and sufficient permissions.
Email Client: Ensure that the recipient’s email client supports HTML and can process the links appropriately.
Security Considerations: Be cautious with file links in emails, as recipients need appropriate access permissions to view the files.
Try this modified script to see if it meets your requirements! If you have any further customization or questions, feel free to ask.
Key Points
Hyperlink Format: The line
vb
echo .HTMLBody = .HTMLBody ^& "<p>Click <a href='file://H:/QA/QAMgmt/Presentations/RCHK_" ^& mm ^& dd ^& ".zip'>here</a> to open the attached file directly.</p>"
is formatted to create a clickable link that directs users to the local file path of the ZIP file. Make sure that the path to the file is correct.
Path Accessibility:
The path H:/QA/QAMgmt/Presentations/RCHK_MMDD.zip must be accessible from the recipient's machine. If this is on a network drive, they need appropriate permissions to access that path.
HTML Body:
The body content is being constructed using HTML, which means that line breaks and formatting will be preserved when the recipient views the email in a client that supports HTML.
Attachment:
The .Attachments.Add line includes the actual file as an attachment, so recipients will see it as part of the email, and they can also use the hyperlink to open it directly.
Important Note on File Links
Security Restrictions: Depending on the user's email client and security settings, direct access to files through hyperlinks might be restricted, especially if they are on a different network or if their client does not support the file:// protocol. Always test to confirm the functionality.
By implementing these adjustments, your script should function as intended, allowing recipients to view the file attachment directly through a hyperlink in the email body! If you have any further questions or need more help, feel free to ask!
คำอธิบาย
สร้างอีเมล: สคริปต์สร้างอีเมลใหม่ด้วย Outlook API โดยใช้ไฟล์เทมเพลตที่กำหนดไว้
เพิ่มไฟล์แนบ: มีการเพิ่มไฟล์ ZIP ที่ระบุในสคริปต์เป็นไฟล์แนบ
The message text defines that these are attachment filenames to ensure clarity for the recipient.
Additional Note
Make sure to test the script to ensure it behaves as expected in generating the email in Outlook with the desired formatting and attachments. You may modify the file paths and names as necessary to reflect your actual file structures and requirements.
Let me know if you need more adjustments or further information!