Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving e-mails to text file 3

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
Hi can anyone tell me how I could save an email with the subject "OFF SITE MOVEMENT (todays date)" to a file call off.txt This can be triggered by a rule or preferably from an excel spreadsheet. If it's done in excel I need it to check to see if outlook is open if it is then fine, if not open outlook and then carry on Or it could have a button that when clicked ok will open outlook.

Please help. I'm currently saving these e-mails manually and then running the macro which tidies the data. I would really apreciate some help.

Thanks
 
Set objApp = GetObject(, "Outlook.Application") '#### Find existing instance of Outlook
If Err Then
WScript.Quit() '#### If Outlook isn't running, quit the script
'Set objApp = CreateObject("Outlook.Application") '#### uncomment this & comment out above line to get data even if Outlook isnt running
End If
Set objOutlook = objApp.GetNameSpace("MAPI")

Set objInboxItems = objOutlook.GetDefaultFolder(6).Items '#### Get inbox folder items

For Each aItem In objInboxItems
If aItem.Subject = "hello world" Then
strMessage = aItem.Text 'or .Message or .body
End If

Next

'set objects to nothing


I havent tried this but the idea is what u r after i think
 
Thanks for the speedy response. It looks exactly like what I'm trying to do. I'll have a play and I'll post back if it did/didn't work.
 
Ok I've got
Code:
Set objApp = GetObject(, "Outlook.Application")        '#### Find existing instance of Outlook
If Err Then
'  WScript.Quit()                    '#### If Outlook isn't running, quit the script
 Set objApp = CreateObject("Outlook.Application")    '#### uncomment this & comment out above line to get data even if Outlook isnt running
End If
Set objOutlook = objApp.GetNameSpace("MAPI")

Set objInboxItems = objOutlook.GetDefaultFolder(6).Items     '#### Get inbox folder items

For Each aItem In objInboxItems
   If aItem.Subject = "TAPE ON SITE MOVEMENT REPORT FOR " Then
     strMessage = aItem.Text 'or .Message or .body
     Else
     If aItem.Subject = "TAPE OFF SITE MOVEMENT REPORT FOR " Then
     strMessage2 = aItem.Text 'or .Message or .body
     End If
   End If

Next

'set objects to nothing
Set objApp = Nothing
Set objOutlook = Nothing
Set objInboxItems = Nothing

But how do I save strmessage2 to off.txt and strmessage to on.txt?

Cheers
 
sounds good to me.
you might want to do something like

strMessage = ""
strMessage2 = ""
For Each aItem In objInboxItems
If aItem.Subject = "TAPE ON SITE MOVEMENT REPORT FOR " Then
strMessage = strMessage & vbCR & "NEXT ONE" & vbCR & aItem.Text 'or .Message or .body
Else
If aItem.Subject = "TAPE OFF SITE MOVEMENT REPORT FOR " Then
strMessage2 = strMessage2 & vbCR & "NEXT ONE" & vbCR & aItem.Text 'or .Message or .body
End If
End If

Next


'that way you only have to write the 2 strings to the 2 file once

 
sorry I'm not too sure what you mean by "that way you only have to write the 2 strings to the 2 file once".

I can't see what NEXT ONE is either. How would I write the outputs of strmessage & strmessage2 to 2 txt files?

Sorry to be a pain!

 
'example 1, you only have to write a text string to a text
'file onmce at the end. this will be quicker.
'you might want to do something else to your text sting
'before writing it to the file
'the use of the NEXT ONE was to delimit each message that is written to the logfile, crude yes but..
strMessage = ""
strMessage2 = ""
Set tsOffLog = FSO.OpenTextFile("c:\offsite.txt", 8, True)
Set tsOnLog = FSO.OpenTextFile("c:\onsite.txt", 8, True)
For Each aItem In objInboxItems
If aItem.Subject = "ON SITE" Then
strMessage = strMessage & vbCR & "NEXT ONE" & vbCR & aItem.Text
Else
If aItem.Subject = "OFF SITE" Then
strMessage2 = strMessage2 & vbCR & "NEXT ONE" & vbCR & aItem.Text
End If
End If

Next

tsOnLog.WriteLine strMessage
tsOffLog.writeLine strMessage2
tsOnLog.Close
tsOffLog.Close
Set tsOnLog = Nothing
Set tsOffLog = Nothing

