我假設你是自動化Outlook 從 Excel。此解決方案可能適用於您,但正如您注意到的,它依賴於保存附件並重新附加操作版本的文件。假設您可以編寫將「編輯」工作簿附件的代碼,這應該適合您。
Sub TestOutlookTemplate()
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim att As Outlook.Attachment
Dim templatePath As String
Dim tempFileName As String
Dim attWorkbook As Workbook
templatePath = "C:\users\david_zemens\desktop\Untitled.oft"
tempFileName = "C:\users\david_zemens\desktop\tempexcelfile.xlsx"
Set MyOutlook = CreateObject("Outlook.Application")
Set MyMail = MyOutlook.CreateItemFromTemplate(templatePath)
MyMail.Display
For Each att In MyMail.Attachments
If att.DisplayName Like "*.xls*" Then
att.SaveAsFile tempFileName
'Now that you have saved the file, delete the attachment
att.Delete
'Open the file
Set attWorkbook = Workbooks.Open(tempFileName)
'Perform manipulation on the file
attWorkbook.Sheets(1).Name = "Sheet ONE"
'Save fhe file
attWorkbook.Save
'Close the file
attWorkbook.Close
MyMail.Attachments.Add tempFileName
End If
Next
'Send your mail (make sure you have added a recipient
MyMail.Send
Set attWorkbook = Nothing
Set MyMail = Nothing
Set MyOutlook = Nothing
End Sub
您有沒有機會嘗試我的建議? –
是的,我做了,我選擇它作爲正確的答案。從技術上講,雖然我只想抓住attatchment對象,並在當前的excel實例中打開/編輯它,而不必將其保存到臨時文件夾等等。它看起來並不像第一個可能,所以我最終使用這種方法的一個變種。 – MattB
這個答案應該是打開當前Excel實例中的文件。不是嗎?至於臨時文件夾...下面的答案不使用臨時文件夾,但它確實需要保存並從已知位置打開該文件。如果您可以複製雙擊操作(這將從臨時文件夾中打開該文件),但它可能*可以使用真正的臨時文件夾,但需要以其他方式完成。 Attachment對象只支持兩種方法('.Delete'和'.SaveAsFile'),我正在尋找類似'OLEFormat.DoVerb'的東西,但這似乎不是一個選項。 –