将 VB6 代码从 Outlook 2007 升级到 Outlook 2010

发布于 2024-11-05 01:01:56 字数 919 浏览 4 评论 0原文

我们想要升级 VB6 代码以使用 Outlook 2010,但出现以下错误: Active x 无法创建对象

这是我们当前的代码:

Public Sub SendEmail()

  Set emailOutlookApp = CreateObject("Outlook.Application.12")

  Set emailNameSpace = emailOutlookApp.GetNamespace("MAPI")
  Set emailFolder = emailNameSpace.GetDefaultFolder(olFolderInbox)

  Set emailItem = emailOutlookApp.CreateItem(olMailItem)
  Set EmailRecipient = emailItem.Recipients
  EmailRecipient.Add (EmailAddress)
  EmailRecipient.Add (EmailAddress2)

  emailItem.Importance = olImportanceHigh
  emailItem.Subject = "My Subject"
  emailItem.Body = "The Body"

'-----Send the Email-----'
  emailItem.Save
  emailItem.Send

'-----Clear out the memory space held by variables-----'
  Set emailNameSpace = Nothing
  Set emailFolder = Nothing
  Set emailItem = Nothing
  Set emailOutlookApp = Nothing
Exit Sub

我不确定“Outlook.Application.12”是否正确。但我无法为此找到明确的答案。

We want to upgrade our VB6 code to use Outlook 2010, but we're getting the following error:
Active x cannot create object

This is our current code:

Public Sub SendEmail()

  Set emailOutlookApp = CreateObject("Outlook.Application.12")

  Set emailNameSpace = emailOutlookApp.GetNamespace("MAPI")
  Set emailFolder = emailNameSpace.GetDefaultFolder(olFolderInbox)

  Set emailItem = emailOutlookApp.CreateItem(olMailItem)
  Set EmailRecipient = emailItem.Recipients
  EmailRecipient.Add (EmailAddress)
  EmailRecipient.Add (EmailAddress2)

  emailItem.Importance = olImportanceHigh
  emailItem.Subject = "My Subject"
  emailItem.Body = "The Body"

'-----Send the Email-----'
  emailItem.Save
  emailItem.Send

'-----Clear out the memory space held by variables-----'
  Set emailNameSpace = Nothing
  Set emailFolder = Nothing
  Set emailItem = Nothing
  Set emailOutlookApp = Nothing
Exit Sub

I'm not sure if "Outlook.Application.12" is correct. But I can't find a definitive answer for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红衣飘飘貌似仙 2024-11-12 01:01:57

对于 Outlook 2010,这绝对是正确的 Outlook.Application.14
但是,我不知道office 2007怎么样。
我认为它是 Outlook.Application.12 ,对于较低版本,它只是“Outlook.Application”。

For Outlook 2010, this is definitly corect Outlook.Application.14.
But, I don't know what about office 2007.
I think it's Outlook.Application.12 and for lower versions it is simply "Outlook.Application".

你的往事 2024-11-12 01:01:57

这是我在 2010 年改用的代码:

Private Sub EmailBlahbutton_Click()

Dim mOutlookApp As Object
Dim OutMail As Object
Dim Intro As String

On Error GoTo ErrorHandler

Set mOutlookApp = GetObject("", "Outlook.application")
Set OutMail = mOutlookApp.CreateItem(0)

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

'These are the ranges being emailed.
ActiveSheet.Range(blahblahblah).Select

'Intro is the first line of the email
Intro = "BLAHBLAHBLHA"

'Set the To and Subject lines.  Send the message.
With OutMail
    .To = "[email protected]"
    .Subject = "More BLAH here"
    .HTMLBody = Intro & RangetoHTML(Selection)
    .Send
End With

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

ActiveSheet.Range("A1").Select
ActiveWindow.ScrollColumn = ActiveCell.Column
ActiveWindow.ScrollRow = ActiveCell.Row

Set OutMail = Nothing
Set mOutlookApp = Nothing

Exit Sub

ErrorHandler:
    Set mOutlookApp = CreateObject("Outlook.application")
    Resume Next

End Sub

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
  .Cells(1).PasteSpecial Paste:=8
  .Cells(1).PasteSpecial xlPasteValues, , False, False
  .Cells(1).PasteSpecial xlPasteFormats, , False, False
  .Cells(1).Select
  Application.CutCopyMode = False
  On Error Resume Next
  .DrawingObjects.Visible = True
  .DrawingObjects.Delete
  On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
  SourceType:=xlSourceRange, _
  Filename:=TempFile, _
  Sheet:=TempWB.Sheets(1).Name, _
  Source:=TempWB.Sheets(1).UsedRange.address, _
  HtmlType:=xlHtmlStatic)
  .Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                      "align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing

End Function

Here's the code I switched to for 2010:

Private Sub EmailBlahbutton_Click()

Dim mOutlookApp As Object
Dim OutMail As Object
Dim Intro As String

On Error GoTo ErrorHandler

Set mOutlookApp = GetObject("", "Outlook.application")
Set OutMail = mOutlookApp.CreateItem(0)

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

'These are the ranges being emailed.
ActiveSheet.Range(blahblahblah).Select

'Intro is the first line of the email
Intro = "BLAHBLAHBLHA"

'Set the To and Subject lines.  Send the message.
With OutMail
    .To = "[email protected]"
    .Subject = "More BLAH here"
    .HTMLBody = Intro & RangetoHTML(Selection)
    .Send
End With

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

ActiveSheet.Range("A1").Select
ActiveWindow.ScrollColumn = ActiveCell.Column
ActiveWindow.ScrollRow = ActiveCell.Row

Set OutMail = Nothing
Set mOutlookApp = Nothing

Exit Sub

ErrorHandler:
    Set mOutlookApp = CreateObject("Outlook.application")
    Resume Next

End Sub

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
  .Cells(1).PasteSpecial Paste:=8
  .Cells(1).PasteSpecial xlPasteValues, , False, False
  .Cells(1).PasteSpecial xlPasteFormats, , False, False
  .Cells(1).Select
  Application.CutCopyMode = False
  On Error Resume Next
  .DrawingObjects.Visible = True
  .DrawingObjects.Delete
  On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
  SourceType:=xlSourceRange, _
  Filename:=TempFile, _
  Sheet:=TempWB.Sheets(1).Name, _
  Source:=TempWB.Sheets(1).UsedRange.address, _
  HtmlType:=xlHtmlStatic)
  .Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                      "align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing

End Function
轻许诺言 2024-11-12 01:01:57

为什么要明确指定版本?为什么不简单地
设置 emailOutlookApp = CreateObject("Outlook.Application")

Why do you explicitly specify the version? Why not simply
Set emailOutlookApp = CreateObject("Outlook.Application")

余厌 2024-11-12 01:01:57

尝试“Outlook.Application.14”。不确定这是否相关: 2007 至 2010 升级问题

我意识到这不是确切的问题,但它可能会引导您走上正确的道路。

Try "Outlook.Application.14". Not sure if this is related though: 2007 to 2010 upgrade issue

I realize it's not the exact issue, but it may lead you down the right path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文