Outlook VBA保存邮件附件,然后将附件数据复制到另一个excel中,并通过邮件发送excel
我正在尝试创建Outlook VBA代码,将特定邮件中的附件保存到文件夹中,然后将附件中的数据复制粘贴到另一个excel中。然后将第二个excel发送到某些ID 我创建了一个规则,第一个规则是将收到的自动邮件移动到特定的邮件文件夹,然后将其附件保存到桌面文件夹。保存附件后,数据将复制到第二个excel。代码是这样的Outlook VBA保存邮件附件,然后将附件数据复制到另一个excel中,并通过邮件发送excel,excel,vba,email,outlook,Excel,Vba,Email,Outlook,我正在尝试创建Outlook VBA代码,将特定邮件中的附件保存到文件夹中,然后将附件中的数据复制粘贴到另一个excel中。然后将第二个excel发送到某些ID 我创建了一个规则,第一个规则是将收到的自动邮件移动到特定的邮件文件夹,然后将其附件保存到桌面文件夹。保存附件后,数据将复制到第二个excel。代码是这样的 Public Sub ExportFile(MyMail As MailItem) Dim outNS As Outlook.NameSpace Dim outFolder As
Public Sub ExportFile(MyMail As MailItem)
Dim outNS As Outlook.NameSpace
Dim outFolder As Outlook.MAPIFolder
Dim outNewMail As Outlook.MailItem
Dim strDir As String
Set outNS = GetNamespace("MAPI")
Set outFolder = outNS.GetDefaultFolder(olFolderInbox).Folders("Network Critical Report")
Set outNewMail = outFolder.Items.GetLast
strDir = "C:\Users\soumyajitd\Desktop\December\Network Critical Report\"
If outNewMail.Attachments.count = 0 Then GoTo Err
outNewMail.Attachments(1).SaveAsFile strDir & "Network_Critical_Report.csv"
Dim xlApp As Excel.Application
Dim wbTarget As Excel.Workbook 'workbook where the data is to be pasted
Dim wsTarget As Excel.Worksheet
Dim wbThis As Excel.Workbook 'workbook from where the data is to copied
Dim wsThis As Excel.Worksheet
Dim strName As String 'name of the source sheet/ target workbook
Set xlApp = New Excel.Application
xlApp.DisplayAlerts = False
'xlApp.Workbooks.Open strDir & "Network_Critical_Report.csv"
'xlApp.Workbooks.Open strDir & "Test.xlsx"
Set wbThis = xlApp.Workbooks.Open("C:\Users\soumyajitd\Desktop\December\Network Critical Report\Network_Critical_Report.csv")
Set wsThis = wbThis.Worksheets("Network_Critical_Report")
Set wbTarget = xlApp.Workbooks.Open("C:\Users\soumyajitd\Desktop\December\Network Critical Report\Test.xlsx")
Set wsTarget = wbTarget.Worksheets("Raw_Data")
'select cell A1 on the target book
'clear existing values form target book
wsTarget.UsedRange.ClearContents
'activate the source book
wbThis.Activate
xlApp.CutCopyMode = False
'copy the range from source book
wsThis.UsedRange.Copy
'paste the data on the target book
wsTarget.Range("A1").PasteSpecial Paste:=xlPasteValues
'save the target book
wbTarget.Save
'close the workbook
wbTarget.Close
wbThis.Close
xlApp.CutCopyMode = False
Kill ("C:\Users\soumyajitd\Desktop\December\Network Critical Report\Network_Critical_Report.csv")
'clear memory
Set wbTarget = Nothing
Set wbThis = Nothing
Set xlApp = Nothing
Set outNewMail = Nothing
Set outFolder = Nothing
Set outNS = Nothing
Err:
Set outFolder = Nothing
Set OuNewMail = Nothing
Set outNS = Nothing
End Sub
第二个代码是发送一封附件为“Test.xlsx”的新电子邮件,如下所示:
Sub SendNew(Item As Outlook.MailItem)
Dim objMsg As MailItem
Dim ToRecipient As Variant
Dim ccRecipient As Variant
Dim Subject As String
Dim Body As String
Dim FilePathtoAdd As String
Set objMsg = Application.CreateItem(olMailItem)
objMsg.ToRecipients.Add "alias@mail.com"
objMsg.CCRecipients.Add "xx@yy.com"
objMsg.Subject = "Subject"
objMsg.Body = "Body"
If FilePathtoAdd <> "" Then
objMsg.Attachments.Add "C:\Users\soumyajitd\Desktop\December\Network Critical Report\Test.xlsx"
End If
objMsg.Send
Sub SendNew(项目名为Outlook.MailItem)
Dim objMsg作为邮件项
Dim被视为变体
Dim作为变体
模糊主题为字符串
暗体为弦
Dim FILEPATH添加为字符串
设置objMsg=Application.CreateItem(olMailItem)
objMsg.ToRecipients.Add“alias@mail.com"
objMsg.CCRecipients.Add“xx@yy.com"
objMsg.Subject=“Subject”
objMsg.Body=“Body”
如果文件路径添加“”,则
objMsg.Attachments.Add“C:\Users\soumyajitd\Desktop\defect\Network Critical Report\Test.xlsx”
如果结束
发送
我几乎没有VBA编码的经验。我从不同的论坛上获取了所有这些代码,并对它们进行了修改以满足我的需要
现在有三个问题
有关您的第一个问题,请参阅 针对您的第二个问题 要组合,请将两个脚本合并到一个子脚本中,或者从第一个子脚本调用另一个脚本 针对您的第三个问题
没有名为
.ToRecipients
和.CCRecipients
的属性。请将其更改为objMsg.to=”alias@mail.com“
和objMsg.CC=”xx@yy.com“
分别
另外,您的文件路径toadd=“”
,因此您的if条件不满足。请删除该if条件或将代码更改为该条件
FilePathtoAdd = "C:\Users\soumyajitd\Desktop\December\Network Critical Report\Test.xlsx"
With objMsg
.To = "alias@mail.com"
.CC = "xx@yy.com"
.Subject = "Subject"
.Body = "Body"
.Attachments.Add FilePathtoAdd
End With
你认为这对你有什么帮助吗?它将解决第一个问题,但发送新邮件选项和链接脚本的问题仍然存在。基本上,从我创建一个新的“Sub”开始“创建了一个新脚本。如何将所有这些小代码保存在一个程序中没有名为
ToRecipients
,CCRecipients
的属性。”。将其更改为objMsg.to=”alias@mail.com“
和objMsg.CC=”xx@yy.com“
附件选项不起作用,这是因为filepathtoad=”“
因此不满足If条件。