If you know the printer you could try the following code:
Set Net = CreateObject("WScript.Network")
Net.AddWindowsPrinterConnection "\\PrintServer1\Xerox300" 'replace the printer location with one that you know they can connect to.
Net.SetDefaultPrinter "\\PrintServer1\Xerox300"
Or for something a bit more elaborate you could try the following:
1. Create a form with a listbox and two buttons on it.
2. Name the listbox lstPrinters.
3. Give the listbox two columns.
4. Make the listbox column widths = '5;0'
5. Name the buttons cmdChoosePrinter and cmdCancel.
6. Call the button captions anything you like, probably Choose and Cancel would be good choices
Put the following code into the printer choice form (which I call frmPrinterSetup for this example)...
Private Sub list_printer_choice()
' Author: Duane Hennessy
' Company: Bandicoot Software, Australia
' Date: 27/01/2006
' Description: Add a list of printers to a listbox on a form.
' Requirements: This code should be in a form with a listbox called lstPrinters _
The listbox should have: _
1. A column count of 2 _
2. Column Widths of 5;0 _
3. A Rowsource Type of Values
Const strcProcedureName As String = "list_printer_choice"
On Error GoTo err_
Dim printers As String
Dim printer As Object
Dim wmi_service As Object
Const computer_ As String = "."
Dim installed_printers As Object
Set wmi_service = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & computer_ & "\root\cimv2")
Set installed_printers = wmi_service.ExecQuery _
("Select * from Win32_Printer")
For Each printer In installed_printers
printers = printers & printer.Name & ";" & printer.Location & ";"
Next
'Put printers into the listbox
Me.lstPrinters.RowSource = printers
exit_routine:
If Not wmi_service Is Nothing Then Set wmi_service = Nothing
If Not printer Is Nothing Then Set printer = Nothing
If Not installed_printers Is Nothing Then Set installed_printers = Nothing
Exit Sub
err_:
MsgBox Err.Number & vbCrLf & Err.Description, 0 + 48, strcProcedureName
GoTo exit_routine
End Sub
Private Sub cmdChoosePrinter_Click()
' Author: Duane Hennessy
' Company: Bandicoot Software, Australia, 0061-7-3398-6131
' Date: 27/01/2006
' Description: Set up printer for user and close form.
' Requirements: requirements_here
Const strcProcedureName As String = "cmdChoosePrinter_Click"
On Error GoTo err_
Dim chosen_printer_location As String
Dim Net As Object
chosen_printer_location = Me.lstPrinters.Column(1)
Set Net = CreateObject("WScript.Network")
Net.AddWindowsPrinterConnection chosen_printer_location
Net.SetDefaultPrinter chosen_printer_location
exit_routine:
If Not Net Is Nothing Then Set Net = Nothing
Docmd.Close acForm, Me.Name
Exit Sub
err_:
MsgBox Err.Number & vbCrLf & Err.Description, 0 + 48, strcProcedureName
GoTo exit_routine
End Sub
Private Sub cmdCancel_Click()
' Author: Duane Hennessy
' Company: Bandicoot Software, Australia
' Date: 27/01/2006
' Description: Set up printer for user and close form.
' Requirements: requirements_here
On Error Resume Next
Docmd.Close acForm, Me.Name
End Sub
So when trying to send the email I'd trap for the error and open the printer setup form for example:
Private Sub cmdChoosePrinter_Click()
Const strcProcedureName As String = "list_printer_choice"
On Error GoTo err_
Dim try as Long 'because we don't want to be kept in a loop of trying to set up a printer
DoCmd.SendObject acSendReport, "rptMaintWO", acFormatRTF, "E-mail Address",, , "Maintenance Work Order", , False".
exit_routine:
Exit Sub
err_:
If Err.Number = 2296 and try < 2 Then
Docmd.Open acForm, frmPrinterSetup 'or whatever form name you have.
try = try + 1
Resume 'sending the email again.
Else
MsgBox Err.Number & vbCrLf & Err.Description, 0 + 48, strcProcedureName
GoTo exit_routine
End If
End Sub
Hope this helps or at least gives you some ideas
Duane Hennessy
Bandicoot Software
Tropical Queensland, Australia
(ABN: 33 682 969 957)
Your own personal library of code snippets.
http://www.bandicootsoftware.com.au
--- In AccessDevelopers@yahoogroups.com, "pjscott1951" <[EMAIL PROTECTED]> wrote:
>
> I have a command button that sends a maintenance work order report
> via Outlook. I use the following code to send the report as an
> attachment:
>
> "DoCmd.SendObject acSendReport, "rptMaintWO", acFormatRTF, "E-mail
> Address",, , "Maintenance Work Order", , False".
>
> This works fine if the user has a printer or is networked to a
> printer.
>
> The problem is some people log-on to computers that need a networked
> printer setup (and the majority of are using don't have the skills
> to do this). In this case when they click the button to send the
> work order they get a popup "You must install a printer before you
> can print". It's error 2296.
>
> I found if you take out "acSendReport" of the code an e-mail will
> send but the report will not be send the report attachment.
>
> Does anyone know why this is happening and is there anyway to code
> around this error?
>
> Thanks for the help,
>
> Paul
>
Please zip all files prior to uploading to Files section.
SPONSORED LINKS
Microsoft access developer | Microsoft access help | Microsoft access database |
Microsoft access training | Microsoft access training course | Microsoft access programming |
YAHOO! GROUPS LINKS
- Visit your group "AccessDevelopers" on the web.
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.