简体   繁体   English

VBA发送邮件Outlook时将Excel文件转换为文本

[英]VBA convert Excel file to text when sending mail outlook

how can i convert the activeworkbook that i am working on , while sending a mail in outlook ? 在Outlook中发送邮件时,如何转换正在处理的activeworkbook? This code has like attachments .xlsx file , but i want it in text , how can i change it ? 这段代码有类似.xlsx附件的文件,但是我想要用文本形式显示,如何更改它? Thanks 谢谢

 'send mail code
                Set ol = New Outlook.Application
                Set olmail = ol.CreateItem(olMailItem)
                With olmail
                    .To = "test@outlook.com"
                    .Subject = objetText
                    .Body = "testing"
                    .Send

                .attachments.Add activeworkbook.fullname
                End With
            End sub

With MS Office you can copy an Excel cell range in the clipboard and paste this into an Outlook mail body if that mail body is in rich text format and you are using Word editor. 使用MS Office,您可以在剪贴板中复制Excel单元格区域,并将其粘贴到Outlook邮件正文中(如果该邮件正文为富文本格式并且您正在使用Word编辑器)。 The cell range will be converted into a rich text table while this process. 在此过程中,单元格区域将转换为RTF表。

This can also be accomplished by code: 这也可以通过代码来完成:

Sub emailer()

 Set oOlApp = CreateObject("Outlook.Application")

 olMailItem = 0
 Set oOlMItem = oOlApp.CreateItem(olMailItem)

 'get Excel cell range which shall be in the mail
 Set oWB = ActiveWorkbook
 Set oWS = ActiveWorkbook.Worksheets(1)
 Set oRange = oWS.Range("A1:C10")

 oRange.Copy ' Range is now in Clipboard

 With oOlMItem

  .Display

  .To = "email@email.com"
  .Subject = "Subject"

  Set oOlInsp = .GetInspector
  Set oWdDoc = oOlInsp.WordEditor ' get Word Document from the MailBody

  olFormatRichText = 3
  .BodyFormat = olFormatRichText ' change to RichTextFormat

  Set oWdRng = oWdDoc.Paragraphs(oWdDoc.Paragraphs.Count).Range
  oWdRng.InsertBefore "This is before the Excel table."
  oWdRng.InsertParagraphAfter
  oWdRng.InsertParagraphAfter

  Set oWdRng = oWdDoc.Paragraphs(oWdDoc.Paragraphs.Count).Range
  oWdRng.Paste ' paste Excel range from Clipboard

  oWdRng.InsertParagraphAfter

  Set oWdRng = oWdDoc.Paragraphs(oWdDoc.Paragraphs.Count).Range
  oWdRng.InsertBefore "This is after the Excel table."


 End With

 Application.CutCopyMode = False

End Sub

You can only convert 1 worksheet to text, so I'm assuming this is what you are trying to accomplish. 您只能将1个工作表转换为文本,所以我假设这就是您要完成的工作。 Copy the sheet to a new workbook (so you can continue working with the current workbook as xlsm), save that as text then use the save location for the string you're using for attachments.add 将工作表复制到新工作簿(以便您可以以xlsm的形式继续使用当前工作簿),将其另存为文本,然后将保存位置保存为用于附件的字符串。

Dim wbkthis As Workbook
Dim wbkNew As Workbook

Set wbkthis = ActiveWorkbook 
'So we can come back here
Set wbkNew = Workbooks.Add 
'New book to copy sheet to

wbkthis.Sheets("relevantsheetname").Copy Before:=wbkNew.Sheets(1)

Application.DisplayAlerts = False
'Supress overwrite and close alerts, you may not want this

wbkNew.SaveAs "savelocation\Mybookname.txt", xlText 
'use this same string with your attachments.add code
wbkNew.Close

Application.DisplayAlerts = True
wbkthis.Activate ' Go back to original file

I am certain that Axel's answer is probably what you need but since you asked about converting to it to text and then putting it in, this will do that. 我确信Axel的答案可能就是您所需要的,但是由于您询问过将其转换为文本然后放入文本的功能,因此可以做到这一点。 The formatting will look terrible of course. 格式化当然看起来很糟糕。 Make sure that (in the VBE) you go into tools/references and place a check next to Microsoft Forms 2.0 Object Library. 确保(在VBE中)进入工具/引用,然后在Microsoft Forms 2.0对象库旁边进行检查。

Dim ws As Worksheet
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
Set ws = ActiveWorkbook.ActiveSheet
    ws.UsedRange.Copy
DataObj.GetFromClipboard
strbody = DataObj.GetText(1)

With NewMail
  .SUBJECT = "Test Mail"
  .From = """Sender"" <sender@email.com>"
  .To = "receiver@email.com"
  .CC = ""
  .BCC = ""
  .TextBody = strbody
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM