使用Outlook VBA从Excel文件复制/粘贴。

耶赫特

好的,所以这里有些难题。这是我正在尝试的罗version版本:

  1. 在我已经在Outlook中制作的模板中,将其打开并拖动一些文件-其中一个将是Excel文件。
  2. 打开Excel文件并读取到预定的最后一个单元格
  3. 将单元格从最后一行/列复制到第一个单元格A1
  4. 将先前在步骤3中复制的单元格粘贴到Outlook正文中

当前我的问题所在是4号。附上代码

Const xlUp = -4162
'Needed to use the .End() method
 Sub Sample()
    Dim NewMail As MailItem, oInspector As Inspector
    Set oInspector = Application.ActiveInspector
    Dim eAttachment As Object, xlsAttachment As Object, i As Integer, lRow As Integer, lPriorRow As Integer, lCommentRow As Integer

    '~~> Get the current open item
    Set NewMail = oInspector.CurrentItem
    'Code given to me from a previous question

    Set eAttachment = CreateObject("Excel.Application")

    With NewMail.Attachments
        For i = 1 To .Count

            If InStr(.Item(i).FileName, ".xls") > 0 Then
                'Save the email attachment so we can open it
                sFileName = "C:/temp/" & .Item(i).FileName
                .Item(i).SaveAsFile sFileName

                eAttachment.Workbooks.Open sFileName

                With eAttachment.Workbooks(.Item(i).FileName).Sheets(1)

                    lCommentRow = .Cells.Find("Comments").Row
                    lPriorRow = .Cells.Find("Prior Inspections").Row

                    lRow = eAttachment.Max(lCommentRow, lPriorRow)
                    ' Weirdly enough, Outlook doesn't seem to have a Max function, so I used the Excel one.

                    .Range("A1:N" & lRow).Select
                    .Range("A1:N" & lRow).Copy

                    'Here is where I get lost; nothing I try seems to work

                    NewMail.Display

                End With


                eAttachment.Workbooks(.Item(i).FileName).Close

                Exit For

            End If

        Next
    End With

End Sub

我在另一个问题上看到了一个将Range对象更改为HTML的函数,但由于该宏代码位于Outlook中而不是Excel中,因此在这里不起作用

任何帮助,将不胜感激。

ptpaterson

也许这个网站会为您指明正确的方向。


编辑:

经过一番修补,我得到了这个工作:

