我的报告格式如下:
r1 Col1 Clmn2 Col3 Col4
r2 itm1 name1 itm1 itm1
r3 itm2 name2 itm2 itm2
r4 itm3 name3 itm3 itm3
r5 itm4 name1 itm4 itm4
我想用它做两件事。
报告被发送给多个人(他们将在Outlook中作为事件处理程序运行脚本)。该脚本将执行以下操作:
以下是一个情境示例:
我(name1)收到附有报告的电子邮件。我的脚本会自动处理电子邮件,这样当我打开它时,我会看到一封电子邮件,其附件只有第1,2和5行(格式正确)。该电子邮件还将在电子邮件的实际正文中包含该表(仅包含行1,2和5)。
到目前为止,这是我的脚本:
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim objNS As Outlook.NameSpace
Dim X As Integer
Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
end Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
If TypeOf Item Is Outlook.MailItem Then
Dim Msg As Outlook.MailItem
Set Msg = Item
If (Msg.Subject = "Report") And _
(Msg.Attachments.Count = 1) Then
' open wkbk and run import macro
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim XLApp As Object ' Excel.Application
Dim XlWK As Object ' Excel.Workbook
Dim Att As String
Const attPath As String = "C:\Users\username\Documents\Reports"
' New Excel.Application
Set XLApp = CreateObject("Excel.Application")
' save attachment
Set myAttachments = Item.Attachments
Att = myAttachments.Item(1).DisplayName
myAttachments.Item(1).SaveAsFile attPath & Att
' open personal.xls where macro is stored,
' just in case it doesn't open on its own
On Error Resume Next
XLApp.Workbooks.Open _
("C:\Documents and Settings\username\Application Data\Microsoft\Excel\XLSTART\PERSONAL.XLS")
On Error GoTo 0
' open workbook and run macro
XLApp.Workbooks.Open (attPath & Att)
XLApp.Run ("PERSONAL.XLS!MacroName")
XLApp.Workbooks.Close
Kill attPath & Att
XLApp.Quit
End If
End If
End Sub
截至目前,它并未将表格复制到电子邮件中(idk如何做到这一点)。它似乎也没有运行宏。每当我打开附件时,它会打开PERSONAL.XLS和附件(未格式化)。
我是否可以直接从outlook脚本编辑附件?比如将excel宏放入excel脚本而不是尝试在excel应用程序中运行它?
报告发送给一个人:
**我已编写宏来编辑/格式化Excel工作表,但我不知道如何将其发送出去。报告中将有一个全名列。我想以某种方式将修改过的个人报告发送给那些人。这有点像将他们的名字从单元格粘贴到" To:"在outlook中的字段然后按ctrl + k。
如果我没有解释清楚,这是一个例子:
r1 Col1 Clmn2 Col3 Col4
r2 itm1 name1 itm1 itm1
r3 itm2 name2 itm2 itm2
r4 itm3 name3 itm3 itm3
r5 itm4 name1 itm4 itm4
我想将第1行,第2行和第5行发送到名称1;第1行和第3行到名称2;第1行和第4行到名称3。
**表格可以作为附件发送,但我也想将表格粘贴到发送的电子邮件中。
提前谢谢!!
答案 0 :(得分:1)
我是否可以直接从outlook脚本编辑附件?
不,不是。 Outlook对象模型不提供任何属性或方法。扩展MAPI(Outlook和其他第三方组件所基于的低级API)允许获取表示附件的字节数组。因此,正确的方法是保存附件,然后重新打开它以进行编辑或阅读内容。
Outlook对象模型提供了三种修改正文的方法:
最后,您可能会发现以下文章有用: