i \'m trying to get a VBA macro in Outlook that will save an email\'s attachment to a specific folder (that changes every month) and add the YYYYMM of prior month received t
Work with MSDN Split Function
Example on your Subject line Here is the NTMR file for you
split it by space character (" ")
Code Example
Sub Example()
Dim Item As Outlook.mailitem
Set Item = ActiveExplorer.Selection.Item(1)
Debug.Print Item.subject ' Print on Immediate Window (Ctrl+G)
Item.subject = Split(Item.subject, " ")(3)
Debug.Print Item.subject ' Print on Immediate Window (Ctrl+G)
End Sub
Your subject = (Here)(1)
(is)(2)
(the)(3)
(NTMR)(4)
(file)(5)
(for)(6)
(you)(7)
Now Split(subject line), "space")(3)
While assigning to string variable
Dim FileName As String
FileName = Split(Item.subject, " ")(3)
Replace objAtt.DisplayName
with FileName
Dim FileName As String
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & _
dateFormat & "\" & _
"Source Files" & "\" & FileName & dateFormat
Next
By default, or when Limit equals -1, the Split function splits the input string at every occurrence of the delimiter string, and returns the substrings in an array.
When the Limit parameter is greater than zero, the Split function splits the string at the first Limit-1 occurrences of the delimiter, and returns an array with the resulting substrings.
For example,Split("a:b:c", ":") returns the array {"a", "b", "c"}
,
whileSplit("a:b:c", ":", 2) returns the array {"a", "b:c"}
.
To get previous month try DateAdd Function
Example
Option Explicit
Public Sub Example()
Dim PrevMonth As String
PrevMonth = Format(DateAdd("m", -1, Date), "yyyymm")
Debug.Print PrevMonth
End Sub
Some useful date functions worth exploring in other contexts include
DateDiff
,DatePart
,DateSerial
, Day, Month, Year, andIsDate
.IsDate
(which checks whether a string is a valid date) is particularly useful for things likeUserForms
where you may want to force the user to type a valid date into a certain textbox.