Option Explicit

 Sub Sample()
    Dim MyOutlook As Object, MyMessage As Object

    Dim NewMail As MailItem, oInspector As Inspector

    Dim i As Integer

    Dim excelApp As Excel.Application, xlsAttachment As Attachment, wb As workBook, rng As Range

    Dim sFileName As String

    Dim lCommentRow As Long, lPriorRow As Long, lRow As Long

    ' Get the current open mail item
    Set oInspector = Application.ActiveInspector
    Set NewMail = oInspector.CurrentItem

    ' Get instance of Excel.Application
    Set excelApp = New Excel.Application

    ' Find the attachment
    For i = 1 To NewMail.Attachments.Count
        If InStr(NewMail.Attachments.Item(i).FileName, ".xls") > 0 Then
            MsgBox "Located attachment: """ & NewMail.Attachments.Item(i).FileName & """"
            Set xlsAttachment = NewMail.Attachments.Item(i)
            Exit For
        End If
    Next

    ' Continue only if attachment was found
    If Not IsNull(xlsAttachment) Then

        ' Set temp file location and use time stamp to allow multiple times with same file
        sFileName = "C:/temp/" & Int(CDbl(Now()) * 10000) & xlsAttachment.FileName
        xlsAttachment.SaveAsFile (sFileName)

        ' Open file so we can copy info
        Set wb = excelApp.Workbooks.Open(sFileName)

        ' Search worksheet for important info
        With wb.Sheets(1)        
            lCommentRow = .Cells.Find("Comments").Row
            lPriorRow = .Cells.Find("Prior Inspections").Row
            lRow = excelApp.Max(lCommentRow, lPriorRow)
            set rng = .Range("A1:H" & lRow)
        End With

        ' Set up the email message
        With NewMail
            .To = "[email protected]"
            .CC = "[email protected]"
            .Subject = "TEST - PLEASE IGNORE"
            .BodyFormat = olFormatHTML
            .HTMLBody = RangetoHTML(rng)
            .Display
        End With

    End If
    wb.Close

End Sub

Function RangetoHTML(rng As Range)
' By Ron de Bruin.
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As workBook

    Dim excelApp As Excel.Application
    Set excelApp = New Excel.Application

    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        ' Paste over column widths from the file
        .Cells(1).PasteSpecial xlPasteValues
        .Cells(1).PasteSpecial xlPasteFormats
        .Cells(1).Select
        excelApp.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

您必须转到“工具”->“参考”并包括Microsoft Excel对象库。这个问题向我指出。我喜欢避免后期绑定,以便出现vba intellisense,并且我知道这些方法有效。

RangetoHTML来自Ron Debruin(我必须编辑PasteSpecial方法才能使它们工作)

我还从该论坛获得了一些有关如何将文本插入电子邮件正文的帮助。

我将日期添加到临时文件的名称,因为我试图多次保存它。

我希望这有帮助。我肯定学到了很多东西!

更多说明:

在我看来,这些细胞正在被截断。正如mvsub1在此说明的,使用RangeToHTML函数的问题在于它将超出列宽的文本视为隐藏文本,并将其粘贴到电子邮件中:

[td class=xl1522522 width=64 style="width:48pt"]This cell i[span style="display:none">s too long.[/span][/td]

如果您遇到类似的问题,页面上还会讨论一些解决方案。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Excel中使用VBA复制粘贴

来自分类Dev

仅在同一列上使用Excel VBA复制粘贴值(xlPasteValues)

来自分类Dev

Excel:如何使用复制粘贴命令到选定的工作表区域 (VBA)

来自分类Dev

比较两列并使用vba复制粘贴

来自分类Dev

使用对象在Excel上复制粘贴时出错

来自分类Dev

Excel VBA复制粘贴

来自分类Dev

使用R将文件复制粘贴到名称匹配的文件夹中

来自分类Dev

使用水豚复制粘贴吗?

来自分类Dev

使用变量复制粘贴表

来自分类Dev

复制粘贴列使用范围

来自分类Dev

使用变量复制粘贴表

来自分类Dev

VBA代码将Excel范围复制并粘贴到Outlook中

来自分类Dev

在VBA Excel中复制粘贴范围

来自分类Dev

复制粘贴为值 Excel VBA

来自分类Dev

Excel VBA 行复制粘贴错误

来自分类Dev

excel VBA 上的复制粘贴列

来自分类Dev

Excel VBA 仅复制粘贴值

来自分类Dev

如何复制粘贴所有格式和值而不在VBA中使用选择

来自分类Dev

使用EXCEL VBA时如何将数据复制并粘贴到所选文件中

来自分类Dev

For循环复制粘贴文件夹并使用增量对其重命名

来自分类Dev

从Excel复制粘贴

来自分类Dev

Excel VBA复制和粘贴(不使用复制+粘贴功能)在空行上

来自分类Dev

Excel VBA复制和粘贴(不使用复制+粘贴功能)在空行上

来自分类Dev

使用VBA在Excel 2016中复制和粘贴值

来自分类Dev

如何使用R将一个本地png文件复制粘贴到Word文档中?

来自分类Dev

在从文件复制粘贴和执行命令之前,在命令行中一一创建要使用的路径

来自分类Dev

VBA 复制粘贴数据

来自分类Dev

VBA 复制粘贴循环

来自分类Dev

使用“偏移”时如何加快“复制粘贴”值

Related 相关文章

热门标签

归档