使用excel vba-子目录从保存在文件夹中的.msg文件中提取附件
能够把一些东西放在一起,通过主文件夹,提取msg文件和附件 我希望代码也能够在子文件夹中循环 我该怎么做呢?我看过一些关于这个主题的文章,但是我很难把它翻译成我所拥有的 '''使用excel vba-子目录从保存在文件夹中的.msg文件中提取附件,excel,vba,subdirectory,msg,Excel,Vba,Subdirectory,Msg,能够把一些东西放在一起,通过主文件夹,提取msg文件和附件 我希望代码也能够在子文件夹中循环 我该怎么做呢?我看过一些关于这个主题的文章,但是我很难把它翻译成我所拥有的 ''' Dim outApp作为对象 将邮件设置为对象 作为对象的附件 将msgfiles设置为字符串,将sourceFolder设置为字符串,将saveInFolder设置为字符串 将文件名设置为字符串 msgfiles=“C:\test\*.msg”'更改-文件夹位置和.msg文件的filespec saveInFolder
Dim outApp作为对象
将邮件设置为对象
作为对象的附件
将msgfiles设置为字符串,将sourceFolder设置为字符串,将saveInFolder设置为字符串
将文件名设置为字符串
msgfiles=“C:\test\*.msg”'更改-文件夹位置和.msg文件的filespec
saveInFolder=“C:\test 2”更改-保存提取的附件的文件夹
如果正确(saveInFolder,1)“\”则saveInFolder=saveInFolder&“\”
sourceFolder=Left(msgfiles,InStrRev(msgfiles,“\”))
出错时继续下一步
Set-outApp=GetObject(,“Outlook.Application”)
如果outApp什么都不是,那么
MsgBox“Outlook未打开”
出口接头
如果结束
错误转到0
fileName=dir(msgfiles)
而文件名为vbNullString
'在Outlook 2003中打开.msg文件
'设置outEmail=outApp.CreateItemFromTemplate(sourceFolder&fileName)
'在Outlook 2007中打开.msg文件+
设置outEmail=outApp.Session.OpenSharedItem(sourceFolder&fileName)
对于outEmail.Attachments中的每个outAttachment
outAttachment.SaveAsFile saveInFolder&outAttachment.fileName
下一个
fileName=dir
温德
'''
编辑
在第一次建议后包括以下代码
Sub LoopThrough(parentFolder As String)
Dim fso As Object
' Create a File System object to loop through folders
If fso Is Nothing Then
Set fso = CreateObject("Scripting.FileSystemObject")
End If
' Get the specified folder
Dim folder As Object
Set folder = fso.GetFolder(parentFolder)
' Call my code on it
MyCode msgfiles.Path
' Get all sub folders
Dim subFolder As Object
On Error Resume Next ' We might have permission issues so lets carry on if we get a folder we cannot access
For Each subFolder In folder.subfolders
On Error GoTo 0 ' If we cant access the folder, reset error
If Not subFolder Is Nothing Then ' Folder will be null/nothing if we had an error so ignore it if it is
LoopThrough subFolder.Path
End If
On Error Resume Next ' When going back over the loop we can still get an error
Next
On Error GoTo 0
End Sub
Sub MyCode(folder As String)
Debug.Print folder
msgfiles = folder & "\*.msg"
' Your code Here
Dim outApp As Object
Dim outEmail As Object
Dim outAttachment As Object
Dim msgfiles As String, sourceFolder As String, saveInFolder As String
Dim fileName As String
msgfiles = "C:\test\*.msg" 'CHANGE - folder location and filespec of .msg files
saveInFolder = "C:\test 2" 'CHANGE - folder where extracted attachments are saved
If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
sourceFolder = Left(msgfiles, InStrRev(msgfiles, "\"))
On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
If outApp Is Nothing Then
MsgBox "Outlook is not open"
Exit Sub
End If
On Error GoTo 0
fileName = Dir(msgfiles)
While fileName <> vbNullString
'Open .msg file in Outlook 2003
'Set outEmail = outApp.CreateItemFromTemplate(sourceFolder & fileName)
'Open .msg file in Outlook 2007+
Set outEmail = outApp.Session.OpenSharedItem(sourceFolder & fileName)
For Each outAttachment In outEmail.Attachments
outAttachment.SaveAsFile saveInFolder & outAttachment.fileName
Next
fileName = Dir
Wend
End Sub
子循环(parentFolder作为字符串)
作为对象的Dim fso
'创建文件系统对象以循环浏览文件夹
如果fso什么都不是,那么
设置fso=CreateObject(“Scripting.FileSystemObject”)
如果结束
'获取指定的文件夹
将文件夹变暗为对象
Set folder=fso.GetFolder(parentFolder)
'在上面调用我的代码
mycodemsgfiles.Path
'获取所有子文件夹
将子文件夹变暗为对象
在错误“继续下一步”时,“我们可能有权限问题,因此,如果我们得到一个无法访问的文件夹,请继续
对于folder.subfolders中的每个子文件夹
错误转到“0”时如果无法访问该文件夹,请重置错误
如果不是,则“子文件夹”为空,如果出现错误,则“文件夹”将为空/为空,因此如果是,则忽略它
循环遍历子文件夹.Path
如果结束
“在错误恢复下一步”当返回循环时,我们仍然可以得到一个错误
下一个
错误转到0
端接头
子代码(文件夹作为字符串)
调试.打印文件夹
msgfiles=文件夹&“\*.msg”
'您的代码在这里
Dim outApp作为对象
将邮件设置为对象
作为对象的附件
将msgfiles设置为字符串,将sourceFolder设置为字符串,将saveInFolder设置为字符串
将文件名设置为字符串
msgfiles=“C:\test\*.msg”'更改-文件夹位置和.msg文件的filespec
saveInFolder=“C:\test 2”更改-保存提取的附件的文件夹
如果正确(saveInFolder,1)“\”则saveInFolder=saveInFolder&“\”
sourceFolder=Left(msgfiles,InStrRev(msgfiles,“\”))
出错时继续下一步
Set-outApp=GetObject(,“Outlook.Application”)
如果outApp什么都不是,那么
MsgBox“Outlook未打开”
出口接头
如果结束
错误转到0
fileName=Dir(msgfiles)
而文件名为vbNullString
'在Outlook 2003中打开.msg文件
'设置outEmail=outApp.CreateItemFromTemplate(sourceFolder&fileName)
'在Outlook 2007中打开.msg文件+
设置outEmail=outApp.Session.OpenSharedItem(sourceFolder&fileName)
对于outEmail.Attachments中的每个outAttachment
outAttachment.SaveAsFile saveInFolder&outAttachment.fileName
下一个
fileName=Dir
温德
端接头
这个循环遍历一个目录及其子目录。它将调用MyCode
,并传入文件夹的完整路径。您需要修改MyCode来执行您想要的操作
Sub LoopThrough(parentFolder As String)
Dim fso As Object
' Create a File System object to loop through folders
If fso Is Nothing Then
Set fso = CreateObject("Scripting.FileSystemObject")
End If
' Get the specified folder
Dim folder As Object
Set folder = fso.GetFolder(parentFolder)
' Call my code on it
MyCode msgfiles.Path
' Get all sub folders
Dim subFolder As Object
On Error Resume Next ' We might have permission issues so lets carry on if we get a folder we cannot access
For Each subFolder In folder.subfolders
On Error GoTo 0 ' If we cant access the folder, reset error
If Not subFolder Is Nothing Then ' Folder will be null/nothing if we had an error so ignore it if it is
LoopThrough subFolder.Path
End If
On Error Resume Next ' When going back over the loop we can still get an error
Next
On Error GoTo 0
End Sub
Sub MyCode(folder As String)
Dim outApp As Object
Dim outEmail As Object
Dim outAttachment As Object
Dim msgfiles As String, sourceFolder As String, saveInFolder As String
Dim fileName As String
Debug.Print folder
msgfiles = folder & "\*.msg"
' Your code Here
''msgfiles = "C:\test\*.msg" 'CHANGE - folder location and filespec of .msg files
saveInFolder = "C:\test 2" 'CHANGE - folder where extracted attachments are saved
If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
sourceFolder = Left(msgfiles, InStrRev(msgfiles, "\"))
On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
If outApp Is Nothing Then
MsgBox "Outlook is not open"
Exit Sub
End If
On Error GoTo 0
fileName = Dir(msgfiles)
While fileName <> vbNullString
'Open .msg file in Outlook 2003
'Set outEmail = outApp.CreateItemFromTemplate(sourceFolder & fileName)
'Open .msg file in Outlook 2007+
Set outEmail = outApp.Session.OpenSharedItem(sourceFolder & fileName)
For Each outAttachment In outEmail.Attachments
outAttachment.SaveAsFile saveInFolder & outAttachment.fileName
Next
fileName = Dir
Wend
End Sub
子循环(parentFolder作为字符串)
作为对象的Dim fso
'创建文件系统对象以循环浏览文件夹
如果fso什么都不是,那么
设置fso=CreateObject(“Scripting.FileSystemObject”)
如果结束
'获取指定的文件夹
将文件夹变暗为对象
Set folder=fso.GetFolder(parentFolder)
'在上面调用我的代码
mycodemsgfiles.Path
'获取所有子文件夹
将子文件夹变暗为对象
在错误“继续下一步”时,“我们可能有权限问题,因此,如果我们得到一个无法访问的文件夹,请继续
对于folder.subfolders中的每个子文件夹
错误转到“0”时如果无法访问该文件夹,请重置错误
如果不是,则“子文件夹”为空,如果出现错误,则“文件夹”将为空/为空,因此如果是,则忽略它
循环遍历子文件夹.Path
如果结束
“在错误恢复下一步”当返回循环时,我们仍然可以得到一个错误
下一个
错误转到0
端接头
子代码(文件夹作为字符串)
Dim outApp作为对象
将邮件设置为对象
作为对象的附件
将msgfiles设置为字符串,将sourceFolder设置为字符串,将saveInFolder设置为字符串
将文件名设置为字符串
调试.打印文件夹
msgfiles=文件夹&“\*.msg”
'您的代码在这里
''msgfiles=“C:\test\*.msg”'更改-文件夹位置和.msg文件的filespec
saveInFolder=“C:\test 2”更改-保存提取的附件的文件夹
如果正确(saveInFolder,1)“\”则saveInFolder=saveInFolder&“\”
sourceFolder=Left(msgfiles,InStrRev(msgfiles,“\”))
出错时继续下一步
Set-outApp=GetObject(,“Outlook.Application”)
如果outApp什么都不是,那么
MsgBox“Outlook未打开”
出口接头
如果结束
错误转到0
fileName=Dir(msgfiles)
而文件名为vbNullString
'在Outlook 2003中打开.msg文件
'设置outEmail=outApp.CreateItemFromTemplate(sourceFolder&fileName)
'在Outlook 2007中打开.msg文件+
设置outEmail=outApp.Session.OpenSharedItem(
Sub LoopThrough(parentFolder As String)
Dim fso As Object
' Create a File System object to loop through folders
If fso Is Nothing Then
Set fso = CreateObject("Scripting.FileSystemObject")
End If
' Get the specified folder
Dim folder As Object
Set folder = fso.GetFolder(parentFolder)
' Call my code on it
MyCode msgfiles.Path
' Get all sub folders
Dim subFolder As Object
On Error Resume Next ' We might have permission issues so lets carry on if we get a folder we cannot access
For Each subFolder In folder.subfolders
On Error GoTo 0 ' If we cant access the folder, reset error
If Not subFolder Is Nothing Then ' Folder will be null/nothing if we had an error so ignore it if it is
LoopThrough subFolder.Path
End If
On Error Resume Next ' When going back over the loop we can still get an error
Next
On Error GoTo 0
End Sub
Sub MyCode(folder As String)
Dim outApp As Object
Dim outEmail As Object
Dim outAttachment As Object
Dim msgfiles As String, sourceFolder As String, saveInFolder As String
Dim fileName As String
Debug.Print folder
msgfiles = folder & "\*.msg"
' Your code Here
''msgfiles = "C:\test\*.msg" 'CHANGE - folder location and filespec of .msg files
saveInFolder = "C:\test 2" 'CHANGE - folder where extracted attachments are saved
If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
sourceFolder = Left(msgfiles, InStrRev(msgfiles, "\"))
On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
If outApp Is Nothing Then
MsgBox "Outlook is not open"
Exit Sub
End If
On Error GoTo 0
fileName = Dir(msgfiles)
While fileName <> vbNullString
'Open .msg file in Outlook 2003
'Set outEmail = outApp.CreateItemFromTemplate(sourceFolder & fileName)
'Open .msg file in Outlook 2007+
Set outEmail = outApp.Session.OpenSharedItem(sourceFolder & fileName)
For Each outAttachment In outEmail.Attachments
outAttachment.SaveAsFile saveInFolder & outAttachment.fileName
Next
fileName = Dir
Wend
End Sub