[英]Manipulate Excel via Outlook VBA
我幾乎可以完成這項工作,但我只能堅持其中的一部分。 這是我想做的事情:
我可以保存文件並在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
這條線
Set oWB = oXL.Workbooks.Save(saveFolder & "\" & objAtt.DisplayName)
需要引用您之前存儲的attachName
字符串
Set oWB = oXL.Workbooks.Save(saveFolder & "\" & attachName)
因為objAtt在那時Nothing
都Nothing
。
采用
MsgBox objAtt.DisplayName
在保存之前,以便您可以檢查它是否合適。
順便說一句,注釋掉隱藏Excel(Visible = True)的行,並按F8鍵逐步執行代碼,以便您了解發生了什么。
多謝你們。 我工作了。 這是最終代碼:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.