vba + Selection.Paste 到 Outlook + 控制点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24644936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:45:30  来源:igfitidea点击:

vba + Selection.Paste into outlook + control poition

excelvbaemailexcel-vbaoutlook

提问by HattrickNZ

What I am trying to do is copy a chart from excel into an outlook email, but after numerous searching I am struggling.

我想要做的是将 Excel 中的图表复制到 Outlook 电子邮件中,但经过多次搜索后,我陷入了困境。

i am having trouble positioning where the chart is pasted. I want it to paste after the last line "this is another line again "in the body of the email. It currently pastes at the start of the email before the line "test ... body"

我无法定位粘贴图表的位置。我希望它粘贴在"this is another line again "电子邮件正文的最后一行之后。它当前粘贴在电子邮件的开头行之前"test ... body"

 Sub CopyAndPasteToMailBody3() ' this works but how do i control where it puts the chart?
        Set mailApp = CreateObject("Outlook.Application")
        Set mail = mailApp.CreateItem(olMailItem)
        mail.Display
        mail.To = "[email protected]"
        mail.subject = "subject" & Now
        mail.body = "test ... body" & vbNewLine & vbNewLine _
                    & "this is another line " & vbCrLf _
                    & "this is another line again "
        Set wEditor = mailApp.ActiveInspector.wordEditor
        ActiveChart.ChartArea.Copy ' chart needs to be active
        wEditor.Application.Selection.Paste

        ' mail.send
    End Sub

Note: using excel 10 on windows 7

注意:在 Windows 7 上使用 excel 10

回答by Patrick Thompson

I have found that

我发现

Set wEditor = mailapp.ActiveInspector.WordEditor

needs to be followed by

需要遵循

wEditor.Range(0, 0).Select

to avoid an error sometimes when you go to paste it.

以避免有时在粘贴时出错。

回答by user3514930

You can modify the code put the Body on the Clipboard and Paste it:

您可以修改代码将 Body 放在剪贴板上并粘贴它:

    Set mailApp = CreateObject("Outlook.Application")
    Set mail = mailApp.CreateItem(olMailItem)
    mail.Display
    mail.To = "[email protected]"
    mail.Subject = "subject" & Now

    Dim Clip As MSForms.DataObject
    Set Clip = New MSForms.DataObject
    Clip.SetText ("test ... body" & vbNewLine & vbNewLine _
                & "this is another line " & vbCrLf _
                & "this is another line again " & vbNewLine & " ")
    Clip.PutInClipboard
    Set wEditor = mailApp.ActiveInspector.wordEditor
    wEditor.Application.Selection.Paste

    ActiveChart.ChartArea.Copy ' chart needs to be active
    wEditor.Application.Selection.Paste
    ' mail.send

In this case you can assembly the mail as you want.
MSForms.DataObjectneed to have the Reference: Microsoft Form 2.0 Object Library (FM20.DLL)

在这种情况下,您可以根据需要组装邮件。
MSForms.DataObject需要有参考:Microsoft Form 2.0 Object Library (FM20.DLL)

回答by user3514930

You can try also with another code (in this case the image are temporary saved on disk):

您也可以尝试使用其他代码(在这种情况下,图像临时保存在磁盘上):

Sub CopyAndPasteToMailBody4() ' this works but how do i control where it puts the chart?
    Set mailApp = CreateObject("Outlook.Application")
    Set mail = mailApp.CreateItem(0)
    mail.Display
    mail.To = "[email protected]"
    mail.Subject = "subject" & Now

    Dim Stri As String
    Stri = "test ... body" & vbNewLine & vbNewLine _
                & "this is another line " & vbCrLf _
                & "this is another line again " & vbNewLine & " "
    ActiveChart.Export "e:##代码##\C1.png"
    Stri = Stri & "<img src='e:##代码##\C1.png'>"
    mail.HTMLBody = Stri
    ' mail.send
End Sub

On my PC the first code ask me some permission, with the second code no...

在我的电脑上,第一个代码要求我获得一些许可,第二个代码没有...