热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Excel2010将范围和图片粘贴到Outlook中-Excel2010PasteRangeandPictureintoOutlook

Iamhavingconsiderabledifficultyfiguringthisoneout.IcanpastearangeasHTMLwithoutissue

I am having considerable difficulty figuring this one out. I can paste a range as HTML without issues, but in some communications we want to past the range as a picture instead. I can create a range and save it as a picture, but I cannot figure out how to past the picture into Outlook after it is created.

我很难解决这个问题。我可以将范围粘贴为HTML而不会出现问题,但在某些通信中,我们希望将范围作为图片而不是。我可以创建一个范围并将其保存为图片,但我无法弄清楚如何在创建后将图片传递到Outlook中。

If you are just looking for code that will copy a range and paste it into Outlook, this works great. All of the email data is referencing cells on a tab called Mail, so you can simply copy and paste the Mail tab and the macro into any workbook and add email automation by editing the fields on the mail tab and not changing the macro. If you use this code, make sure to reference Microsoft Outlook x.x Object Library (In VBA Window: Tools - References - Microsoft Outlook x.x Object Library).

如果您只是在寻找能够复制范围并将其粘贴到Outlook中的代码,那么这非常有用。所有电子邮件数据都在名为Mail的选项卡上引用单元格,因此您只需将“邮件”选项卡和宏复制并粘贴到任何工作簿中,并通过编辑邮件选项卡上的字段而不更改宏来添加电子邮件自动化。如果使用此代码,请确保引用Microsoft Outlook x.x对象库(在VBA窗口中:工具 - 引用 - Microsoft Outlook x.x对象库)。

I need to take this one step further and be able to turn the range into a picture and paste it into the email. I can attach it, but I cannot insert it into the body, which is what I need. I have looked at several examples, including those on Ron DeBruins website, but I have not been able to get any of them to work. I am running Windows 7 x64 With Office 2010 x64.

我需要更进一步,并能够将范围转换为图片并将其粘贴到电子邮件中。我可以附上它,但我不能将它插入身体,这是我需要的。我看了几个例子,包括Ron DeBruins网站上的例子,但是我还没能让它们中的任何一个工作。我使用Office 2010 x64运行Windows 7 x64。

Here is the code I am running to paste a range.

这是我运行的用于粘贴范围的代码。

Option Explicit

Sub Mail_AS_Range()

' Working in Office 2010-2013
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String

On Error Resume Next

Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Sheets(sh.Range("C11").Value).Select
ActiveWorkbook.Save


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
    .SentOnBehalfOfName= sh.Range("C4")  'This allows us to send from an alternate email address
    .Display  'Alternate send address will not work if we do not display the email first.
              'I dont know why but this step is a MUST
    .To = sh.Range("C5")
    .CC = sh.Range("C6")
    .BCC = sh.Range("C7")
    .Subject = sh.Range("C8").Value
    .HTMLBody = "
" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & .HTMLBody ' This is where the body of the email is pulled together. '
is an HTML tag to turn the text into HTML ' strbody is your text from cell C9 on the mail tab ' fncRangetoHtml is converting the range you specified into HTML ' .HTMLBody inserts your email signature .Attachments.Add sh.Range("C10").Value '.Send End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub Private Function fncRangeToHtml( _ strWorksheetName As String, _ strRangeAddress As String) As String ' This is creating a private function to make the range specified in the Mail macro into HTML Dim objFilesytem As Object, objTextstream As Object, objShape As Shape Dim strFilename As String, strTempText As String Dim blnRangeContainsShapes As Boolean strFilename = Environ$("temp") & "\" & _ Format(Now, "dd-mm-yy_h-mm-ss") & ".htm" ThisWorkbook.PublishObjects.Add( _ SourceType:=xlSourceRange, _ Filename:=strFilename, _ Sheet:=strWorksheetName, _ Source:=strRangeAddress, _ HtmlType:=xlHtmlStatic).Publish True Set objFilesytem = CreateObject("Scripting.FileSystemObject") Set objTextstream = objFilesytem.GetFile(strFilename).OpenAsTextStream(1, -2) strTempText = objTextstream.ReadAll objTextstream.Close strTempText = Replace(strTempText, "align=center x:publishsource=", "align=left x:publishsource=") For Each objShape In Worksheets(strWorksheetName).Shapes If Not Intersect(objShape.TopLeftCell, Worksheets( _ strWorksheetName).Range(strRangeAddress)) Is Nothing Then blnRangeCOntainsShapes= True Exit For End If Next If blnRangeContainsShapes Then strTempText = fncConvertPictureToMail(strTempText, Worksheets(strWorksheetName)) fncRangeToHtml = strTempText Set objTextstream = Nothing Set objFilesytem = Nothing Kill strFilename End Function Public Function fncConvertPictureToMail(strTempText As String, objWorksheet As Worksheet) As String Const HTM_START = "") - lngPathLeft) strTemp = Replace(strTemp, HTM_START & Chr$(34), "") strTemp = Replace(strTemp, HTM_END & Chr$(34), "") strTemp = strTemp & "/" strTempText = Replace(strTempText, strTemp, Environ$("temp") & "\" & strTemp) fncCOnvertPictureToMail= strTempText End Function

Any suggestions would be appreciated. Thanks!

任何建议,将不胜感激。谢谢!

1 个解决方案

#1


2  

Thank you to BP_ who directed me to a link, which answered my question. Here is my code after modifying for my application.

感谢BP_谁指导我一个回答我问题的链接。这是修改我的应用程序后的代码。

This allows me to set all the variables within a tab in Excel and not edit the query itself. I use this method because some folks on my team are not comfortable editing VBA.

这允许我在Excel中的选项卡中设置所有变量,而不是编辑查询本身。我使用这种方法是因为我团队中的一些人不习惯编辑VBA。

Sub Mail_W_Pic()

Dim TempFilePath As String
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Dim width As String
Dim height As String

On Error Resume Next

Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Sheets(sh.Range("C11").Value).Select
width = (sh.Range("C15").Value)
height = (sh.Range("C16").Value)

    'Create a new Microsoft Outlook session
    Set OutApp = CreateObject("outlook.application")
    'create a new message
    Set OutMail = OutApp.CreateItem(olMailItem)

    With OutMail
        .SentOnBehalfOfName= sh.Range("C4")
        .Display
        .Subject = sh.Range("C8").Value
        .To = sh.Range("C5")
        .CC = sh.Range("C6")
        .BCC = sh.Range("C7")
        'first we create the image as a JPG file
        Call createJpg(sh.Range("C13").Value, sh.Range("C14").Value, "DashboardFile")
        'we attached the embedded image with a Position at 0 (makes the attachment hidden)
        TempFilePath = Environ$("temp") & "\"
        .Attachments.Add TempFilePath & "DashboardFile.jpg", olByValue, 0

        'Then we add an html  link to this image
        'Note than you can customize width and height - not mandatory

        .HTMLBody = "
" & strbody & "

" _ & "Best Regards,
Ed" & .HTMLBody .Display '.Send End With Set sh = Nothing End Sub Sub createJpg(Namesheet As String, nameRange As String, nameFile As String) ThisWorkbook.Activate Worksheets(Namesheet).Activate Set Plage = ThisWorkbook.Worksheets(Namesheet).Range(nameRange) Plage.CopyPicture With ThisWorkbook.Worksheets(Namesheet).ChartObjects.Add(Plage.Left, Plage.Top, Plage.width, Plage.height) .Activate .Chart.Paste .Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG" End With Worksheets(Namesheet).ChartObjects(Worksheets(Namesheet).ChartObjects.Count).Delete Set Plage = Nothing End Sub

推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
author-avatar
hedongsheng
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有