2015-11-04 54 views
0

使用電子郵件規則觸發VBA腳本,是否可以將電子郵件正文發送到剪貼板?我發現這個method,但它似乎沒有工作。Outlook 2013規則將電子郵件正文複製到剪貼板

Sub SaveToClipboard(Item As Outlook.MailItem) 
Dim Explorer As Outlook.Explorer 
Dim oitem As Outlook.MailItem 
Dim oData As MSForms.DataObject 


Set Explorer = Application.ActiveExplorer 

If Explorer.Selection.Count Then 

    Set CurrentItem = Explorer.Selection(1) 
    Set oitem = CurrentItem 

    oData = oitem.Body 
    oData.PutInClipboard 

    'Using this to test the output 
    MsgBox oData 

End If 
End Sub 

接下來,我將如何搜索以「Info:」開頭的電子郵件正文中的一行並僅將該行復制到剪貼板?

+0

每個線程只有一個問題。首先搜索諸如「vba結構化文本」之類的東西,並在您編寫一些代碼後,在必要時開始一個新問題。 – niton

回答

0

您錯過了該網站上示例中描述的重要部分。

以及使用傳入的mailitem(itm作爲mailItem)。

Option Explicit 

Private Sub SaveToClipboard_test() 

    Dim curritem As Object 
    Set curritem = ActiveExplorer.Selection(1) 
    If TypeOf curritem Is mailItem Then SaveToClipboard curritem 

ExitRoutine: 
    Set curritem = Nothing 

End Sub 


Sub SaveToClipboard(itm As mailItem) 

    Dim oData As MSForms.DataObject 
    Dim strPaste As Variant 

    Set oData = New MSForms.DataObject 
    oData.SetText itm.body 
    oData.PutInClipboard 

    oData.GetFromClipboard 
    strPaste = oData.GetText 

    strPaste = Left(strPaste, 1024) 
    MsgBox strPaste 

ExitRoutine: 
    Set oData = Nothing 

End Sub