将特定的工作表范围粘贴到电子邮件正文中。

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

Paste specific worksheet range into email body

问题

I'm here to help you with the translation. Here's the translated content:

我是相当新的VBA编程,我需要帮助解决我工作中需要的问题。
我创建了一个工作表,以减少我的团队成员在工作日内所花费的时间,为了提供更多信息,我们使用这个工作表来填写团队处理的所有关键问题的信息。
当发生关键问题时,团队负责向公司发送通告,由于旧流程导致通告缺乏标准化,因此我决定创建了一个自定义电子邮件布局的工作表。
为了防止工作人员编辑此布局,我保护了工作表并创建了带有宏的按钮来生成这些通告,但是如同新手所期望的那样,它的工作效果不太理想。

以下是生成通告的工作表的屏幕截图。单元格从B6开始,结束于L27。工作表标识是“Planilha5”,我需要的是:当工作人员单击工作表中的“发送”按钮时,将在Microsoft Outlook 365中调用新消息,并填写VBA脚本中定义的信息,并且必须将工作表定义的范围粘贴到邮件消息的正文中。

通告截图

这是我正在尝试修改以供使用的VBA脚本:

Sub EnviarAbertura()
Dim Outlook As Object, Novo_Email As Object, Email_Body As String, b1 As Variant

b1 = Planilha5.Range("B6:L27")

Set Outlook = CreateObject("Outlook.Application")

    Email_Body = b1
    
    Set Novo_Email = Outlook.CreateItem(0)
    Corpo = Planilha5.Range("B6:L27").Copy
    
    With Novo_Email
    .SentOnBehalfOfName = "centraldecomandotd@natura.net"
    .Subject = Planilha5.Range("G4")
    .Body = Email_Body
    .Display
    
    End With
    
End Sub

当我注释掉这些行'b1 = Planilha5.Range("B6:L27")';'Email_Body = b1';'.Body = Email_Body',脚本可以正常工作,但邮件正文未填充。

VBA脚本正常工作

我手动粘贴了工作表范围,如下截图所示,以展示我希望VBA脚本执行的操作。

预期结果

请问是否有人可以帮助我解决这个VBA脚本的问题?

英文:

I'm quite new VBA programming and I need help with something that I need at my work.
I have a sheet that I created to reduce the time spent by my team workers on their job during the day and just to give more information, we use this sheet to fill all the information regarding critical issues that the team manage into war rooms.
The team is responsible to send announcements for the company when a critial issue is hapening and as the old process were generating a lack of standardization in these announcements, I decided to create this sheet with a custom layout for e-mails.
And to prevent workers to edit this layout, I protected the worksheets and created buttons with macros to generate these announcements, but as spected from a newbie person, it's not working properly as spected.

Below is a screenshot from this worksheet where the announcement is generated. The cells starts at B6 and ends at L27. The worksheet identification is "Planilha5" and what I need is: When the worker click over the SEND button present into the worksheet, a new message into Microsoft Outlook 365 is invoked and filled with the defined information into the VBA script and the worksheet defined range must be pasted into the body of the mail message.

Announcement screenshot

And this is the VBA script that I'm trying to modify to be used:

Sub EnviarAbertura()
Dim Outlook As Object, Novo_Email As Object, Email_Body As String, b1 As Variant

b1 = Planilha5.Range("B6:L27")

Set Outlook = CreateObject("Outlook.application")

    Email_Body = b1
    
    Set Novo_Email = Outlook.CreateItem(0)
    Corpo = Planilha5.Range("B6:L27").Copy
    
    With Novo_Email
    .SentOnBehalfOfName = "centraldecomandotd@natura.net"
    .Subject = Planilha5.Range("G4")
    .Body = Email_Body
    .Display
    
    End With
    
End Sub

When I comment the lines 'b1 = Planilha5.Range("B6:L27")'; 'Email_Body = b1'; '.Body = Email_Body', the script works properly without the body filled.

VBA Script working properly

I have pasted manually the worksheet range into a screenshot below to show you guys what I'm specting to do with the VBA Script.

Expected results

Please, could someone help me with this VBA script?

答案1

得分: 2

