从 Excel 编辑打开的电子邮件 [英] Edit an open email from excel

查看:70
本文介绍了从 Excel 编辑打开的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢大家.所有的建议可能都有效.我终于意识到,我的雇主启用了一个安全选项来防止此类行为.很遗憾,我无法编辑电子邮件,只能创建它们.

我正在研究如何修改 Excel 中打开的电子邮件.

我有一个电子邮件模板,excel 会用我工作簿中工作表中的值替换某些字符串.

我一直在研究 activeinspector,但我仍然不确定 Excel 和 Outlook 之间的交叉编程是如何工作的.搜索各种网站让我得到这个,但它不起作用.

<块引用>

str = outlookMail.Body 上的错误 287

' 创建 Outlook 对象昏暗的 OutlookApp 作为 Outlook.Application设置 OutlookApp = 新建 Outlook.Application将 OutlookInspector 调暗为 Outlook.Inspector将 OutlookMail 调暗为 Outlook.MailItem设置 OutlookInspector = outlookApp.ActiveInspectorDim str 作为字符串如果不是 OutlookInspector 什么都不是如果 TypeOf outlookInspector.CurrentItem 是 Outlook.MailItem 那么设置 OutlookMail = outlookInspector.CurrentItemstr = OutlookMail.Body' 替换str = Replace(str, "DIPOC", "string1")str = Replace(str, "REFNUM", "string2")str = Replace(str, "RCVDATE", "string3")str = Replace(str, "EMPNAME", "string4")str = Replace(str, "EMPEIDN", "string5")str = Replace(str, "ACTIONREQ", "string6")OutlookMail.Body = str万一万一

我还是个新手,所以任何帮助将不胜感激.提前致谢.

解决方案

将 Outlook 调暗为对象设置 Outlook = CreateObject("Outlook.Application")Dim oInspector 作为 InspectorDim olOutMail As MailItem

如果您的代码编译通过,那么您就有了对 Outlook 对象模型的引用,并且 Outlook 标识符引用了 Outlook 类型库:Dim Outlook AsObject 正在隐藏那个声明.重命名它,并使用它们来自的库明确限定类型:

将outlookApp 调暗为Outlook.Application设置 OutlookApp = 新建 Outlook.Application将 OutlookInspector 调暗为 Outlook.Inspector将 OutlookMail 调暗为 Outlook.MailItem

我无法为您的前缀找出一致的方案,所以我放弃了它.

现在当您分配检查员参考时:

<块引用>

设置 oInspector = Outlook.ActiveInspector

这是模棱两可的,至少对于人类读者来说:如果您在键入 . 点时获得 IntelliSense,那么 VBA 正在理解 Outlook作为 Outlook.Application,这意味着麻烦,因为现在你有一个隐含的 Application 引用这不是你想要使用的实例... 这很可能是您收到此错误的原因.

你想消除歧义.

设置outlookInspector = outlookApp.ActiveInspector

这应该会为您提供要使用的 Inspector 参考.

下一个问题是您设置了 olOutMail 对象引用假设有一个活动检查器,并且您正在查看一个 MailItem- 如果其中一个假设不成立,则预计会发生火灾.

如果不是outlookInspector什么都不是如果 TypeOf outlookInspector.CurrentItem 是 Outlook.MailItem 那么'现在我们知道我们正在查看邮件.设置 OutlookMail = outlookInspector.CurrentItem'...在这里使用 OutlookMail...万一万一

Edit: Thank you everyone. All the suggestions probably work. I just finally realized, there's a security option enabled by my employer to prevent this type of action. Unfortunately, I can't edit emails, only create them.

I'm trying to figure out how to modify an open email from excel.

I have an email template that excel will replace certain strings with values from a worksheet in my workbook.

I've been researching activeinspector, but I'm still unsure how cross programming works between excel and outlook. Searching various sites gets me this, but it's not working.

Error 287 on str = outlookMail.Body

' Create the outlook object
Dim outlookApp As Outlook.Application
Set outlookApp = New Outlook.Application

Dim outlookInspector As Outlook.Inspector
Dim outlookMail As Outlook.MailItem

Set outlookInspector = outlookApp.ActiveInspector

Dim str As String

If Not outlookInspector Is Nothing Then
    If TypeOf outlookInspector.CurrentItem Is Outlook.MailItem Then
        Set outlookMail = outlookInspector.CurrentItem
        str = outlookMail.Body

        ' Replacements
        str = Replace(str, "DIPOC", "string1")
        str = Replace(str, "REFNUM", "string2")
        str = Replace(str, "RCVDATE", "string3")
        str = Replace(str, "EMPNAME", "string4")
        str = Replace(str, "EMPEIDN", "string5")
        str = Replace(str, "ACTIONREQ", "string6")

        outlookMail.Body = str
    End If
End If

I'm still new to this so any help will be appreciated. Thank you in advance.

解决方案

Dim Outlook As Object
Set Outlook = CreateObject("Outlook.Application")

Dim oInspector As Inspector
Dim olOutMail As MailItem

If your code compiles, then you have a reference to the Outlook object model, and the Outlook identifier refers to the Outlook type library: Dim Outlook As Object is shadowing that declaration. Rename it, and explicitly qualify the types with the library they're from:

Dim outlookApp As Outlook.Application
Set outlookApp = New Outlook.Application

Dim outlookInspector As Outlook.Inspector
Dim outlookMail As Outlook.MailItem

I couldn't figure out a consistent scheme for your prefixing, so I dropped it.

Now when you assign the inspector reference:

Set oInspector = Outlook.ActiveInspector

That's ambiguous, at least to a human reader: if you get IntelliSense when you type that . dot, then VBA is understanding Outlook as Outlook.Application, and this means trouble, for now you have an implicit Application reference that is not the instance you mean to work with... and that could very well why you're getting this error.

You want to disambiguate that.

Set outlookInspector = outlookApp.ActiveInspector

That should give you the Inspector reference you mean to work with.

Next problem, is that you set the olOutMail object reference assuming there's an active inspector, and that you're looking at a MailItem - expect fire if either assumption isn't true.

If Not outlookInspector Is Nothing Then
    If TypeOf outlookInspector.CurrentItem Is Outlook.MailItem Then
        'NOW we KNOW we're looking at a mailitem.
        Set outlookMail = outlookInspector.CurrentItem
        '...work with outlookMail here...
    End If
End If

这篇关于从 Excel 编辑打开的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