'example 2, you have to write a line to the log file from
'each of the if then statements. this will mean more writes to the file which will be slower, imagine if it were over a ras connection and then file was on a server.

strMessage = ""
strMessage2 = ""
Set tsOffLog = FSO.OpenTextFile("c:\offsite.txt", 8, True)
Set tsOnLog = FSO.OpenTextFile("c:\onsite.txt", 8, True)
For Each aItem In objInboxItems
If aItem.Subject = "ON SITE" Then
tsOnLog.writeline CStr(aItem.Text)
Else
If aItem.Subject = "OFF SITE" Then
tsOffLog.WriteLine CStr(aItem.Text)
End If
End If

Next

tsOnLog.Close
tsOffLog.Close
Set tsOnLog = Nothing
Set tsOffLog = Nothing

i guess it comes down to which one you prefer
 
Thanks for all your help so far. Just got a little bit more of a problem with this. The e-mails that i recieve are generated with the date in the subject line. I've got the format set-up in the script and it should work. Only problem is that it doesn't! Is there a way to add a wild card to the subject line. Or can you look at the code below and tell me what i'm doing wrong.

E-mail subject: OFF SITE MOVEMENT REPORT FOR 24-SEP-2003
Code:
Dim fso, myfile, myfile2, strMessage, strMessage2
strMessage = ""
strMessage2 = ""
Set fso = CreateObject("scripting.FileSystemObject")
Set myfile = fso.CreateTextFile("C:\ON.txt", True)
Set myfile2 = fso.CreateTextFile("C:\OFF.txt", True)
Dim today
today = (Day(Date) & "-" & MonthName(Month(Date), True) & "-" & Right(Year(Date), 2))
        
Set objApp = GetObject(, "Outlook.Application")        '#### Find existing instance of Outlook
If Err Then
'  WScript.Quit()                    '#### If Outlook isn't running, quit the script
 Set objApp = CreateObject("Outlook.Application")    '#### uncomment this & comment out above line to get data even if Outlook isnt running
End If
Set objOutlook = objApp.GetNameSpace("MAPI")

Set objInboxItems = objOutlook.GetDefaultFolder(6).Items     '#### Get inbox folder items

For Each aItem In objInboxItems
   If aItem.Subject = "ON SITE MOVEMENT REPORT FOR "  & today & "" Then
'     strMessage = aItem.Message 'or .Message or .body .text
strMessage = strMessage & vbCr & "NEXT ONE" & vbCr & aItem.body
     Else
'     If aItem.Subject = "**DIALOG** TAPE OFF SITE MOVEMENT REPORT FOR " & today & "" Then
     '     strMessage2 = aItem.Message     'or .Message or .body .text
strMessage2 = strMessage2 & vbCr & "NEXT ONE" & vbCr & aItem.body
     End If
   End If

Next
myfile.Writeline (strMessage)
myfile2.Writeline (strMessage2)
myfile.Close
myfile2.Close

'set objects to nothing
Set objApp = Nothing
Set objOutlook = Nothing
Set objInboxItems = Nothing
Set fso = Nothing
Set myfile = Nothing
Set myfile2 = Nothing

thanks for your patience!
 
try LCase() on all your strings.

or just display the strings for debuggin

For Each aItem In objInboxItems
wscript.echo aItem.Subject & " " & today

that way you can compare the strings on screen and you should see what is going on...

dont forget about the case sensitive stuff

 
I think I figured it out just this second. The subject contains a 4 digit year. My date format (today) contains a 2 digit year. I only worked this out when I was explaining the frustrations this has been causing me to a colleague!

I'll post back and let you know.
 
That was part of the problem, but the other part came from what you said about the case sensitivity. The SEP part of the date is of course uppercase, yet by default the way my date was displayed was 24-Sep-2003.

Thanks. for all your help. If I could give you 2 stars I would. Thanks again!
 
i guess you wouldnt want to run it server side, if by server side you are meaning asp. as the server wouldnt have outlook installed etc
 
You could run the script server side, if outlook is installed. Ie a home/work pc being used as a web server etc. As for server hosting, chances are that any company that you'd pay to host your asp pages probably wouldn't install outlook as a security measure.

Also you may need a few modifications to the code, to make it ASP compatable.
 
Hi well you c i have to make a prototype and i need to be able to write to a file on the client no servers at all and ths not using asp
 
That shouldn't be a problem, just use a macro in word / excel. You can open / write various files quite easily. I've got my script running on my client PC at work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top