0

我几乎已经完成了这项工作,但我被困在了一个部分。这是我正在尝试做的事情:

  1. 保存 Outlook 电子邮件附件(.csv 文件)
  2. 在 Excel 中打开附件
  3. 删除文件的最后 6 行
  4. 重新保存文件

我能够保存文件并让它在 Excel 中打开,但没有其他任何事情发生。无论我尝试什么,我都无法在 Excel 中执行任何操作;我无法删除最后 6 行(解析页脚)。任何帮助将不胜感激!

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim attachName As String

Dim oXL As Object, oWB As Object, oSheet As Object

saveFolder = "C:\Temp\"

    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
        attachName = objAtt.DisplayName
        Set objAtt = Nothing
    Next


' Start Excel and get Application object
Set oXL = CreateObject("Excel.Application")

' Hide Excel
oXL.Visible = False

' Open the File
Set oWB = oXL.Workbooks.Open(saveFolder & attachName)

'Set the Worksheet
Set oSheet = oWB.Sheets("Sheet1")

'Parse the Footer
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(-5, 0).Range("A1:A6").Select
ActiveCell.Activate
Selection.ClearContents

'Save the File
Set oWB = oXL.Workbooks.Save(saveFolder & "\" & objAtt.DisplayName)

'Clean Up
oWB.Close (True)
oXL.Quit
Set oWB = Nothing
Set oXL = Nothing

End Sub
4

2 回答 2

0

多谢你们。我得到了这个工作。这是最终的代码:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim attachName As String

Dim oXL As Excel.Application
Dim oWB As Excel.workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range

saveFolder = "C:\Temp\"
   'Grab attachment
      For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          attachName = objAtt.DisplayName
          Set objAtt = Nothing
      Next

   ' Start Excel and get Application object.
      Set oXL = CreateObject("Excel.Application")
      oXL.Visible = True

   ' Get a new workbook.
      Set oWB = oXL.Workbooks.Open(saveFolder & attachName)
      Set oSheet = oWB.ActiveSheet

    ' Find Last Row and Clear Contents; Do this 5 Times
      Set oRng = oSheet.Columns("A:A").Find("*", oSheet.[a1], xlValues, , xlByRows, xlPrevious)
      oRng.Cells.ClearContents

      Set oRng = oSheet.Columns("A:A").Find("*", oSheet.[a1], xlValues, , xlByRows, xlPrevious)
      oRng.Cells.ClearContents

      Set oRng = oSheet.Columns("A:A").Find("*", oSheet.[a1], xlValues, , xlByRows, xlPrevious)
      oRng.Cells.ClearContents

      Set oRng = oSheet.Columns("A:A").Find("*", oSheet.[a1], xlValues, , xlByRows, xlPrevious)
      oRng.Cells.ClearContents

      Set oRng = oSheet.Columns("A:A").Find("*", oSheet.[a1], xlValues, , xlByRows, xlPrevious)
      oRng.Cells.ClearContents

    ' Make sure Excel is visible and give the user control
    ' of Microsoft Excel's lifetime.
      oXL.Visible = True
      oXL.UserControl = True

    'Save the File
      oWB.Save
      oWB.Saved = True

    ' Quite, Close and Make sure you release object references.
      oWB.Close
      oXL.Quit
      Set oRng = Nothing
      Set oSheet = Nothing
      Set oWB = Nothing
      Set oXL = Nothing

End Sub
于 2013-07-12T13:40:41.723 回答
0

这条线

Set oWB = oXL.Workbooks.Save(saveFolder & "\" & objAtt.DisplayName)

需要引用attachName你之前存储的字符串

Set oWB = oXL.Workbooks.Save(saveFolder & "\" & attachName)

因为 objAttNothing在那个时候。

利用

MsgBox objAtt.DisplayName

就在保存之前,以便您可以检查它是否合适。

顺便说一句,注释掉隐藏 Excel 的行(Visible = True)并按 F8 单步执行代码,这样您就可以看到发生了什么。

于 2013-07-11T19:29:42.427 回答