尝试在 Outlook 中运行 VBA 脚本时出现“未定义子或函数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23722013/
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-08 18:13:05  来源:igfitidea点击:

"Sub or Function not defined" when trying to run a VBA script in Outlook

vbaoutlookoutlook-vba

提问by Kurt Peek

As a first step in creating a VBA script to resize a currently selected image to 100% x 100%, I'm trying to reproduce the example in http://msdn.microsoft.com/en-us/library/ee814736(v=office.14).aspx. The macro is very simple:

作为创建 VBA 脚本以将当前选定的图像大小调整为 100% x 100% 的第一步,我试图在http://msdn.microsoft.com/en-us/library/ee814736(v =office.14).aspx。宏很简单:

Sub Test()
   MsgBox ("Hello world")
End Sub

The VBA script was simply created in "Project1" which opens by default when one presses Alt+F11. However, I keep getting the error "Sub or Function not defined" when trying to run the VBA script (Figures 1 and 2).

VBA 脚本只是在“Project1”中创建的,当按下 Alt+F11 时它会默认打开。但是,在尝试运行 VBA 脚本时,我不断收到错误“Sub or Function not defined”(图 1 和图 2)。

How can I make the VBA script 'accessible' to Outlook?

如何使 VBA 脚本“可访问”到 Outlook?

Running the "Test" macro in Microsoft OutlookFigure 1 Running the "Test" macro in Microsoft Outlook

在 Microsoft Outlook 中运行“测试”宏图 1 在 Microsoft Outlook 中运行“测试”宏

enter image description hereFigure 2 "Sub or Function not defined" error, with module tree in the background

在此处输入图片说明图 2 “Sub or Function not defined”错误,后台有模块树

采纳答案by Kurt Peek

I solved the problem by following the instructions on msdn.microsoft.com more closely. There, it is stated that one must create the new macro by selecting Developer -> Macros, typing a new macro name, and clicking "Create". Creating the macro in this way, I was able to run it (see message box below).

我通过更密切地按照 msdn.microsoft.com 上的说明解决了该问题。在那里,据说必须通过选择“开发人员”->“宏”,键入新的宏名称,然后单击“创建”来创建新宏。以这种方式创建宏,我能够运行它(请参阅下面的消息框)。

enter image description here

在此处输入图片说明

回答by Arturas M

I had a similar situation with this issue. In this case it would have looked like this

我在这个问题上遇到了类似的情况。在这种情况下,它看起来像这样

Sub Test()
   MsqBox ("Hello world")
End Sub

The problem was, that I had a lot more code there and couldn't recognize, that there was a misspelling in "MsqBox" (q instead of g) and therefore I had an error, it was really misleading, but since you can get on this error like that, maybe someone else will notice that it was cause by a misspelling like this...

问题是,我在那里有更多的代码并且无法识别,“MsqBox”中有一个拼写错误(q 而不是 g),因此我有一个错误,这真的是误导,但因为你可以得到在这样的错误上,也许其他人会注意到它是由这样的拼写错误引起的......

回答by Arpit Jain

This error “Sub or Function not defined”, will come every time when there is some compile error in script, so please check syntax again of your script.

这个错误“Sub or Function not defined”,每次在脚本中出现编译错误时都会出现,所以请再次检查脚本的语法。

I guess that is why when you used msqbox instead of msgbox it throws the error.

我想这就是为什么当您使用 msqbox 而不是 msgbox 它会引发错误的原因。

回答by Emwat

This probably does not answer your question, but I had the same question and it answered mine.

这可能没有回答你的问题,但我有同样的问题,它回答了我的问题。

I changed Private Functionto Public Functionand it worked.

我改变Private FunctionPublic Function,它奏效了。

回答by Kangqiao Zhao

I need to add that, if the Module name and the sub name is the same you have such issue. Consider change the Module name to mod_Test instead of "Test" which is the same as the sub.

我需要补充一点,如果模块名称和子名称相同,您就会遇到此类问题。考虑将模块名称更改为 mod_Test 而不是与 sub 相同的“Test”。

回答by user3287522

I think you need to update your libraries so that your VBA code works, your using ms outlook

我认为您需要更新您的库,以便您的 VBA 代码正常工作,您使用的是 ms Outlook