Outlook

Excel 宏:執行時錯誤“1004”一般郵件失敗

  • June 8, 2011

我們計劃盡快將所有使用者升級到 Outlook 2010 和 Exchange 2010,但在 Outlook 遷移中遇到了一些麻煩。我們有一個 Excel 電子表格,它基本上是一份費用報告,當他們點擊電子表格中的按鈕時,它會自動通過電子郵件發送給相應的人員。它使用一些宏(我不是程序員)來實現這一點。它適用於我們現在擁有的 Outlook 2003,但我們的測試使用者組無法使用它 - 他們收到以下錯誤。

任何人都可以幫助解決這個問題。它快把我逼瘋了!!

我嘗試在 Outlook 打開和關閉的情況下發送它。 在此處輸入圖像描述

更新:已解決

問題出在我們的 citrix 農場設置…由於 MS 不允許並排安裝 Outlook 版本,就像它們與所有其他辦公應用程序一樣,我們必須在“測試”xen 應用伺服器上安裝 Outlook 2010。由於這些使用者是測試 Outlook 2010 使用者,因此他們的配置文件僅有權使用 Outlook 2010 和 Outlook 2010 作為其預設電子郵件客戶端。當他們在生產 xenapp 農場上打開 excel 2003 中的 spreadhseet 時,問題就出現了 - 沒有安裝 Outlook 2010。因此它試圖通過該伺服器上不存在的預設電子郵件客戶端(outlook 2010)發送郵件。而且由於他們的帳戶僅配置為使用 Outlook 2010,excel 不知道該怎麼做。

我們的解決方案是在 citrix 中創建一個新的 Excel 發布應用程序,該應用程序安裝在與我們的 Outlook 2010 安裝相同的伺服器上,將伺服器場限制為該測試伺服器,並將該應用程序發布給我們的測試使用者,同時刪除他們的舊 excel應用程序。這樣,測試伺服器上只執行 Outlook 和 Excel。

我只能向您展示我必須訪問的程式碼,以便我的電子郵件觸發 Excel 電子表格中的按鈕可以正常工作。他們改變了一些東西,所以舊程式碼不能很好地工作。

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

引用自:https://serverfault.com/questions/278027