[英]Reply to specific email in Outlook folder
我正在嘗試使用VBA在Outlook收件箱中搜索文件夾,並使其回復具有給定主題的最新電子郵件。 到目前為止,我有以下代碼:
Dim Fldr As Outlook.Folder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
'Dim IsExecuted As Boolean
Set Fldr = Session.GetDefaultFolder(olFolderInbox).folders("Refund Correspondence")
' IsExecuted = False
Set olItems = Fldr.Items
olItems.Sort "[Received]", True
For i = 1 To olItems.Count
Set olMail = olItems(i)
If InStr(olMail.subject, Me.Vendor_Client & " Tax Refund Request - " & Me.Vendor_Name) > 0 Then
' If Not IsExecuted Then
If Not olMail.categories = "Executed" Then
Set olReply = olMail.ReplyAll
With olReply
.BodyFormat = olFormatHTML '''This is where I'm running into trouble
.Display
.To = Me.Vendor_E_mail
.subject = Me.Vendor_Client & " Tax Refund Request - " & Me.Vendor_Name
End With
Exit For
olMail.categories = "Executed"
' IsExecuted = True
End If
End If
Next i
在我從事的其他項目中,我只需要從頭開始創建電子郵件,就可以使用Ron DeBruin的RangeToHTML(selection)使用包含特定單詞的現有電子郵件模板將指定范圍粘貼到我的電子郵件中和replace函數將單詞替換為表格。 但是,對於這個項目,我想回復一個現有的電子郵件鏈。 由於我無法引用電子郵件模板,而無法用要插入的表格替換單詞,所以我很茫然。 .bodyFormat = olFormatHTML確實可以答復我想要的電子郵件,而響應的其余部分位於我的響應下方,但是此后我不知道如何將想要的表粘貼到電子郵件中。 我嘗試使用.HTMLBody = rangetohtml(selection)函數,但這僅創建了一封新電子郵件,而鏈中沒有以前的電子郵件。
如果將Word用作電子郵件編輯器,則此方法有效。 請嘗試以下中間部分的代碼。 我假設您已將指定范圍復制到剪貼板中。
內部:
' needs a reference to the Microsoft Word x.x Object Library
With olReply
.Display
Dim wdDoc As Word.Document
Set wdDoc = .GetInspector.WordEditor
If Not wdDoc Is Nothing Then
With wdDoc.Range
.Collapse wdCollapseStart
.InsertBefore "Hi," & vbCrLf & vbCrLf & _
"here comes my inserted table:" & vbCrLf
.Collapse wdCollapseEnd
.InsertAfter "Best wishes," & vbCrLf & _
"..." & vbCrLf
.Collapse wdCollapseStart
.Paste
'.PasteAndFormat wdChartPicture
'.PasteAndFormat wdFormatPlainText
End With
End If
Set wdDoc = Nothing
End With
如果您想知道粘貼部分之前和之后插入文本的順序:如果通過.PasteAndFormat wdFormatPlainText
粘貼純文本,則光標不會在文本之后移動。 因此,在任何粘貼變體中,am命令對我來說都可以正常工作。
如果需要調試光標位置,只需在With wdDoc.Range
區域內添加一些.Select
(僅用於調試目的)。
面向未來讀者的“完整”示例:
Public Sub PasteExcelRangeToEmail()
Dim objOL As Outlook.Application
Dim NewEmail As Outlook.MailItem
Dim wdDoc As Word.Document
Dim wdRange As Word.Range
' get your Outlook object
On Error Resume Next
If objOL Is Nothing Then
Set objOL = GetObject(, "Outlook.Application")
If objOL Is Nothing Then
Set objOL = New Outlook.Application
End If
End If
On Error GoTo 0
Set NewEmail = objOL.CreateItem(olMailItem)
With NewEmail
.To = "info@world"
.Subject = "Concerning ..."
.Display
Set wdDoc = .GetInspector.WordEditor
If Not wdDoc Is Nothing Then
With wdDoc.Range
.Collapse wdCollapseStart
.InsertBefore "Hi there," & vbCrLf & "here's my table:" & vbCrLf
.Collapse wdCollapseEnd
.InsertAfter "Best wishes," & vbCrLf
.Collapse wdCollapseStart
ActiveSheet.Range("A1:C3").Copy
.Paste
'.PasteAndFormat wdChartPicture
'.PasteAndFormat wdFormatPlainText
End With
Set wdDoc = Nothing
End If
'.Send
End With
Set NewEmail = Nothing
Set objOL = Nothing
Application.CutCopyMode = False
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.