Solved

How to reference a file folder with a variable extension?

Posted on 2004-08-30
12
224 Views
Last Modified: 2008-03-17
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??
0
Comment
Question by:rkckjk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11932032
oMessage.SaveAs "s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") & "\Incident Alert.msg", olMSG
0
 
LVL 2

Expert Comment

by:x50Fade
ID: 11932189
Yup...you just forgot your slash before your filename....as mladenovicz pointed out
0
 
LVL 2

Author Comment

by:rkckjk
ID: 11932320
I get an internal application error when I added the slash.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 2

Expert Comment

by:x50Fade
ID: 11932355
Are you sure that s:\temp\MSR\Incident Alert\" & Format(Now, "d-mmmm-yy") exists...have you allready created it?
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11932362
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11932386
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
 
LVL 2

Author Comment

by:rkckjk
ID: 11932406
Here it is:

s:\temp\MSR\Incident Alert\30-August-04Incident Alert.msg
0
 
LVL 2

Author Comment

by:rkckjk
ID: 11932443
Here it is with a slash before the file name:

s:\temp\MSR\Incident Alert\30-August-04\Incident Alert.msg
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11932459
Does folder s:\temp\MSR\Incident Alert\30-August-04 exists?
0
 
LVL 8

Accepted Solution

by:
mladenovicz earned 250 total points
ID: 11932472
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
 
LVL 2

Author Comment

by:rkckjk
ID: 11932565
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
 
LVL 2

Author Comment

by:rkckjk
ID: 11932595
I found the problem I had one of the folders setup wrong. Thanks for all the help
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question