Connecting Tech Pros Worldwide Help | Site Map

Print Access 2007 Attachment with VBA

  #1  
Old February 19th, 2009, 12:37 AM
Newbie
 
Join Date: Aug 2008
Posts: 31
Hi,

I dont even know where to start on this one. I found this on the net (everything in italics are the authors comments):

Expand|Select|Wrap|Line Numbers
  1. Public Function OpenFirstAttachmentAsTempFile(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String) As String 
  2.  
  3.     Dim rstChild As DAO.Recordset2
  4.     Dim fldAttach As DAO.Field2
  5.     Dim strFilePath As String
  6.     Dim strTempDir As String
  7.  
  8.     strTempDir = Environ("Temp") ' Get the Temp directory from the environment variable.
  9.     If Right(strTempDir, 1) <> "\" Then strTempDir = strTempDir & "\" ' Make sure the path always ends with a backslash.
  10.         Set rstChild = rstCurrent.Fields(strFieldName).Value ' the .Value for a complex field returns the underlying recordset.
  11.         strFilePath = strTempDir & rstChild.Fields("FileName").Value ' Append the name of the first (and only) attached file to temp dir.
  12.         If Dir(strFilePath) <> "" Then ' the file already exists--delete it first.
  13.         VBA.SetAttr strFilePath, vbNormal ' remove any file attributes (e.g. read-only) that would block the kill command.
  14.         VBA.Kill strFilePath ' delete the file.
  15.     End If
  16.  
  17.     Set fldAttach = rstChild.Fields("FileData") ' The binary data of the file.
  18.     fldAttach.SaveToFile strFilePath
  19.     rstChild.Close ' cleanup
  20.     VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch  the file.
  21.  
  22. End Function 'OpenFirstAttachmentAsTempFile
  23.  

Then I wrote this function just to test it in a database that has a Table1 with an Attachment field named "Files" in which I had already placed a file:
Expand|Select|Wrap|Line Numbers
  1. Public Function TestOpenFirstAttachmentAsTempFile()
  2.     Dim dbs As DAO.Database
  3.     Dim rst As DAO.Recordset
  4.  
  5.     Const strTable = "Table1"
  6.     Const strField = "Files" ' Attachment field in Table1
  7.  
  8.     Set dbs = CurrentDb
  9.     Set rst = dbs.OpenRecordset(strTable)
  10.     'rst.MoveNext ' Uncomment this line to go to the 2nd row in the Table.
  11.     OpenFirstAttachmentAsTempFile rst, strField
  12.     rst.Close
  13. End Function 
  14.  
By using the VBA.Shell command with Explorer.exe, the file will be opened just as if you double clicked it from Windows Explorer!

But i would like to take it one step farther and print it with the default printer. Does anybody think that is possible, or even worth doing. Basically i have documents attached (PDF documents), and want them to print out as the last 2 pages of a report (actually printed to a pdf document, if that helps at all).

Again, as i always say "Any help is greatly appreciated".
  #2  
Old February 19th, 2009, 07:07 AM
Expert
 
Join Date: Jan 2008
Posts: 365

re: Print Access 2007 Attachment with VBA


Quote:
Originally Posted by crazyhouse View Post
Hi,

I dont even know where to start on this one. I found this on the net (everything in italics are the authors comments):

Expand|Select|Wrap|Line Numbers
  1. Public Function OpenFirstAttachmentAsTempFile(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String) As String 
  2.  
  3.     Dim rstChild As DAO.Recordset2
  4.     Dim fldAttach As DAO.Field2
  5.     Dim strFilePath As String
  6.     Dim strTempDir As String
  7.  
  8.     strTempDir = Environ("Temp") ' Get the Temp directory from the environment variable.
  9.     If Right(strTempDir, 1) <> "\" Then strTempDir = strTempDir & "\" ' Make sure the path always ends with a backslash.
  10.         Set rstChild = rstCurrent.Fields(strFieldName).Value ' the .Value for a complex field returns the underlying recordset.
  11.         strFilePath = strTempDir & rstChild.Fields("FileName").Value ' Append the name of the first (and only) attached file to temp dir.
  12.         If Dir(strFilePath) <> "" Then ' the file already exists--delete it first.
  13.         VBA.SetAttr strFilePath, vbNormal ' remove any file attributes (e.g. read-only) that would block the kill command.
  14.         VBA.Kill strFilePath ' delete the file.
  15.     End If
  16.  
  17.     Set fldAttach = rstChild.Fields("FileData") ' The binary data of the file.
  18.     fldAttach.SaveToFile strFilePath
  19.     rstChild.Close ' cleanup
  20.     VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch  the file.
  21.  
  22. End Function 'OpenFirstAttachmentAsTempFile
  23.  

Then I wrote this function just to test it in a database that has a Table1 with an Attachment field named "Files" in which I had already placed a file:
Expand|Select|Wrap|Line Numbers
  1. Public Function TestOpenFirstAttachmentAsTempFile()
  2.     Dim dbs As DAO.Database
  3.     Dim rst As DAO.Recordset
  4.  
  5.     Const strTable = "Table1"
  6.     Const strField = "Files" ' Attachment field in Table1
  7.  
  8.     Set dbs = CurrentDb
  9.     Set rst = dbs.OpenRecordset(strTable)
  10.     'rst.MoveNext ' Uncomment this line to go to the 2nd row in the Table.
  11.     OpenFirstAttachmentAsTempFile rst, strField
  12.     rst.Close
  13. End Function 
  14.  
By using the VBA.Shell command with Explorer.exe, the file will be opened just as if you double clicked it from Windows Explorer!

But i would like to take it one step farther and print it with the default printer. Does anybody think that is possible, or even worth doing. Basically i have documents attached (PDF documents), and want them to print out as the last 2 pages of a report (actually printed to a pdf document, if that helps at all).

Again, as i always say "Any help is greatly appreciated".

Hey crasyhouse,

I found this link about being able to print batches of PDF files from OLE or DDE.

See if this will help?

http://support.adobe.com/devsup/devs...docs/51412.htm

Thanks,

Joe P.
  #3  
Old July 2nd, 2009, 06:19 PM
Newbie
 
Join Date: Jul 2009
Posts: 1

re: Print Access 2007 Attachment with VBA


I was wondering if anyone had an answer for this. I can run the code in this post and it works great to open assigned attachments from the table (pdf), but what i want to do also is:

First run the access reports, then either save these Access reports as a pdf or open the pdf attachments per record and create one document as a pdf. Can anyone help me out on this? Thanks so much!
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem exporting multivalued field to Word 2007 template using Access 2007 Mientje answers 1 November 6th, 2008 07:56 PM
How to disclose attributes of an entity Hulas answers 1 March 23rd, 2008 10:29 PM
Exporting AC2007 Report with Images to HTML scubasteve answers 6 January 29th, 2008 09:57 AM
Access 2007 Custom Menu Bars Wayne answers 27 June 5th, 2006 12:35 AM