如何调整在Outlook电子邮件中粘贴的图片大小?

huangapple go评论189阅读模式
英文:

How do I adjust the size of the pasted image in Outlook email?

问题

在网站上查看旧问题时,我找到了所需的代码,但我需要调整电子邮件中粘贴的图像的大小(高度和宽度),但我没有成功。你能帮助我吗?

Sub SendEmail()
    '打开一个新邮件项
    Set outlookApp = CreateObject("Outlook.Application")

    Set OutMail = outlookApp.CreateItem(olMailItem)

    With OutMail
        .To = ""
        .Subject = "** 请在上午10:30之前确认工时表 **"
        .Importance = olImportanceHigh
        .Display
    End With

    '获取其Word编辑器
    Set wordDoc = OutMail.GetInspector.WordEditor

    '以图片方式粘贴
    rng.Copy
    wordDoc.Range.PasteSpecial , , , , wdPasteBitmap

    OutMail.HTMLBody = "Timesheets Submitted by " & "Marco" & "<br>" & _
    vbNewLine & OutMail.HTMLBody
End Sub

我尝试创建一些命令来调整图像大小,但没有成功。

英文:

Looking at old questions here on the site I found the code I needed, but I need to adjust the size (height and width) of the image pasted in the email, but I was unsuccessful. Can you help me?

Sub SendEmail()
    &#39;Open a new mail item
    Set outlookApp = CreateObject(&quot;Outlook.Application&quot;)
   
    Set OutMail = outlookApp.CreateItem(olMailItem)
    
    With OutMail
        .To = &quot;&quot;
        .Subject = &quot;** Please confirm Timesheet by 10:30AM **&quot;
        .Importance = olImportanceHigh
        .Display
    End With

    &#39;Get its Word editor
    Set wordDoc = OutMail.GetInspector.WordEditor

    &#39;To paste as picture
    rng.Copy
    wordDoc.Range.PasteSpecial , , , , wdPasteBitmap

    OutMail.HTMLBody = &quot;Timesheets Submitted by &quot; &amp; &quot;Marco&quot; &amp; &quot;&lt;br&gt;&quot; &amp; _
    vbNewLine &amp; OutMail.HTMLBody
End Sub

I tried to create some command to resize image size but without success.

答案1

得分: 2

这是你正在尝试的吗?我已经对代码进行了注释,但如果你遇到困难,可以随时问。

    选项 显式
    
    '~~> 由于我们使用的是后期绑定
    
    '~~> Outlook 常量
    Private Const olImportanceHigh = 2
    Private Const olMailItem = 0

    '~~> Word 常量
    Private Const wdChartPicture = 13
    
    Sub 发送邮件()
        '~~> 工作表操作
        Dim ws As 工作表
        Dim rng As 范围
        Dim pic As 图片
            
        '~~> 将此更改为相关工作表
        Set ws = Sheet1
        '~~> 将此更改为相关范围
        Set rng = ws.Range("A1:A15")
        
        '~~> 复制范围并将其粘贴到图片对象中
        rng.Copy
        Set pic = ws.Pictures.Paste
        
        '~~> 在此设置尺寸
        With pic.ShapeRange
            .LockAspectRatio = msoTrue
            .Height = 200
            .Width = 200
        End With
        
        '~~> Outlook 操作
        Dim OutApp As 对象
        Dim OutMail As 对象
     
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(olMailItem)
        
        With OutMail
            .To = ""
            .Subject = "** 请在上午10:30之前确认时间表 **"
            .Importance = olImportanceHigh
            .Display
        End With
    
        Dim wordDoc As 对象
        Set wordDoc = OutMail.GetInspector.WordEditor
        
        '~~> 剪切图片并粘贴到电子邮件中
        pic.Cut
        DoEvents
        
        wordDoc.Range.pasteandformat wdChartPicture
    
        OutMail.HTMLBody = "Marco提交的时间表" & _
                           "<br>" & OutMail.HTMLBody
    End Sub

**一个重要的提示**:始终声明并使用对象/变量。这将让你的生活更轻松...
英文:

Is this what you are trying? I have commented the code but if you get stuck then simply ask.

Option Explicit

&#39;~~&gt; Since we are working using Late Binding

&#39;~~&gt; Outlook Constants
Private Const olImportanceHigh = 2
Private Const olMailItem = 0

&#39;~~&gt; Word Constant
Private Const wdChartPicture = 13

Sub SendEmail()
    &#39;~~&gt; Worksheet Operations
    Dim ws As Worksheet
    Dim rng As Range
    Dim pic As Picture
        
    &#39;~~&gt; Change this to the relevant sheet
    Set ws = Sheet1
    &#39;~~&gt; Change this to the relevant range
    Set rng = ws.Range(&quot;A1:A15&quot;)
    
    &#39;~~&gt; Copy the range and paste it in a picture object
    rng.Copy
    Set pic = ws.Pictures.Paste
    
    &#39;~~&gt; Set the dimensions here
    With pic.ShapeRange
        .LockAspectRatio = msoTrue
        .Height = 200
        .Width = 200
    End With
    
    &#39;~~&gt; Outlook Operations
    Dim OutApp As Object
    Dim OutMail As Object
 
    Set OutApp = CreateObject(&quot;Outlook.Application&quot;)
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    With OutMail
        .To = &quot;&quot;
        .Subject = &quot;** Please confirm Timesheet by 10:30AM **&quot;
        .Importance = olImportanceHigh
        .Display
    End With

    Dim wordDoc As Object
    Set wordDoc = OutMail.GetInspector.WordEditor
    
    &#39;~~&gt; Cut the picture and paste in email
    pic.Cut
    DoEvents
    
    wordDoc.Range.pasteandformat wdChartPicture

    OutMail.HTMLBody = &quot;Timesheets Submitted by Marco&quot; &amp; _
                       &quot;&lt;br&gt;&quot; &amp; _
                       vbNewLine &amp; OutMail.HTMLBody
End Sub

One important tip: Always declare and work with Objects/Variables. Will make your life easier...

答案2

得分: 1

使用Word对象模型粘贴内容后,您可以通过指定img元素的heightwidth来编辑生成的HTML标记。

或者只需使用Siddharth建议的Range.PasteAndFormat方法。

英文:

After using the Word object model for pasting the content you can edit the resulted HTML markup by specifying the height and width for the img element.

Or just use the Range.PasteAndFormat method which Siddharth suggested.

huangapple
  • 本文由 发表于 2023年2月27日 03:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定