Visual studio 查找与VB(MSVisualStudio 2005)和VBA(Excel)的不同之处
我在网上找到了我想做的事情的代码。 像往常一样,我在VisualStudio中启动了它,它工作起来没有问题 问题在于,当我尝试将其移植到Excel时,它停止工作。 据我所知,VBA是VB的简化版。(基于阅读本文: ) 因此,如何找出在这两种编程环境之间所失去的东西 更详细地说: 我在VisualStudio中编写了一个程序,当我按下按钮时,它会向我发送一封电子邮件。 然后我尝试将其作为宏移植到Excel中,但没有成功 编辑:编辑其他问题信息 以下是我在VisualStudio中的内容 公开课表格1Visual studio 查找与VB(MSVisualStudio 2005)和VBA(Excel)的不同之处,visual-studio,excel,vba,Visual Studio,Excel,Vba,我在网上找到了我想做的事情的代码。 像往常一样,我在VisualStudio中启动了它,它工作起来没有问题 问题在于,当我尝试将其移植到Excel时,它停止工作。 据我所知,VBA是VB的简化版。(基于阅读本文: ) 因此,如何找出在这两种编程环境之间所失去的东西 更详细地说: 我在VisualStudio中编写了一个程序,当我按下按钮时,它会向我发送一封电子邮件。 然后我尝试将其作为宏移植到Excel中,但没有成功 编辑:编辑其他问题信息 以下是我在VisualStudio中的内容 公开课表格
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
Private子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
CDO_Mail_Small_Text()
端接头
子CDO_邮件_小_文本()
作为对象的Dim iMsg
作为对象的Dim-iConf
像弦一样暗的链子
将FLD设置为对象
iMsg=CreateObject(“CDO.Message”)
iConf=CreateObject(“CDO.Configuration”)
iConf.Load(-1)“CDO源默认值
Flds=iConf.字段
使用FLD
.项目(”http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserver") _
“摩根”
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update()
以
strbody=“你好”&vbNewLine&vbNewLine&_
“这是第1行”&vbNewLine&_
“这是第2行”&vbNewLine&_
“这是第3行”&vbNewLine&_
“这是第四行”
与iMsg
.Configuration=iConf
.给耶利米。Tantongco@Powerex.com"
.CC=“”
.BCC=“”
.From=“”罗恩“
.Subject=“重要信息”
.TextBody=strbody
.Send()
以
端接头
末级
以下是我在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
子按钮1\u单击()
CDO\u邮件\u小文本
端接头
子CDO_邮件_小_文本()
作为对象的Dim iMsg
作为对象的Dim-iConf
像弦一样暗的链子
将FLD设置为对象
iMsg=CreateObject(“CDO.Message”)
iConf=CreateObject(“CDO.Configuration”)
iConf.Load(-1)
Flds=iConf.字段
使用FLD
.项目(”http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserver") _
“摩根”
.项目(”http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
使现代化
以
strbody=“你好”&vbNewLine&vbNewLine&_
“这是第1行”&vbNewLine&_
“这是第2行”&vbNewLine&_
“这是第3行”&vbNewLine&_
“这是第四行”
与iMsg
.Configuration=iConf
.给耶利米。Tantongco@Powerex.com"
.CC=“”
.BCC=“”
.From=“”罗恩“
.Subject=“重要信息”
.TextBody=strbody
邮寄
以
端接头
不工作的原因是:
“运行时错误'91':
对象变量或未设置块变量“
调试时,我会转到以下行:
“iMsg=CreateObject(“CDO.Message”)”
干杯,
-Jeremiah TantongcoVBA是经典VB的即时编译版本。我一直认为它介于完整的VB6.0和VB脚本之间。关键是您只能访问基本的VB6.0库和其他COM库。因为很多好的com库几乎总是可用的(比如脚本、ADO 2.6、Excel和Word等Office库),所以这实际上非常强大
但是,这不是.NET,您无法访问.NET库。当你说Visual Studio时,你是指Visual Studio 6.0吗?如果您要将代码从VS.NET复制到Excel,则不可能工作。但是,如果要将代码从VS6(或更早版本)复制到ExcelVBA,则应该能够使其正常工作。您可能只需要引用VS中引用的库。我们需要更多信息,当然还有错误。VBA是经典VB的即时编译版本。我一直认为它介于完整的VB6.0和VB脚本之间。关键是您只能访问基本的VB6.0库和其他COM库。因为很多好的com库几乎总是可用的(比如脚本、ADO 2.6、Excel和Word等Office库),所以这实际上非常强大
但是,这不是.NET,您无法访问.NET库。当你说Visual Studio时,你是指Visual Studio 6.0吗?如果您要将代码从VS.NET复制到Excel,则不可能工作。但是,如果要将代码从VS6(或更早版本)复制到ExcelVBA,则应该能够使其正常工作。您可能只需要引用VS中引用的库。我们需要更多信息,当然还有错误。无法证明代码中的差异,但下面的链接提供了用VBA发送电子邮件的好例子。也许能帮你找出哪里出了问题
无法证明代码之间的差异,但下面的链接提供了用VBA发送电子邮件的好例子。也许能帮你找出哪里出了问题
在VB6/VBA中,处理对象时需要使用SET语句
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
在VB6/VBA中,处理对象时需要使用SET语句
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
提供代码或至少在“停止工作”时提供错误。提供代码或至少在“停止工作”时提供错误。谢谢。现在可以了!我想在语法上有细微的差别。我的阅读使我相信语法是相同的!谢谢现在可以了!我想在语法上有细微的差别。我的阅读使我相信语法是相同的!