PC Review


Reply
Thread Tools Rate Thread

Outlook Attachments via Recordset

 
 
lgray
Guest
Posts: n/a
 
      8th Jul 2008
Can someone take a look through this code to see if they can find the error
in the code. The code is supposed to cycle through a recordset of
path/files located in a table and attach them into an email.

The email part works, the attachment part works if I hard code the path into
VBA. But when I try to attach the file via a recordset I get an error that
reads. "Cant create file:" It then provides the correct name of the file,
and then reads, "Right-click on the folder you want to create the file in,
and then click Properties on the shortcut menu to check your permissions for
the folder.

On debug, the following message of the code is highlighted:
newMailAttachments.Add File1, olByValue, 1, "Test Doc"


Sub createOutlookMailItem()

Dim EDB_V052708_WIP As Database
Dim rs As Recordset
Dim objOutlook As Outlook.Application
Dim newMail As MailItem

Set objOutlook = CreateObject("Outlook.application")
Set newMail = objOutlook.CreateItem(olMailItem)
Set EDB_V052708_WIP = CurrentDb


Set newMailAttachments = newMail.Attachments
Set rs = EDB_V052708_WIP.OpenRecordset("Select * from
Encroachment_Attachments")
File1 = rs("FullPathToFile")
newMail.To = "(E-Mail Removed)"
newMail.Subject = "Encroachment Documents"

While Not rs.EOF
newMailAttachments.Add File1, olByValue, 1, "Test Doc"
Wend

newMail.Display
newMail.Subject = "Documents Confirmation"
newMail.OriginatorDeliveryReportRequested = True
newMail.ReadReceiptRequested = True
newMail.Display

Set newMailAttachments = Nothing
Set newMail = Nothing
Set objOutlook = Nothing


End Sub







--
Linda
 
Reply With Quote
 
 
 
 
roger
Guest
Posts: n/a
 
      9th Jul 2008
When it errors, and highlights that line go to the immediate window and type:

?file1

if your code up that point is correct it should with a valid file path.

If it does then the error is in the newMailAttachments.Add line

if not then its somewhere before that, like the set rs= line or like that

hth roger



"lgray" wrote:

> Can someone take a look through this code to see if they can find the error
> in the code. The code is supposed to cycle through a recordset of
> path/files located in a table and attach them into an email.
>
> The email part works, the attachment part works if I hard code the path into
> VBA. But when I try to attach the file via a recordset I get an error that
> reads. "Cant create file:" It then provides the correct name of the file,
> and then reads, "Right-click on the folder you want to create the file in,
> and then click Properties on the shortcut menu to check your permissions for
> the folder.
>
> On debug, the following message of the code is highlighted:
> newMailAttachments.Add File1, olByValue, 1, "Test Doc"
>
>
> Sub createOutlookMailItem()
>
> Dim EDB_V052708_WIP As Database
> Dim rs As Recordset
> Dim objOutlook As Outlook.Application
> Dim newMail As MailItem
>
> Set objOutlook = CreateObject("Outlook.application")
> Set newMail = objOutlook.CreateItem(olMailItem)
> Set EDB_V052708_WIP = CurrentDb
>
>
> Set newMailAttachments = newMail.Attachments
> Set rs = EDB_V052708_WIP.OpenRecordset("Select * from
> Encroachment_Attachments")
> File1 = rs("FullPathToFile")
> newMail.To = "(E-Mail Removed)"
> newMail.Subject = "Encroachment Documents"
>
> While Not rs.EOF
> newMailAttachments.Add File1, olByValue, 1, "Test Doc"
> Wend
>
> newMail.Display
> newMail.Subject = "Documents Confirmation"
> newMail.OriginatorDeliveryReportRequested = True
> newMail.ReadReceiptRequested = True
> newMail.Display
>
> Set newMailAttachments = Nothing
> Set newMail = Nothing
> Set objOutlook = Nothing
>
>
> End Sub
>
>
>
>
>
>
>
> --
> Linda

 
Reply With Quote
 
pietlinden@hotmail.com
Guest
Posts: n/a
 
      10th Jul 2008
On Jul 8, 2:52*pm, lgray <lindabg...@hotmail.com> wrote:
> Can someone take a look through this code to see if they can find the error
> in the code. *The code is supposed to cycle through a *recordset of
> path/files located in a table and attach them into an email.
>
> The email part works, the attachment part works if I hard code the path into
> VBA. *But when I try to attach the file via a recordset I get an error that
> reads. *"Cant create file:" *It then provides the correct name of thefile,
> and then reads, "Right-click on the folder you want to create the file in,
> and then click Properties on the shortcut menu to check your permissions for
> the folder.
>
> On debug, the following message of the code is highlighted:
> * * * *newMailAttachments.Add File1, olByValue, 1, "Test Doc"
>
> Sub createOutlookMailItem()
>
> * * Dim EDB_V052708_WIP As Database
> * * Dim rs As Recordset
> * * Dim objOutlook As Outlook.Application
> * * Dim newMail As MailItem
>
> * * Set objOutlook = CreateObject("Outlook.application")
> * * Set newMail = objOutlook.CreateItem(olMailItem)
> * * Set EDB_V052708_WIP = CurrentDb
>
> * * Set newMailAttachments = newMail.Attachments
> * * Set rs = EDB_V052708_WIP.OpenRecordset("Select * from
> Encroachment_Attachments")
> * * File1 = rs("FullPathToFile")
> * * newMail.To = "linda.t...@test.com"
> * * newMail.Subject = "Encroachment Documents"
>
> * * While Not rs.EOF
> * * * * newMailAttachments.Add File1, olByValue, 1, "Test Doc"
> * * * * Wend
>
> * * newMail.Display
> * * newMail.Subject = "Documents Confirmation"
> * * newMail.OriginatorDeliveryReportRequested = True
> * * newMail.ReadReceiptRequested = True
> * * newMail.Display
>
> * * Set newMailAttachments = Nothing
> * * Set newMail = Nothing
> * * Set objOutlook = Nothing
>
> End Sub
>
> --
> Linda


Looks like this is the problem:
> While Not rs.EOF
> newMailAttachments.Add File1, olByValue, 1, "Test Doc"
> Wend
>

The extra step of setting File1=rs("FullPathToFile") *outside* of the
loop means that your code never reads the next file name (in the next
record). So instead of using File1 at all, just put the whole thing
inside the loop

> While Not rs.EOF
> newMailAttachments.Add rs.Fields("FullPathToFile"), olByValue, 1,"Test Doc"
> Wend

 
Reply With Quote
 
lgray
Guest
Posts: n/a
 
      16th Jul 2008
I continued to be humbled by this seemingly simple code.....Below is my
version of the code. I get the following error when I run it.
"Run-time error - 1836446541 (928a0cb3):
Can't find this file. Make sure path and filename are correct:
On debug it highlights this statement - "newMail.Attachments.Add
("Encroachment_Attachments")"

Sub createOutlookMailItem()

THE CODE:
Dim EmailCode As Database
Dim rs As Recordset
Dim objOutlook As Outlook.Application
Dim newMail As MailItem

Set objOutlook = CreateObject("Outlook.application")
Set newMail = objOutlook.CreateItem(olMailItem)

Set EmailCode = CurrentDb
newMail.Attachments.Add ("Encroachment_Attachments")

newMail.To = "(E-Mail Removed)"
newMail.Subject = "Encroachment Documents"

Set rs = EmailCode.OpenRecordset("Encroachment_Attachments")
While Not rs.EOF
newMail.Attachments.Add rs.Fields("FullPathToFile"), olByValue, 1, "Test Doc"
Wend

newMail.Display
Set newMailAttachments = Nothing
Set newMail = Nothing
Set objOutlook = Nothing
End Sub


--
Linda


"(E-Mail Removed)" wrote:

> On Jul 8, 2:52 pm, lgray <lindabg...@hotmail.com> wrote:
> > Can someone take a look through this code to see if they can find the error
> > in the code. The code is supposed to cycle through a recordset of
> > path/files located in a table and attach them into an email.
> >
> > The email part works, the attachment part works if I hard code the path into
> > VBA. But when I try to attach the file via a recordset I get an error that
> > reads. "Cant create file:" It then provides the correct name of the file,
> > and then reads, "Right-click on the folder you want to create the file in,
> > and then click Properties on the shortcut menu to check your permissions for
> > the folder.
> >
> > On debug, the following message of the code is highlighted:
> > newMailAttachments.Add File1, olByValue, 1, "Test Doc"
> >
> > Sub createOutlookMailItem()
> >
> > Dim EDB_V052708_WIP As Database
> > Dim rs As Recordset
> > Dim objOutlook As Outlook.Application
> > Dim newMail As MailItem
> >
> > Set objOutlook = CreateObject("Outlook.application")
> > Set newMail = objOutlook.CreateItem(olMailItem)
> > Set EDB_V052708_WIP = CurrentDb
> >
> > Set newMailAttachments = newMail.Attachments
> > Set rs = EDB_V052708_WIP.OpenRecordset("Select * from
> > Encroachment_Attachments")
> > File1 = rs("FullPathToFile")
> > newMail.To = "linda.t...@test.com"
> > newMail.Subject = "Encroachment Documents"
> >
> > While Not rs.EOF
> > newMailAttachments.Add File1, olByValue, 1, "Test Doc"
> > Wend
> >
> > newMail.Display
> > newMail.Subject = "Documents Confirmation"
> > newMail.OriginatorDeliveryReportRequested = True
> > newMail.ReadReceiptRequested = True
> > newMail.Display
> >
> > Set newMailAttachments = Nothing
> > Set newMail = Nothing
> > Set objOutlook = Nothing
> >
> > End Sub
> >
> > --
> > Linda

>
> Looks like this is the problem:
> > While Not rs.EOF
> > newMailAttachments.Add File1, olByValue, 1, "Test Doc"
> > Wend
> >

> The extra step of setting File1=rs("FullPathToFile") *outside* of the
> loop means that your code never reads the next file name (in the next
> record). So instead of using File1 at all, just put the whole thing
> inside the loop
>
> > While Not rs.EOF
> > newMailAttachments.Add rs.Fields("FullPathToFile"), olByValue, 1, "Test Doc"
> > Wend

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook - Print all attachments at once (or: Handle attachments with VBA) Tech Expert Microsoft Outlook Printing 1 26th Aug 2009 06:27 PM
Binding a form to a disconnected recordset and making it capable to update such recordset Yarik Microsoft Access 2 22nd Nov 2006 03:18 AM
Access 2002: bind adodb recordset to listbox recordset property Craig Buchanan Microsoft Access Form Coding 2 1st May 2005 01:14 AM
How to loop through a recordset and update the field of a recordset or delete current record Karen Middleton Microsoft Access Macros 1 4th Jan 2005 11:30 AM
How to loop through a recordset and update the field of a recordset or delete current record Karen Middleton Microsoft Access Queries 1 4th Jan 2005 11:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:52 PM.