寻找与VB(MSVisualStudio 2005)和VBA(Excel)不同的东西
我在网上找到了我想要做的事情的代码。像往常一样,我在Visual Studio中解雇了它,它没有任何问题。寻找与VB(MSVisualStudio 2005)和VBA(Excel)不同的东西
问题发生在于,当我尝试将它移植到Excel时,它停止工作。 据我所知,VBA是一个淡化的VB版本。 (基于阅读这篇文章: Difference between Visual Basic 6.0 and VBA)
因此,如何找出在两种编程环境之间会发生什么?
给予更多的细节: 我在Visual Studio中编写了一个程序,当我按下按钮时向我发送一封电子邮件。 然后我尝试将它作为宏移植到Excel中,但这并不起作用。
编辑:Adeed附加问题的信息
下面是我在Visual Studio 公共类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 = "[email protected]"
.CC = ""
.BCC = ""
.From = """Ron"" <ron[email protected]>"
.Subject = "Important message"
.TextBody = strbody
.Send()
End With
End Sub
末级
以下是我已经在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 = "[email protected]"
.CC = ""
.BCC = ""
.From = """Ron"" <[email protected]>"
.Subject = "Important message"
.TextBody = strbody
.Send
End With
End Sub
不工作是: “运行时错误‘91’: 对象变量或带块变量未设置”
当调试它带我到下面的行: “IMSG =的CreateObject(” CDO.Message“)”
干杯, -Jeremiah Tantongco
在VB6/VBA您需要使用SET语句对象
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
谢谢。它现在有效!我猜在语法上有细微差别。我的阅读让我相信语法是相同的! – Zigu 2009-10-19 18:41:56
VBA是一个编译上即时版本经典 VB的。我一直认为它完全VB 6.0和VB脚本之间的中途。关键是你只能访问基本的VB 6.0库和其他COM库。因为许多好的COM库几乎总是可用的(比如Scripting,ADO 2.6,Excel库和Word等Office库),这实际上非常强大。
但是,这不是.NET,并且您无论如何都无法访问.NET库。当你说Visual Studio时,你的意思是Visual Studio 6.0吗?如果您将代码从VS.NET复制到Excel,那没有任何工作的机会。但是,如果您将代码从VS6(或更早版本)复制到Excel VBA,那么您应该能够正常工作。您可能只需引用您在VS中引用的库。我们需要更多的信息,当然还有错误。
提供代码或至少是错误的工作时,当“停止工作”。 – 2009-10-16 23:58:15