關于這個問題自動打開附件的副本,以及@FaneDuru 先生接受的答案。
我需要在編輯打開的作業簿后,
然后洗掉附件并將編輯的作業簿保存(添加)到電子郵件本身中。
我希望通過向事件excel workbook itself添加代碼來完成該任務。
我設法洗掉了附件。
我知道在電子郵件中添加附件的代碼,但我不知道如何從 Excel 作業簿本身使用它。
提前感謝所有有用的評論和答案。 BeforeClose
Option Explicit
Option Compare Text
Public WithEvents myItem As Outlook.MailItem
Public EventsDisable As Boolean
Private Sub Application_ItemLoad(ByVal Item As Object)
If EventsDisable = True Then Exit Sub
If Item.Class = olMail Then
Set myItem = Item
End If
End Sub
Private Sub myItem_Open(Cancel As Boolean)
EventsDisable = True
If myItem.Subject = "Auto Plan" And Application.ActiveExplorer.CurrentFolder.Name = "MyTemplate" Then
If myItem.Attachments.Count > 0 Then
Dim obAttach As Attachment, strSaveMail As String, objExcel As Object
Set obAttach = myItem.Attachments(1)
strSaveMail = "C:\Users\Waleed\Desktop\outlook-attachments\"
obAttach.SaveAsFile strSaveMail & obAttach.DisplayName
Dim obAttachName As String
obAttachName = obAttach.FileName
obAttach.Delete 'Remove attached file
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open strSaveMail & obAttach.DisplayName
'Add the below line to workbook itself on event (BeforeClose)
myItem.Attachments.Add strSaveMail & obAttachName
objExcel.Visible = True
'AppActivate objExcel.ActiveWindow.Caption 'using AppActivate causes error
Set objExcel = Nothing
End If
End If
EventsDisable = False
End Sub
uj5u.com熱心網友回復:
為了完成整個程序,您應該繼續,我將嘗試解釋。
首先,我想簡要介紹一下調??用的程序: 1. 從模板所在的位置打開模板。2.使用觸發的OutlookApplication_ItemLoad事件myItem_Open,保存附件,洗掉它(從郵件中)并在Microsoft Excel中打開它。3. 您修改保存的附件,保存它,回傳郵件視窗并按Send。4. OutlookItemSend事件將重新附加以前保存的作業簿(現在已修改),并發送包含保存的作業簿的郵件。
- 請復制
ThisOutlookSession代碼模塊頂部的下兩個變數宣告(在宣告區域中):
Private Const strSaveMail As String = "C:\Users\Waleed\Desktop\outlook-attachments\"
Private wbName As String 'to keep the attachment name
洗掉strSaveMail As String宣告,strSaveMail = "C:\Users\Waleed\Desktop\outlook-attachments\"并將附件作業簿名稱賦予代碼wbName。myItem_Open
- 修改后的代碼事件應如下所示:
Private Sub myItem_Open(Cancel As Boolean)
EventsDisable = True
If myItem.Subject = "Auto Plan" And Application.ActiveExplorer.CurrentFolder.Name = "MyTemplate" Then
If myItem.Attachments.Count > 0 Then
Dim obAttach As Attachment, objExcel As Object
Set obAttach = myItem.Attachments(1)
obAttach.SaveAsFile strSaveMail & obAttach.DisplayName
wbName = obAttach.DisplayName 'to be used later, when the workbook will be reattached
obAttach.Delete 'Remove attached file
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open strSaveMail & wbName
objExcel.Visible = True
Set objExcel = Nothing
End If
End If
EventsDisable = False
End Sub
- 修改之前保存和打開的作業簿,保存并回傳郵件視窗并按
Send。OutlookItemSend事件將被觸發并重新附加保存的作業簿:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeName(Item) = "MailItem" Then
Set MyItem = Item
On Error GoTo Err_Handler
If MyItem.Subject = "Auto Plan" And MyItem.Attachments.Count = 0 Then
MyItem.Attachments.Add strSaveMail & wbName, 1
End If
End If
Exit Sub
Err_Handler:
MsgBox Err.Number & vbCrLf & Err.Description
Cancel = True 'if an error will be raised, the mail sending is cancelled, to see what problem does appear...
End Sub
郵件將與修改后的作業簿作為附件一起發送。
如果沒有任何錯誤(在測驗期間)出現,使用全域變數會很好。因此,即使出現錯誤,它們都可以保存在注冊表中并毫無問題地使用。
Open可以修改該事件以使 Excel 在郵件視窗后面的最大化視窗中打開(附加)作業簿。
回傳郵件視窗也可以自動進行,正如您在我上一個問題的回答中看到的那樣。
如果有任何不清楚的地方,請不要猶豫,要求澄清。
但是請按原樣嘗試代碼/解決方案,并且只有在看到它作業后才嘗試修改它,如有必要......
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464235.html
