 |
|
|
|
| Author |
Message |
Perge
Joined: 27 Feb 2008 Posts: 3
|
Posted: Wed Feb 27, 2008 2:13 pm Post subject: automated saving body and attachment |
|
|
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?
Archived from group: microsoft>public>outlook>program_vba |
|
| Back to top |
|
 |
Sue Mosher [MVP-Outlook]
Joined: 12 Aug 2007 Posts: 656
|
Posted: Wed Feb 27, 2008 6:30 pm Post subject: Re: automated saving body and attachment |
|
|
> 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 Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
"Perge" wrote in message @microsoft.com...
>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? |
|
| Back to top |
|
 |
Perge
Joined: 27 Feb 2008 Posts: 3
|
Posted: Wed Feb 27, 2008 4:11 pm Post subject: Re: automated saving body and attachment |
|
|
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. |
|
| Back to top |
|
 |
Perge
Joined: 27 Feb 2008 Posts: 3
|
Posted: Wed Feb 27, 2008 6:46 pm Post subject: Re: automated saving body and attachment |
|
|
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 to top |
|
 |
Sue Mosher [MVP-Outlook]
Joined: 12 Aug 2007 Posts: 656
|
Posted: Wed Feb 27, 2008 10:40 pm Post subject: Re: automated saving body and attachment |
|
|
It's a warning, not an error. See http://www.outlookcode.com/article.aspx?ID=52 for your options with regard to the "object model guard" security in Outlook 2000 SP2 and later versions.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
"Perge" wrote in message @microsoft.com...
>
> 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 to top |
|
 |
|
|
| Related Topics: | Adding af signature file at the end of the body text Hi I tried to put in the signature after I have made a mailtext in VBA , but the text is replaced by the signature the code is like this v=" Myletter text" With OutMail .to = "yyy@xxx.com" .CC = "" .BCC = "" .Subject = "Subject text" .body = v & Chr(13)
VBA code to put standard string into body of exisiting task I want to make a script that puts standard textstring into the body of an open task. I have found a lot on the internet about the subject, but they all create a new task. I only want to automate a standard string (like date and name) into the bodyof an op
Save Attachment via VBA Hello, I have my inbox linked to an Access DB. Is there a way to create a macro that will save an attachment from an e-mail? If so, what would be a difficulty level of completing? Basically, I would like to setup some criteria in access that decides if
Resend an attachment which is an email There are a lot of related to finding and saving attachments but I think this is a little different. I receive emails from a site's indicating that the email that I sent it could not be delivered. However I know th
Getting location of embedded attachment and saving to disk I'm trying to use Redemption to save attachments that are embedded OLE links in the notes field of a contact item. This is my first attempt at using Redemption so I'm not quite sure what I'm doing wrong here. If anyone can point out my error, I'd apprecia |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|