VBS脚本实现Outlook自动发件:附件添加功能失效求助
Let's work through why your attachment isn't showing up in your Outlook VBS script—other parts working means we're close to fixing this!
Common Causes & Fixes
Verify the File Path & Existence
Double-check thatC:\VzW\Ankur.txtactually exists in that exact location. Typos in the path (like missing folders, extra spaces, or incorrect capitalization, even though Windows is case-insensitive) are the most common culprit here. Also, make sure your script has permission to access that folder—if it's in a restricted directory (likeProgram Files), you might need to run the script as an administrator.Correct the
AddAttachmentSyntax
Your initial code had the right approach using.AddAttachmentinside theWith MyItemblock, but the later attemptMyApp..AddAttachmenthad an extra dot and was referencing the wrong object (attachments belong to the mail item, not the Outlook application itself). Stick to calling it on theMyItemobject.Add Debugging Checks
Adding a quick file existence check will help you rule out missing files immediately. Here's a revised version of your script with this check and clean-up:
Set MyApp = CreateObject("Outlook.Application") Set MyItem = MyApp.CreateItem(0) ' olMailItem ' Check if the attachment file exists first Dim fileSystem Set fileSystem = CreateObject("Scripting.FileSystemObject") If Not fileSystem.FileExists("C:\VzW\Ankur.txt") Then MsgBox "Error: Attachment file not found at C:\VzW\Ankur.txt", vbCritical WScript.Quit End If With MyItem .To = Vdistro .CC = Vregion .Subject = Vsubject .HTMLBody = EmailComments & EmailBody & "<br><b>Regards,</b>" & mysignature ' Attach the file to the mail item .AddAttachment "C:\VzW\Ankur.txt" '.Importance = Vimportance '.FlagStatus = Vflagstatus ' Use .Display to preview the mail first (helps debug) .Display End With ' Clean up objects Set fileSystem = Nothing Set MyItem = Nothing Set MyApp = Nothing
- Check Outlook Security Restrictions
Outlook sometimes blocks script-based attachments as a security measure. If you don't see any error messages, it might be silently blocking the action. Try running the script while Outlook is open, and watch for any security prompts you might have missed. If needed, you can adjust your Outlook trust settings temporarily (just be cautious with this, as it affects overall security).
Final Tip
Instead of immediately sending the mail, use .Display first like in the revised script. This lets you visually confirm if the attachment is present before sending, making debugging much easier.
内容的提问来源于stack exchange,提问作者Ankur Raj Sinha




