forked from christwellman/VBAMacros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigger and better Parsing script.vb
342 lines (258 loc) · 10.2 KB
/
bigger and better Parsing script.vb
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This procedure exports parsed data from an email and exports the '
' items to an excel workbook/sheet '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ExportToExcel()
On Error GoTo ErrHandler
Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Excel.Range
Dim strSheet As String
Dim strPath As String
Dim intRowCounter As Integer
Dim intColumnCounter As Integer
Dim msg As Outlook.MailItem
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim NextRow As Integer
Dim projType As String
'Text Parsing Variables
Dim ParseText As String
Dim ParseDate As Double
Dim ParseNumber As Integer
Dim intLocLabel As Integer
Dim intLocCRLF As Integer
Dim intLenLabel As Integer
Dim strText As String
'Define location of document to add new records to
'File name
strSheet = "DataCenterPracticeNewMetricsDatasheet.xlsm"
'File Path
strPath = "C:\Users\ctwellma\Desktop\"
strSheet = strPath & strSheet
Debug.Print strSheet
'Select export folder
Set nms = Application.GetNamespace("MAPI")
Set fld = nms.PickFolder
'Handle potential errors with Select Folder dialog box.
If fld Is Nothing Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub
ElseIf fld.DefaultItemType <> olMailItem Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub
ElseIf fld.Items.Count = 0 Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub
End If
'Open and activate Excel workbook.
Set appExcel = CreateObject("Excel.Application")
appExcel.Workbooks.Open (strSheet)
Set wkb = appExcel.ActiveWorkbook
Set wks = wkb.Sheets(1)
wks.Activate
appExcel.Application.Visible = True
'Where to start Populating Data:
'NextRow = 3
NextRow = LastRow(wks.Range("A1:AS1"))
'***Debug Find LastRow?
'MsgBox "Last Row is: " & NextRow
intRowCounter = NextRow
'Copy field items in mail folder.
For Each itm In fld.Items
intColumnCounter = 1
Set msg = itm
intRowCounter = intRowCounter + 1
PID = ParseTextLinePair(msg.body, "PID:")
'Parse Submit time
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Date and Time of Submission:")
'rng.Value = CDate(SumbissionDate)
intColumnCounter = intColumnCounter + 1
'Parse Requestor
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = msg.SenderName
intColumnCounter = intColumnCounter + 1
'Parse PID
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "PID:")
intColumnCounter = intColumnCounter + 1
'PID Status
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = InputBox(Prompt:="What is the project Status in OP for PID: " & PID, Title:="Project Type?", Default:="")
intColumnCounter = intColumnCounter + 1
'Customer Name
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Customer Name:")
intColumnCounter = intColumnCounter + 1
'Eng Desk Notes
intColumnCounter = intColumnCounter + 1
'Service Type(s)
intColumnCounter = intColumnCounter + 1
'Delivery Location City
Set rng = wks.Cells(intRowCounter, intColumnCounter)
locCity = ParseTextLinePair(msg.body, "Customer Site Location:")
rng.Value = InputBox(Prompt:="What is the delivery City for PID: " & PID, Title:="Delivery City?", Default:=locCity)
intColumnCounter = intColumnCounter + 1
'Delivery Location State
Set rng = wks.Cells(intRowCounter, intColumnCounter)
locState = ParseTextLinePair(msg.body, "Customer Site Location:")
rng.Value = InputBox(Prompt:="What is the delivery State for PID: " & PID, Title:="Delivery State?", Default:=locState)
intColumnCounter = intColumnCounter + 1
'Request Type
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Request Type:")
intColumnCounter = intColumnCounter + 1
'Project Type
Set rng = wks.Cells(intRowCounter, intColumnCounter)
projType = ParseTextLinePair(msg.body, "Project Type:")
rng.Value = InputBox(Prompt:="What is the project type in OP for PID: " & PID, Title:="Project Type?", Default:=projType)
intColumnCounter = intColumnCounter + 1
'Customer Primary Contact
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Customer Primary Contact:")
intColumnCounter = intColumnCounter + 1
'Services Description
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Service Description:")
intColumnCounter = intColumnCounter + 1
'Services Revenue
Set rng = wks.Cells(intRowCounter, intColumnCounter)
ServRev = ParseTextLinePair(msg.body, "Services Revenue:")
rng.Value = InputBox(Prompt:="How much service revenue is generated by PID: " & PID, Title:="Services Revenue?", Default:=ServRev)
intColumnCounter = intColumnCounter + 1
'Funding
intColumnCounter = intColumnCounter + 1
'Oracle Project Name
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = InputBox(Prompt:="What's the OP Project Name for PID: " & PID, Title:="OP Project Name?", Default:="")
intColumnCounter = intColumnCounter + 1
'Theater
Set rng = wks.Cells(intRowCounter, intColumnCounter)
Segment = ParseTextLinePair(msg.body, "Theather/Market: Mkt Seg -")
rng.Value = InputBox(Prompt:="What is the project segment in OP for PID: " & (ParseTextLinePair(msg.body, "PID:")), Title:="Project Type?", Default:=Segment)
'rng.Value = ParseTextLinePair(msg.body, "Theather/Market: Mkt Seg - ")
intColumnCounter = intColumnCounter + 1
'SO#
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Sales Order Nbr:")
intColumnCounter = intColumnCounter + 1
'Deal ID
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "DID:")
intColumnCounter = intColumnCounter + 1
'Project Start Date
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Project Start Date:")
rng.Value = CDate(rng.Value)
'need to add if statement to only CDATE If numeric
intColumnCounter = intColumnCounter + 1
'Project Kick Off Date
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Project Kick-Off Meeting:")
rng.Value = CDate(rng.Value)
intColumnCounter = intColumnCounter + 1
'Project End Date
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "End Date:")
rng.Value = CDate(rng.Value)
intColumnCounter = intColumnCounter + 1
'Margin Analysis
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Latest version of proposal or SOW:")
intColumnCounter = intColumnCounter + 1
'SOW/ASPT Quote
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = ParseTextLinePair(msg.body, "Margin analysis spreadsheet:")
intColumnCounter = intColumnCounter + 1
'Market Segment
''''Segment Vs Theater??
intColumnCounter = intColumnCounter + 1
'Project Status
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = "New"
intColumnCounter = intColumnCounter + 1
'Delivery Close Date
intColumnCounter = intColumnCounter + 1
'Project Close Date
intColumnCounter = intColumnCounter + 1
'Past Due Date
intColumnCounter = intColumnCounter + 1
'Walker Survey Sent Date
intColumnCounter = intColumnCounter + 1
'Sales rep
intColumnCounter = intColumnCounter + 1
'DC PM Assigned
'Set rng = wks.Cells(intRowCounter, intColumnCounter)
'rng.Value = "Needs DCPM"
intColumnCounter = intColumnCounter + 1
'Work Manager Assigned
'Set rng = wks.Cells(intRowCounter, intColumnCounter)
'rng.Value = "Needs DCPM"
intColumnCounter = intColumnCounter + 1
'Technical resourcing Status
intColumnCounter = intColumnCounter + 1
'Initial Follow up Sent
intColumnCounter = intColumnCounter + 1
'DCPM Assigned Date
intColumnCounter = intColumnCounter + 1
'Technical Resource Assigned Date
intColumnCounter = intColumnCounter + 1
'WM has PID
intColumnCounter = intColumnCounter + 1
'PM Assigned to PID
intColumnCounter = intColumnCounter + 1
'Last Cost Forecast Date
intColumnCounter = intColumnCounter + 1
'Days since last Cost Forecast
intColumnCounter = intColumnCounter + 1
'Workplan Chargeable
intColumnCounter = intColumnCounter + 1
'Revenue Recognized to date
intColumnCounter = intColumnCounter + 1
'Costs to Date
intColumnCounter = intColumnCounter + 1
'Margin
intColumnCounter = intColumnCounter + 1
Next itm
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
Exit Sub
ErrHandler: If Err.Number = 1004 Then
MsgBox strSheet & " doesn't exist", vbOKOnly, "Error"
Else
MsgBox Err.Number & "; Description: ", vbOKOnly, "Error"
End If
'Zero Variables
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = CreateObject("Outlook.Application")
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
Case Else
' anything else will result in an error, which is
' why we have the error handler above
End Select
Set objApp = Nothing
End Function