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

将VB6代码从Outlook 2007升级到Outlook 2010,vb6,mapi,outlook-2010,Vb6,Mapi,Outlook 2010,我们希望升级VB6代码以使用Outlook 2010,但出现以下错误: 活动x无法创建对象 这是我们当前的代码: Public Sub SendEmail() Set emailOutlookApp = CreateObject("Outlook.Application.12") Set emailNameSpace = emailOutlookApp.GetNamespace("MAPI") Set emailFolder = emailNameSpace.GetDefault

我们希望升级VB6代码以使用Outlook 2010,但出现以下错误:
活动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”是否正确。但是我找不到一个明确的答案。

试试
“Outlook.Application.14”
。但不确定这是否相关:

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

试试
“Outlook.Application.14”
。但不确定这是否相关:


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

以下是我为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 = "blahblah@blah.com"
    .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

以下是我在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 = "blahblah@blah.com"
    .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

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

为什么要显式指定版本?为什么不简单
为Outlook 2010设置emailOutlookApp=CreateObject(“Outlook.Application”)

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

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

快速搜索后,2010年似乎是
“Outlook.Application.14”
快速搜索后,2010年似乎是
“Outlook.Application.14”
不知道可以不显式地完成P我会试试看。我不知道可以不明确地做到P我来试试。