有两种主要的方法可以基于Excel数据设置邮件正文:

  1. 基于工作表内容构建HTML标记。这基本上是Ron在邮件正文中的邮件范围/选择页面上描述的内容,您可以在该页面找到以下示例代码:

    Function RangetoHTML(rng As Range)
     Dim fso As Object
     Dim ts As Object
     Dim TempFile As String
     Dim TempWB As Workbook
    
     TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    
     '复制范围并创建一个新工作簿以粘贴数据
     rng.Copy
     Set TempWB = Workbooks.Add(1)
     With TempWB.Sheets(1)
         .Cells(1).PasteSpecial Paste:=8
         .Cells(1).PasteSpecial xlPasteValues, , False, False
         .Cells(1).PasteSpecial xlPasteFormats, , False, False
         .Cells(1).Select
         Application.CutCopyMode = False
         On Error Resume Next
         .DrawingObjects.Visible = True
         .DrawingObjects.Delete
         On Error GoTo 0
     End With
    
     '将工作表发布到htm文件
     With TempWB.PublishObjects.Add( _
          SourceType:=xlSourceRange, _
          Filename:=TempFile, _
          Sheet:=TempWB.Sheets(1).Name, _
          Source:=TempWB.Sheets(1).UsedRange.Address, _
          HtmlType:=xlHtmlStatic)
         .Publish (True)
     End With
    
     '从htm文件中读取所有数据并保存到RangetoHTML
     Set fso = CreateObject("Scripting.FileSystemObject")
     Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
     RangetoHTML = ts.ReadAll
     ts.Close
     RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                           "align=left x:publishsource=")
    
     '关闭TempWB
     TempWB.Close SaveChanges:=False
    
     '删除在此函数中使用的htm文件
     Kill TempFile
    
     Set ts = Nothing
     Set fso = Nothing
     Set TempWB = Nothing
     End Function
    

    请注意,在获取HTML标记后,您需要使用HTMLBody属性,而不是纯文本的Body属性。

  2. 您可以使用Range.Copy方法在Excel中复制所需的范围,然后通过使用Word编辑器将其直接粘贴到邮件正文中,查看Inspector类的WordEditor属性。该属性返回显示的邮件的Microsoft Word文档对象模型。有关更多信息,请参阅第17章:处理项目正文

英文:

There are two main ways of setting the message body based on the Excel data:

  1. Build the HTML markup based on the worksheet content. That is basically what Ron described on the Mail Range/Selection in the body of the mail page where you can find the following sample code:

    Function RangetoHTML(rng As Range)
     Dim fso As Object
     Dim ts As Object
     Dim TempFile As String
     Dim TempWB As Workbook
    
     TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    
     'Copy the range and create a new workbook to past the data in
     rng.Copy
     Set TempWB = Workbooks.Add(1)
     With TempWB.Sheets(1)
         .Cells(1).PasteSpecial Paste:=8
         .Cells(1).PasteSpecial xlPasteValues, , False, False
         .Cells(1).PasteSpecial xlPasteFormats, , False, False
         .Cells(1).Select
         Application.CutCopyMode = False
         On Error Resume Next
         .DrawingObjects.Visible = True
         .DrawingObjects.Delete
         On Error GoTo 0
     End With
    
     'Publish the sheet to a htm file
     With TempWB.PublishObjects.Add( _
          SourceType:=xlSourceRange, _
          Filename:=TempFile, _
          Sheet:=TempWB.Sheets(1).Name, _
          Source:=TempWB.Sheets(1).UsedRange.Address, _
          HtmlType:=xlHtmlStatic)
         .Publish (True)
     End With
    
     'Read all data from the htm file into RangetoHTML
     Set fso = CreateObject("Scripting.FileSystemObject")
     Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
     RangetoHTML = ts.readall
     ts.Close
     RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                           "align=left x:publishsource=")
    
     'Close TempWB
     TempWB.Close savechanges:=False
    
     'Delete the htm file we used in this function
     Kill TempFile
    
     Set ts = Nothing
     Set fso = Nothing
     Set TempWB = Nothing
     End Function
    

    Note, after getting the HTML markup you need to use the HTMLBody property, not the plain text Body.

  2. You can copy the required range in Excel by using the Range.Copy method and then paste directly to the message body by using the Word editor, see the WordEditor property of the Inspector class. The property returns the Microsoft Word Document Object Model of the message being displayed. See Chapter 17: Working with Item Bodies for more information.

huangapple
  • 本文由 发表于 2023年6月9日 03:38:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435199.html
匿名

发表评论

匿名网友

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

确定