automated saving body and attachment

  • Thread starter Thread starter Perge
  • Start date Start date
P

Perge

I am trying to make a VB macro to run Outlook, process every email that comes
to the inbox from a machine that sends an identical email subject with a
small amount of descriptive text in the body and a numerical .csv data file
attachment. It reads a unique time, date and type from the body then using
them, saves the body and attachment with a unique filename as a .txt and a
..csv file respectively.
Two problems 1) with the code:

Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
myFolder.Display

it opens a new instance of the inbox every time it executes. How do I
prevent that?

It displays a security error when it tries to save the data. How do I
suppress that error?
 
it opens a new instance of the inbox every time it executes. How do I
prevent that?

Don't call the Display method. There should be no reason to do so for your scenario.
It displays a security error when it tries to save the data. How do I
suppress that error?

Please show the relevant code and give your Outlook version. If you are using Outlook 2003 and derive the object from the intrinsic Application object in Outlook VBA, no security prompt should occur.
 
Sue,
Here is the whole sub. It is being developed in Outlook 2002. When it gets
to the line to save the body it displays a warning.

Sub SaveBodyandAttchment()

'To do
' when email received, run this process

'Outlook objects
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myBody As String
Dim Rdate As String
Dim CType As String
Dim ETD As String
Dim L As Long
Dim SavePath As String


'Open Outlook if not already
Set myOlApp = Application 'CreateObject("Outlook.Application")

'set path to save body.txt and data.csv files
SavePath = "C:\Terroir\SmartRoast data\"

'Display the inbox (if not already)

Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
'myFolder.Display 'diabled to prevent multiple instances

'Select first email and open
' if no items then exit sub
If myFolder.Items.Count <> 0 Then
Set myItem = myFolder.Items(1)
myItem.Display
Else
Exit Sub
End If


'Get the email body text, find type
' key off the word Bean
myBody = myItem.Body
L = InStr(myBody, "Bean")

'go back 33 chars to find type number
L = L - 33

'MsgBox L 'for debugging

'get type number
CType = Mid(myBody, L, 3)

'MsgBox CType 'for debugging

'go back another 31 chars to get date
L = L - 31
'and then get the roast time
Rdate = Mid(myBody, L, 17)

'MsgBox RDate 'for debugging

'remove special chars in date time string
ETD = ""
ETD = Replace(Rdate, "/", "")
ETD = Replace(ETD, ":", "")
ETD = Replace(ETD, " ", "")
ETD = Left(ETD, 10)

'MsgBox ETD 'for debugging

'save email body with unique filename as text
myItem.SaveAs SavePath & CType & "_" & ETD & ".txt", olTXT

'Save the attachment with unique filename as csv
Set myItem = Application.ActiveInspector.CurrentItem
Set myAttachments = myItem.Attachments
myAttachments.Item(1).SaveAsFile SavePath & CType & "_" & ETD & ".csv"

'Delete the email to avoid duplication
myItem.Close (1) 'enabled for debugging
'myItem.Delete 'disbled for debugging

Set myNameSpace = Nothing
Set myFolder = Nothing
Set myItem = Nothing

End Sub

Also I would like to have Outlook display the inbox not the Today window
when it starts up or when this sub runs. Commenting out the myFolder.Display
statement prevents this from ever happening.
 
OK, I figured out how to have Outlook open displaying the inbox and I have
changed the program so it does not open the individual emails and it loops
through the inbox to save them all. Now if if didn't present this error it
would be working (except it needs to run on the email received event).
I don't know how to attach the .bmp file so I will retype the error message:

" A program is trying to access data from Outlook that may include address
book information. Do you want to allow this? If this is unexpected, it may
be a virus and you should choose"No"."
 
Back
Top