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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to use command button to default and print to PDF.writer. 1

Status
Not open for further replies.

davesaint

IS-IT--Management
Feb 22, 2002
86
US
I want to able to add to command buttons to one of my menus in by MS Access97 DB.

1. How can I set the command button to go out and print the document using PDF writer when my default print is set to antoher printer.

2. Second I would another command button to go out and write a report to a pdf file using pdf writer and then attach it in an email.
 
I've used A third party software from Attac Consulting which works great for your purpose. It allows you to run reports and save them as pdf files at the click of a button as well as send out an email with attachment. They offer a trial too. Here is the link.
If you prefer a "no-cost" solution, Tony's has code that works for Access97 and 2000 to switch printers. "Printer Switch Code" at
I have also done the following which works for creating and sending email attachments it can work alone, or as I am using it, is a function called from a form that allows the user to select various options such as the list of users to send distribution to.

This code is used with Outlook. Format for recipients is semicolon separated such as "EmailID1; EmailID2; EmailID3"

I am including the portion of the code that will send the attachments, you will most likely need to modify it for your use. If you use it more or less as is, you pass it the Recipients, Body, Subject and attachment path. A good portion of the code originally came from elsewhere in tek-tips. You also need fIsFileDIR (from Dev Ashish which I have included further below which is used to check if the file exists.


Function SendOutlookMessage(Recipients As String, Subject As String, Body As String, DisplayMsg As Boolean, Optional CopyRecipients As String, Optional BlindCopyRecipients As String, Optional Importance As Integer = 2, Optional AttachmentPath, Optional AttachmentOptionNumber As Integer)
'Function to create and send an outlook message with more control than sendobject
'separate multiple recipients or CC, or BCC with comma
'importance - 1=low, 2=normal, 3=high

'AttachmentOptionNumber allows additional Attachment based on which Option
'was clicked in order to send the email. -- 05-Aug-2003

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.recipient
Dim objOutlookAttach As Outlook.Attachment
Dim txtRecipient As String
Dim stAttachment As String 'Use to store original attachment so that order of attachments for notice will be correct
Dim stattach As String
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To recipient(s) to the message.
Do While InStr(1, Recipients, &quot;,&quot;, vbTextCompare) <> 0 'checks for multiple recipients and adds each
txtRecipient = Left(Recipients, InStr(1, Recipients, &quot;,&quot;, vbTextCompare) - 1)
Recipients = Trim(Mid(Recipients, Len(txtRecipient) + 2, Len(Recipients)))
Set objOutlookRecip = .Recipients.Add(txtRecipient)
objOutlookRecip.Type = olTo
Loop

Set objOutlookRecip = .Recipients.Add(Trim(Recipients))
objOutlookRecip.Type = olTo

' Add the CC recipient(s) to the message if existing
If CopyRecipients <> &quot;&quot; Then
Set objOutlookRecip = .Recipients.Add(CopyRecipients)
objOutlookRecip.Type = olCC
End If

' Add the BCC recipient(s) to the message.
If BlindCopyRecipients <> &quot;&quot; Then
Set objOutlookRecip = .Recipients.Add(BlindCopyRecipients)
objOutlookRecip.Type = olBCC
End If

' Set the Subject, Body, and Importance of the message.
.Subject = Subject
.Body = Body & vbCrLf & vbCrLf
Select Case Importance
Case 1
.Importance = olImportanceLow
Case 2
.Importance = olImportanceNormal
Case 3
.Importance = olImportanceHigh
Case Else
.Importance = olImportanceNormal
End Select

' Add attachments to the message.
On Error GoTo SendOutlookMessage_err
If Not IsMissing(AttachmentPath) Then
stAttachment = AttachmentPath

If AttachmentOptionNumber = 1 Then
'change extension to .zip to attach zip rather than pdf
AttachmentPath = Replace(AttachmentPath, &quot;Meeting Notice&quot;, &quot;MN&quot;)
AttachmentPath = Replace(AttachmentPath, &quot;.pdf&quot;, &quot;.zip&quot;)
'In order for fIsFileDir to work so that it can check if the file
'exists, needed to store AttachmentPath in a local variable
stattach = AttachmentPath
If fIsFileDIR(stattach) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
Else
'change extension back to .pdf if .zip not found
AttachmentPath = Replace(AttachmentPath, &quot;.zip&quot;, &quot;.pdf&quot;)
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
On Error GoTo SendOutlookMessage_err
AttachmentPath = stAttachment
End If




' AttachmentPath = stAttachmentpath
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
Exit Function
SendOutlookMessage_err:
'If the file isn't found, attach the not found pdf file to
'allow code to continue and user to manually attach file to
'the email
AttachmentPath = &quot;V:\Files\PDF_Files\NotFound.pdf&quot;
'MsgBox &quot;Old Path &quot; & AttachmentPath
Debug.Print AttachmentPath
'AttachmentPath = &quot;M:\Files\NotFound.pdf&quot;

'MsgBox &quot;New Path &quot; & AttachmentPath
Debug.Print AttachmentPath
Resume
ChangeToPDF_err:
'Change Extension from ZIP to PDF If Attachment isn't found
AttachmentPath = Replace(AttachmentPath, &quot;.zip&quot;, &quot;.pdf&quot;)
Resume
End Function


'***************** Code Start *******************
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fIsFileDIR(stpath As String, _
Optional lngType As Long) _
As Integer
'Fully qualify stPath
'To check for a file
' ?fIsFileDIR(&quot;c:\winnt\win.ini&quot;)
'To check for a Dir
' ?fIsFileDir(&quot;c:\msoffice&quot;,vbdirectory)
'
On Error Resume Next
fIsFileDIR = Len(Dir(stpath, lngType)) > 0
End Function
'***************** Code End *********************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top