查找VB(MSVisualStudio 2005)和VBA(Excel)之间的区别。
查找VB(MSVisualStudio 2005)和VBA(Excel)之间的区别。
我在网上找到了一个我想要实现的功能的代码。
像往常一样,我在Visual Studio中启动它,它完全没有问题。
问题出在当我试图将它转移到Excel时,它就不起作用了。
据我所知,VBA是VB的简化版。 (基于阅读这篇文章:
因此,我该如何找出在两种编程环境之间转换时丢失了什么?
为了提供更详细的信息:
我在Visual Studio中编写了一个程序,按下按钮后会向我发送电子邮件。
然后我尝试将它作为宏移到Excel中,但那不起作用。
编辑:添加额外的问题信息
这是我在Visual Studio中拥有的内容:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CDO_Mail_Small_Text() End Sub Sub CDO_Mail_Small_Text() Dim iMsg As Object Dim iConf As Object Dim strbody As String Dim Flds As Object iMsg = CreateObject("CDO.Message") iConf = CreateObject("CDO.Configuration") iConf.Load(-1) ' CDO Source Defaults Flds = iConf.Fields With Flds .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _ = "morgan" .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 .Update() End With strbody = "Hi there" & vbNewLine & vbNewLine & _ "This is line 1" & vbNewLine & _ "This is line 2" & vbNewLine & _ "This is line 3" & vbNewLine & _ "This is line 4" With iMsg .Configuration = iConf .To = "Jeremiah.Tantongco@Powerex.com" .CC = "" .BCC = "" .From = """Ron"" <ron@something.nl>" .Subject = "Important message" .TextBody = strbody .Send() End With End Sub
End Class
这是我在Excel中运行的内容:
Sub Button1_Click() CDO_Mail_Small_Text End Sub Sub CDO_Mail_Small_Text() Dim iMsg As Object Dim iConf As Object Dim strbody As String Dim Flds As Object iMsg = CreateObject("CDO.Message") iConf = CreateObject("CDO.Configuration") iConf.Load (-1) Flds = iConf.Fields With Flds .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _ = "morgan" .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 .Update End With strbody = "Hi there" & vbNewLine & vbNewLine & _ "This is line 1" & vbNewLine & _ "This is line 2" & vbNewLine & _ "This is line 3" & vbNewLine & _ "This is line 4" With iMsg .Configuration = iConf .To = "Jeremiah.Tantongco@Powerex.com" .CC = "" .BCC = "" .From = """Ron"" <ron@something.nl>" .Subject = "Important message" .TextBody = strbody .Send End With End Sub
不起作用的原因是:
“运行时错误\'91\':
对象变量或With块变量未设置”
当我进行调试时,它带我到以下行:
“iMsg = CreateObject(“CDO.Message”)”
谢谢,
-Jeremiah Tantongco
admin 更改状态以发布 2023年5月21日
VBA 是经过即时编译的经典 VB 版本。我一直认为它是全面的 VB6.0 和 VB 脚本之间的一种方法。关键是你只能访问基本的 VB6.0 库和其他 COM 库。因为许多好的 COM 库几乎总是可用的(如脚本、ADO 2.6、Office 库如 Excel 和 Word 等),这实际上非常强大。
然而,这不是 .NET,你根本无法访问 .NET 库。当你说 Visual Studio 时,你是指 Visual Studio 6.0 吗?如果你从 VS.NET 复制代码到 Excel,它没有任何机会工作。但是如果你从 VS6(或更早版本)复制代码到 Excel VBA,你应该能够让其工作。你可能只需要引用在 VS 中引用的库。我们需要更多的信息和当然错误信息。