-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExtractEmailAttachment.hta
302 lines (266 loc) · 9.88 KB
/
ExtractEmailAttachment.hta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<META http-equiv="Content-Type" content="text/html; charset=windows-1252">
<META name="GENERATOR" content="MSHTML 6.00.2800.1264">
<META http-equiv="MSThemeCompatible" content="Yes">
<HTA:APPLICATION
WINDOWSTATE="normal"
SYSMENU="yes"
SINGLEINSTANCE="no"
SHOWINTASKBAR="yes"
SELECTION="yes"
SCROLLFLAT="no"
SCROLL="no"
NAVIGABLE="yes"
MINIMIZEBUTTON="yes"
MAXIMIZEBUTTON="no"
INNERBORDER="yes"
CONTEXTMENU="yes"
CAPTION="yes"
BORDERSTYLE="normal"
BORDER="thick"
APPLICATIONNAME="AttachmentExtractor"></HTA:APPLICATION>
<head>
<style>
body
{
background-color: buttonface;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana;
font-size: 9pt;
margin-top: 2px;
margin-left: 8px;
margin-right: 3px;
margin-bottom: 3px;
}
#divOutput{
font-family:'Segoe UI', Tahoma, Geneva, Verdana;
font-size: 10pt;
border: 2px inset;
overflow: scroll;
width: 100%;
height: 200px;
}
textarea{
font-family:'Segoe UI', Tahoma, Geneva, Verdana;
font-size: 14pt;
margin-left: 0px;
margin-right: 0px;
width: 358px;
}
</style>
<title>Extract Outlook Email Attachments</title>
</head>
<script language = "vbscript" type = "text/vbscript">
On Error Resume Next
Const DESIGN_WIDTH = 686 ' default popup width in 96 dpi
Const DESIGN_HEIGHT = 325 ' default popup height in 96 dpi
strHTML = ""
Sub Window_onLoad
'-------------------------------------------------------------------
' Main function
'-------------------------------------------------------------------
Window.ResizeTo DESIGN_WIDTH, DESIGN_HEIGHT
WindowLeft = (Window.screen.availWidth / 2 - DESIGN_WIDTH /2 )
WindowTop = (Window.screen.availHeight / 2 - DESIGN_HEIGHT / 2 )
Window.moveTo WindowLeft, WindowTop
mailboxHTML.InnerHTML = "<font color=""darkred"" style=""font-size:10pt; font-weight:Bold;"">Connecting to Outlook Application ...</br></font>"
get_primary_mailbox
End Sub
Sub DisplayOutputDiv(msg)
'-------------------------------------------------------------------
' Print msg in the outputDiv inner html
'-------------------------------------------------------------------
divOutput.innerHTML = divOutput.innerHTML & msg & "</br>"
divOutput.doScroll("pageDown")
End Sub
Sub PickFolder
'-------------------------------------------------------------------
' On Browse Folder event click
'-------------------------------------------------------------------
Set folder = objShellApp.BrowseForFolder(0, "Please select the folder.", 1, "")
If Not folder is Nothing Then
txt_output.value = folder.Self.path
End If
End Sub
Sub get_primary_mailbox
'-------------------------------------------------------------------
' Populate the dropdownlist with the folders in the outlook mailbox
'-------------------------------------------------------------------
DisplayOutputDiv "Connecting Outlook..."
strHTML = "<select style=""width:425px;"" id=""MailboxSelect"" name=""MailboxSelect"">"
strHTML = strHTML & "<option value=""""" & ">" & "Select a Folder" & "<" & "/option>"
For Each folder in objNamespace.Folders
If Not Instr(folder.Name, "Internet") Then
DisplayOutputDiv folder.Name
DisplayOutputDiv "Accessing Root: " & folder.FullFolderPath
strHTML = strHTML & "<option value=" & Chr(34) & folder.FullFolderPath & Chr(34) & ">" & folder.FullFolderPath & "</option>"
For Each subfolder in folder.Folders
recursiveFetchFolder subfolder
Next
End If
DisplayOutputDiv "------------------------"
Next
strHTML = strHTML & "</select>"
mailboxHTML.innerHTML = strHTML
DisplayOutputDiv "Ready to extract<br>"
End Sub
Sub recursiveFetchFolder(objFolder)
'-------------------------------------------------------------------
' Get folder and subfolder names and append to the drop down list
'-------------------------------------------------------------------
DisplayOutputDiv "Accessing Subfolder: " & objFolder.FullFolderPath
strHTML = strHTML & "<option value=" & Chr(34) & objFolder.FullFolderPath & Chr(34) & ">" & objFolder.FullFolderPath & "</option>"
For Each objSubFolder in objFolder.Folders
recursiveFetchFolder objSubFolder
Next
End Sub
Sub createFolder(path)
'-------------------------------------------------------------------
' Create folder if does not exist
'-------------------------------------------------------------------
output_folder_path = txt_output.value & path
If Not fso.FolderExists(output_folder_path) Then
fso.CreateFolder output_folder_path
DisplayOutputDiv "Creating Folder Dir...." & chr(34) & output_folder_path & chr(34) & "</br>"
End If
End Sub
Sub SaveAllAttachments(colItems, output_folder_path)
'-------------------------------------------------------------------
' Saving all attachment in folder
'-------------------------------------------------------------------
For Each objMessage In colItems
intCount = objMessage.Attachments.Count
DisplayOutputDiv "Checking >> <i>" & objMessage.ConversationTopic & "</i>"
If intCount > 0 Then
For i = 1 To intCount
If Not fso.FolderExists(output_folder_path) Then
fso.CreateFolder output_folder_path
DisplayOutputDiv "Creating Sub Folder Dir...." & output_folder_path & "</br>"
End If
' Edit Here for file name / extension / save path
' Do not save signatures images or in body images
If instr(objMessage.Attachments.Item(i).FileName, "image0") = 0 Then
DisplayOutputDiv "<font color=""green"" font-weight:Bold;"">Saving ->>>>>> " & output_folder_path & objMessage.SenderName & " - " & objMessage.Attachments.Item(i).FileName & "</font>"
objMessage.Attachments.Item(i).SaveAsFile output_folder_path & objMessage.SenderName & " - " & objMessage.Attachments.Item(i).FileName
' totalExtract = totalExtract + 1
end if
Next
End If
Next
End Sub
Sub LoopFolderForAttachment(objFolder, checkPath)
'-------------------------------------------------------------------
' Extracts attachments of every mail in the folder and subfolder
'-------------------------------------------------------------------
For Each objSubfolder in objFolder.Folders
DisplayOutputDiv "Retrieving Items >>> " & objSubfolder.Name
SaveAllAttachments objSubfolder.Items, txt_output.value & checkPath & "\" & objSubfolder.Name & "\"
LoopFolderForAttachment objSubFolder, checkPath & "\" & objSubfolder.Name
Next
End Sub
Sub ExtractClick
'-------------------------------------------------------------------
' Checks for folder path and mailbox path before extracting
'-------------------------------------------------------------------
if MailboxSelect.SelectedIndex = 0 then
msgbox "You have to select a folder from list.", 48, "Select Folder"
exit sub
end if
ButtonDisabled True
On Error Resume Next
DisplayOutputDiv ""
' Check if output folder is valid
If Not fso.FolderExists(txt_output.value) Then
msgbox "Invalid Output Path"
Else
' Get path for input
' check if path is valid in the mail
DisplayOutputDiv "Checking mailbox folders..."
mailbox_path = Right(MailboxSelect.Value, Len(MailboxSelect.Value) - 2)
' Get the starting folder, which is the last folder name of the path
checkPath = ""
loopCount = 0
spiltPath = split(mailbox_path, "\")
For Each path in spiltPath
checkPath = checkPath & "\" & path
DisplayOutputDiv "Accessing >>> " & checkPath
createFolder checkPath
If loopCount = 0 Then
Set objFolder = objOutlook.Session.Folders.item(path)
Else
Set objFolder = objFolder.Folders.Item(path)
End If
loopCount = loopCount + 1
Next
' Check the starting folder attachements before going into subfolders
'folderToStart = spiltPath(UBound(spiltPath))
DisplayOutputDiv "Retrieving Items >>> " & objFolder.Name
Set colItems = objFolder.Items
SaveAllAttachments objFolder.Items, txt_output.value & checkPath & "\"
' Loop the subfolders
LoopFolderForAttachment objFolder,checkPath
MsgBox "Done"
End If
ButtonDisabled False
End Sub
Sub ButtonDisabled(Toggle)
'-------------------------------------------------------------------
' Toggle the button to enable/disable
'-------------------------------------------------------------------
btn_pickfolder.Disabled = Toggle
btn_extract.Disabled = Toggle
End Sub
</SCRIPT>
<body style="background-color: #FFFFFF;margin:0;scroll:no;" onload="zoom()">
<table cellspacing="0" cellpadding="0" border="0" weight="100%" width="100%">
<tr>
<td style="width: 10px"> </td>
<td width="26%">
Email Folder Path:
</td>
<td>
<span id="mailboxHTML"></span>
</td>
<td> </td>
<td></td>
</tr>
<tr>
<td style="width: 10px"> </td>
<td>
Output Folder Dir:
</td>
<td>
<input class="input_style" ID="txt_output" onFocus="Javascript:this.select()" style="border-style;width:420px;font-size:8pt;" type="text" disabled/>
</td>
<td style="width: 10px"> </td>
<td align="right">
<input id=btn_pickfolder style="width:100px" type="button" value="Browse Output" name="check_button" onClick="PickFolder()">
<span style="width:10px"></span>
</td>
</tr>
</table>
<table border="0" width="100%">
<tr>
<td align="right" width="100%">
<input id=btn_extract style="width:97px" type="button" value="Extract" name="check_button" onClick="ExtractClick()">
<span style="width:10px"></span>
</td>
</tr>
</table>
<div id="divOutput">...<br></div>
</body>
<script type="text/javascript">
function zoom() {
document.body.style.zoom = "100%"
}
</script>
<SCRIPT LANGUAGE="VBScript">
Dim objShell,WshNetwork,objShellApp,fso,objNamespace,objOutlook,objMailbox
Set objShell = CreateObject("WScript.Shell")
Set objShellApp = CreateObject("Shell.Application")
Set WshNetwork = CreateObject("WScript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
</SCRIPT>
</html>