How to reference a file folder with a variable extension?

I have the following function in a program which creates a new directory(folder) for the current day. This program runs once a day. So everyday it would create a new folder. Example:

s:\temp\MSR\30-Aug-04
s:\temp\MSR\31-Aug-04
s:\temp\MSR\01-Sep-04
and so on....

Function Make_Folder()
 Dim Folder As String
 Folder = "s:\temp\MSR\" & Format(Now, "d-mmmm-yy")
 If Dir(Folder, vbDirectory) = "" Then
    MkDir Folder
 End If
End Function

Then I have the following '.exe' which gets triggered by a rule in Outlook to look in the 'InBox' folder for certain type of messages based on the 'Subject' text that I want stored in the current day folder based on it being created by the above.
I'm not sure how to assign a variable to the following statement below:

oMessage.SaveAs "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") & "Incident Alert.msg", olMSG

The way I have it now it's adding the: Format(Now, "d-mmmm-yy") to the .msg. So it ends up looking like this:

30-Aug-04Incident Alert.msg

I want the message to go into the current day folder. And then the next day the .msg's would go to another new folder that was created.


Private Sub Main()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim oFldr As Outlook.MAPIFolder
Dim oMessage As Outlook.MailItem
Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox)


For Each oMessage In oFldr.Items
    If InStr(1, oMessage.Subject, "Incident Alert") > 0 Then
    oMessage.SaveAs "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") & "Incident Alert.msg", olMSG
    oMessage.Delete
End If

    Next oMessage
 
    Set oMessage = Nothing
    Set oFldr = Nothing
    Set oNs = Nothing
    Set oOutlook = Nothing
   
End Sub

Any Ideas??
LVL 2
Richard KreidlSoftware DeveloperAsked:
Who is Participating?
 
mladenoviczConnect With a Mentor Commented:
if it does not exist then

For Each oMessage In oFldr.Items
    If InStr(1, oMessage.Subject, "Incident Alert") > 0 Then
    Call Make_Folder
    oMessage.SaveAs "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") & "Incident Alert.msg", olMSG
    oMessage.Delete
End If
0
 
mladenoviczCommented:
oMessage.SaveAs "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") & "\Incident Alert.msg", olMSG
0
 
x50FadeCommented:
Yup...you just forgot your slash before your filename....as mladenovicz pointed out
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Richard KreidlSoftware DeveloperAuthor Commented:
I get an internal application error when I added the slash.
0
 
x50FadeCommented:
Are you sure that s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") exists...have you allready created it?
0
 
mladenoviczCommented:
Private Sub Main()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim oFldr As Outlook.MAPIFolder
Dim oMessage As Outlook.MailItem
Dim FileName As String

Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox)


For Each oMessage In oFldr.Items
    If InStr(1, oMessage.Subject, "Incident Alert") > 0 Then
    FileName = "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") & "Incident Alert.msg"
    Debug.Print FileName
    oMessage.SaveAs FileName, olMSG
    oMessage.Delete
End If

    Next oMessage
 
    Set oMessage = Nothing
    Set oFldr = Nothing
    Set oNs = Nothing
    Set oOutlook = Nothing
   
End Sub

Can you post content of immediate window here?
0
 
mladenoviczCommented:
Function Make_Folder()
 Dim Folder As String
 Folder = "s:\temp\MSR\" & Format(Now, "d-mmmm-yy")
 If Dir(Folder, vbDirectory) = "" Then
    MkDir Folder
 End If
End Function

Shouldn't this

 Folder = "s:\temp\MSR\" & Format(Now, "d-mmmm-yy")

be

 Folder = "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy")
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
Here it is:

s:\temp\MSR\Incident Alert\30-August-04Incident Alert.msg
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
Here it is with a slash before the file name:

s:\temp\MSR\Incident Alert\30-August-04\Incident Alert.msg
0
 
mladenoviczCommented:
Does folder s:\temp\MSR\Incident Alert\30-August-04 exists?
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
I'm still getting an internal application error. Here is the code I'm testing with:
Private Sub Main()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim oFldr As Outlook.MAPIFolder
Dim oMessage As Outlook.MailItem
Dim FileName As String

Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set oFldr = oNs.GetDefaultFolder(olFolderInbox)

For Each oMessage In oFldr.Items
    If InStr(1, oMessage.Subject, "Incident Alert") > 0 Then
    Call Make_Folder
    oMessage.SaveAs "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") & "\Incident Alert.msg", olMSG
    oMessage.Delete
End If


    Next oMessage
 
    Set oMessage = Nothing
    Set oFldr = Nothing
    Set oNs = Nothing
    Set oOutlook = Nothing
   
End Sub

Function Make_Folder()
 Dim Folder As String
 Folder = "s:\group0k\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy")
 If Dir(Folder, vbDirectory) = "" Then
    MkDir Folder
 End If
End Function

0
 
Richard KreidlSoftware DeveloperAuthor Commented:
I found the problem I had one of the folders setup wrong. Thanks for all the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.