vba 为什么我不能在不参考 Outlook 库的情况下使用 MailItem.BodyFormat?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23732114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:10:45  来源:igfitidea点击:

Why can't I use MailItem.BodyFormat without reference to Outlook library?

excelvba

提问by Kapol

The following code works fine when the VBA Project includes a reference to Microsoft Outlook Object Library. When it doesn't, the last line produces the Invalid procedure call or argumenterror.

当 VBA 项目包含对 Microsoft Outlook 对象库的引用时,以下代码工作正常。如果没有,最后一行会产生Invalid procedure call or argument错误。

Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
olMail.BodyFormat = olFormatHTML 'error here :-(

I don't think I have ever encountered a situation where a property or method is available in a library only when I add a reference to that library.

我认为我从来没有遇到过这样的情况,即只有当我添加对该库的引用时,该库中的属性或方法才可用。

This problem occures on Excel 2010/2013. I have not tested it on other versions.

此问题发生在 Excel 2010/2013 上。我没有在其他版本上测试过。

回答by Rory

I think your problem is simply that olFormatHTML has no value without the reference, since it's part of an Outlook enumeration. Try using the literal value instead:

我认为您的问题只是 olFormatHTML 没有引用就没有价值,因为它是 Outlook 枚举的一部分。尝试改用文字值:

olMail.BodyFormat = 2

or declare a constant first:

或者先声明一个常量:

Const olFormatHTML as Long = 2

回答by GoughW

That's because constants aren't available without a reference.

那是因为常量在没有引用的情况下不可用。

Specifies the format of the body text of an item.

指定项目正文的格式。

Name Value Description

名称 值 说明

olFormatHTML 2 HTML format

olFormatHTML 2 HTML 格式

olFormatPlain 1 Plain format

olFormatPlain 1 纯格式

olFormatRichText 3 Rich text format

olFormatRichText 3 富文本格式

olFormatUnspecified 0 Unspecified format

olFormatUnspecified 0 未指定的格式

olMail.BodyFormat = 2