VBA Outlook宏,根据主题行或收件人的姓氏附加特定文件到发送的电子邮件。

huangapple go评论83阅读模式
英文:

VBA Outlook Macro to attach a specific file to an outgoing email based on Subject line or Recipient's Last name

问题

I can help with the translation. Here is the translated text without the code part:

我是新手使用VBA。我想要看看是否可能创建一个宏,如果主题行部分匹配或者根据收件人的姓氏,将特定文件附加到即将发送的电子邮件。

例如,如果我正在撰写一个主题为“XYZ-03210 - 案例 - 日期”的电子邮件,我想要一个宏按钮,将附加一个名为“XYZ-03210_TEMPLATE”的文档。

如果这不可能,该文件也位于一个以收件人姓氏命名的共享文件夹中,因此宏可以根据姓氏查找文件。

谢谢!

目前,我只有一个基本的宏按钮,将一个通用文件附加到即将发送的草稿电子邮件,但我想创建一个更复杂的宏,根据我上面提到的条件附加一个特定的文件。

英文:

I'm new to using VBA. I'm looking to see if it's possible to create a macro that will attach a specific file to an outgoing email if there is a partial match with the Subject Line OR depending on the recipient's last name.

For example, if I am drafting an email with the subject line "XYZ-03210 - Case - Date" I'd like to have a macro button that will attach a doc with the file name "XYZ-03210_TEMPLATE"

If that isn't possible, that file is also located in a shared folder named with the Recipient's last name, so the macro could look to find the file based on the last name as well.

Thank you!

Right now I just have a basic macro button that will attach a generic file to an outgoing draft email, but I would like to create a more complex macro that will attach a SPECIFIC file based on the criteria I mentioned above.

答案1

得分: 1

是的,可以根据Outlook中的“主题”属性值附加文件到传出的项目。您可以尝试处理“Application”类的“ItemSend”事件,该事件在通过Inspector(在检查员关闭之前但在用户单击“发送”按钮之后)或在程序中使用Outlook项目的“Send”方法,例如“MailItem”时触发。

因此,在“ItemSend”事件处理程序中,您可以检查传递的项目的“主题”属性,并尝试附加具有该名称的文件。使用Mid函数从字符串中获取指定数量的字符。例如:

Dim MyString, FirstWord, LastWord, MidWords
YourSubject = "Mid Function Demo"    '创建文本字符串。
MidWords = Mid(MyString, 5)    '返回“Function Demo”

要附加文件,您需要使用Attachments.Add方法,该方法在“Attachments”集合中创建新附件。附件的来源可以是文件(由文件系统路径和文件名表示)或构成附件的Outlook项目。因此,如果需要附加来自共享文件夹的文件,需要在调用“Add”方法之前将其复制或移动到本地。

最后,我建议从Getting started with VBA in Office文章开始。

英文:

Yes, it is possible to attach files based on the Subject property value to the outgoing items in Outlook. You can try handling the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.

So, in the ItemSend event handler you may check the Subject property of the Item passed as a parameter and try to attach a file with such name. Use the Mid function to get a specified number of characters from a string. For example:

Dim MyString, FirstWord, LastWord, MidWords
YourSubject = "Mid Function Demo"    ' Create text string.
MidWords = Mid(MyString, 5)    ' Returns "Function Demo"

To attach a file you need to use the Attachments.Add method which creates a new attachment in the Attachments collection. The source of the attachment. can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. So, if you need to attach a file from a shared folder, you need to copy or move it locally before calling the Add method.

Finally, I'd suggest starting from the Getting started with VBA in Office article.

huangapple
  • 本文由 发表于 2023年4月6日 22:30:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定