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

setting printer 4

Status
Not open for further replies.

Dherrera

Programmer
Mar 22, 2004
86
0
0
US
does the "Set printer" procedure not work in office 2k3? i have this procedure coded that will set the printer to network printer and it worked fine in 2k2 but when i upgraded it stopped working. what gives? here is the code in question

Code:
Dim tempPrinter As Printer
    
    Set tempPrinter = Application.Printers(Me.txtSetPrinter.Value)
    Set Application.Printer = tempPrinter

this is coded in form load section.
 
Have you tried to browse the Application.Printers collection to offer a choice ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
i dont want the user to have a choice of which printer they print to. users are in different branches and i want they to print to default network printer.
 
the error message im getting is: "Invalid procedure call or argument" on the first "Set" line. Could i be missing a reference?
 
Try adding this module.
I can't take credit for it, someone else wrote the code
It does work in Office 2k3
Code:
'************************
' Printer setup module
' Set/retrieves the default printer - originaly for VB6
' Works for A97/a2000
' This is minimal code.
' Albert D.Kallal - 01/13/2002
' Rev history:       Date           Who                   notes
'                    01/13/2002     Albert D. kallal
'
' I wrote this after looking at some the code on the net. Some of the routines
' to change a printer were approaching 500 + of lines of code. Just the printer
' constant defs was over 100 lines of code! Yikes!
' I use only TWO API's (the 3rd one is optional). There is a total of only 4 functions!
' KISS is the word. Keep it simple stupid. I don't care about device drivers, or the
' port number. All these routines just work with the simple printer name. If you do
' actually care about the device driver and port stuff..then use the one of many
' examples available on the net. Those other examples also deal with margins, orientation
' etc.
'
' You can paste this code into a module..and away you go
'
'************************
' How to use
' To get the default printer
'        debug.print   GetDefaultPrinter
' To set the default printer
'        debug.print SetDefaultPrinter("HP Laser JET")
'  above returns true if success.
' To get a list of printers suitable for a listbox, or combo
'        debug.print GetPrinters
'
' that is all there folks!
'
' Thus, when printing a report, you can:
'
'       1) save the default printer into a string
'              strCurrentPtr = GetDefaultPrinter
'       2) switch to your report printer
'              SetDefaultPrinter strReportsPtr
'       3) print report
'       4) switch back to the default printer
'              SetDefaultPrinter strCurrentPtr
'

Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A

' The following code allows one to read, and write to the WIN.INI files
' In win 2000 the printer settings are actually in the registry. However, windows
' handles this correctly
'
Private Declare Function GetProfileString Lib "kernel32" _
   Alias "GetProfileStringA" _
  (ByVal lpAppName As String, _
   ByVal lpKeyName As String, _
   ByVal lpDefault As String, _
   ByVal lpReturnedString As String, _
   ByVal nSize As Long) As Long

Private Declare Function WriteProfileString Lib "kernel32" _
   Alias "WriteProfileStringA" _
  (ByVal lpszSection As String, _
   ByVal lpszKeyName As String, _
   ByVal lpszString As String) As Long

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lparam As Any) As Long
   
         

Function SetDefaultPrinter(strPrinterName As String) As Boolean
   Dim strDeviceLine As String
   Dim strBuffer     As String
   Dim lngbuf        As Long
    
  ' get the full device string
  '
   strBuffer = Space(1024)
   lngbuf = GetProfileString("PrinterPorts", strPrinterName, "", strBuffer, Len(strBuffer))
  
  'Write out this new printer information in
  ' WIN.INI file for DEVICE item
  If lngbuf > 0 Then
     
     strDeviceLine = strPrinterName & "," & _
                     fstrDField(strBuffer, Chr(0), 1) & "," & _
                     fstrDField(strBuffer, Chr(0), 2)
                     
     Call WriteProfileString("windows", "Device", strDeviceLine)
     SetDefaultPrinter = True
     
     ' Below is optional, and should be done. It updates the existing windows
     ' so the "default" printer icon changes. If you don't do the below..then
     ' you will often see more than one printer as the default! The reason *not*
     ' to do the SendMessage is that many open applications will now sense the change
     ' in printer. I vote to leave it in..but your case you might not want this.
     '
     
     Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")
    
  Else
     SetDefaultPrinter = False
  End If
       
End Function

Function GetDefaultPrinter() As String

   Dim strDefault    As String
   Dim lngbuf        As Long

   strDefault = String(255, Chr(0))
   lngbuf = GetProfileString("Windows", "Device", "", strDefault, Len(strDefault))
   If lngbuf > 0 Then
      GetDefaultPrinter = fstrDField(strDefault, ",", 1)
   Else
      GetDefaultPrinter = ""
   End If

End Function


Function GetPrinters() As String
   
   ' this routine returns a list of printers, separated by
   ' a ";", and thus the results are suitable for stuffing into a combo box
   
   Dim strBuffer  As String
   Dim strOnePtr  As String
   Dim intPos     As Integer
   Dim lngChars   As Long
   
   strBuffer = Space(2048)
   lngChars = GetProfileString("PrinterPorts", vbNullString, "", strBuffer, Len(strBuffer))
   
   If lngChars > 0 Then
      intPos = InStr(strBuffer, Chr(0))
     Do While intPos > 1
        strOnePtr = Left(strBuffer, intPos - 1)
        strBuffer = Mid(strBuffer, intPos + 1)
        If GetPrinters <> "" Then GetPrinters = GetPrinters & ";"
        GetPrinters = GetPrinters & strOnePtr
        intPos = InStr(strBuffer, Chr(0))
     Loop
   Else
      GetPrinters = ""
   End If
   
 End Function

Public Function testPrintersGet()

   Debug.Print GetDefaultPrinter
   Debug.Print GetPrinters
   
   
End Function
[End Code]

Hope this helps...

AccessGuruCarl
 
Using the above Functions to populate
a combo box with available printers
This may be a better choice, Not all users
may want to print to the default printer.

Code:
Private Sub Form_Load()
' Add unbound combobox to form

   Me.Combo0.RowSource = GetPrinters
   Me.Combo0 = GetDefaultPrinter

End Sub

[End Code]


AccessGuruCarl
 
I've tried to use the code above from 'AccessGuruCarl' on both Access 97 and 2000.

I get the following error:

Compile Error: Sub or Function not defined.

and it then highlights - fstrDField


Can I use this in 97? If not, how can I modify it or find similiar code. I have look at many threads with noluck.

Thanks!

 
Wes1961

Here is the Function fstrDField, copy and paste into a module and everything should work...

Code:
Private Function fstrDField(mytext As String, delim As String, groupnum As Integer) As String

   ' this is a standard delimiter routine that every developer I know has.
   ' This routine has a million uses. This routine is great for splitting up
   ' data fields, or sending multiple parms to a openargs of a form
   '
   '  Parms are
   '        mytext   - a delimited string
   '        delim    - our delimiter (usually a , or / or a space)
   '        groupnum - which of the delimited values to return
   '
   
Dim startpos As Integer, endpos As Integer
Dim groupptr As Integer, chptr As Integer

chptr = 1
startpos = 0
 For groupptr = 1 To groupnum - 1
    chptr = InStr(chptr, mytext, delim)
    If chptr = 0 Then
       fstrDField = ""
       Exit Function
    Else
       chptr = chptr + 1
    End If
 Next groupptr
startpos = chptr
endpos = InStr(startpos + 1, mytext, delim)
If endpos = 0 Then
   endpos = Len(mytext) + 1
End If

fstrDField = Mid$(mytext, startpos, endpos - startpos)

End Function

[END CODE]



AccessGuruCarl
 
how would i set the default printer to specific printer that i have listed in a text box. the point of this is to force the user to print to our network printers only.
 
Dherrera,

Try something like this,

This code goes on your form with the textbox.
I set the Default Value for txtSetPrinter and it worked fine for me.

Make sure you include the full path and printer name.
Use the Function testPrintersGet() to make sure your using the correct path and printer name.

Example \\servername\printer
Code:
Option Compare Database
Option Explicit
Dim strUserDefaultPrinter As String

Private Sub Form_Load()
'Use OpenArg to display correct message
DoCmd.OpenForm "PrinterMSG", acNormal, , , , , "Network"
strUserDefaultPrinter = GetDefaultPrinter
SetDefaultPrinter (Me.txtSetPrinter.Value)
DoCmd.Close acForm, "PrinterMSG"

End Sub

Private Sub Form_Unload(Cancel As Integer)

DoCmd.OpenForm "PrinterMSG", acNormal, , , , , "User"
' Reset the users default printer.
SetDefaultPrinter (strUserDefaultPrinter)
DoCmd.Close acForm, "PrinterMSG"

End Sub

Now there will be a delay loading and unloading the form, while the printer is being changed.

So create a form, PrinterMSG that tells the user whats going on.
Set the properties on this form,
Caption -------- Setting Printer
Border Style --- Thin
Control Box ---- No
Nav Buttons ---- No
Etc...
Etc...
Place 2 labels on the form
Name them lblMSG and lblMSG_Close
Just hit the space bar to insert a space into each caption.
Re-Size them so that they are about 3 - 4 inches
Make lblMSG -- Bold Font - Size 10
Make lblMSG_Close -- Text Centered -- Font Color RED
Play around with the design looks, so it's User Friendly...

Use this code for the PrinterMSG form.
Code:
Option Compare Database
Option Explicit

Private Sub Form_Open(Cancel As Integer)
Dim intTimer As Integer
Me.lblMSG_Close.Visible = False
Me.lblMSG_Close.Caption = "Form will close in 5 seconds."
intTimer = 5000 '5 seconds
    If IsNull(Me.OpenArgs) Then
        Me.lblMSG.Caption = "This form is a message when setting printers"
        Me.lblMSG_Close.Visible = True
        Me.Form.TimerInterval = intTimer
    Else
        Select Case (Me.OpenArgs)
            Case "Network"
                Me.lblMSG.Caption = "Please wait, setting network printer."
            Case "User"
                Me.lblMSG.Caption = "Please wait, re-setting default printer."
            Case Else
                'Error- display msg, close form
                Me.lblMSG.Caption = "This form is a message when setting printers"
                Me.lblMSG_Close.Visible = True
                Me.Form.TimerInterval = intTimer
        End Select
    End If

End Sub

Private Sub Form_Timer()
DoCmd.Close acForm, Me.Name
End Sub

Make sure you add the Function fstrDField from above to your Printer Module that I forgot to post in the orginal post!

Hope this helps...


AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
i used the code but its not setting it to the correct printer. i keeps the current default printer instead of changing it to what is shown in txtSetPrinter.
 
Not sure what's going on.

Have you stepped through the code in debug mode, to make sure it's calling the procedure.

I've noticed it doesn't show up as a new default under XP Printer settings, but it does print to the SetDefault.

Have you tried printing?

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
yeah, i tried printing and it prints to my desktop printer instead of the network printer.
when i stepped through it i think i might have picked up where it going wrong. in the SetDefaultPrinter function the IF statement just automatically goes to the ELSE portion. i dont think that it is grabbing the printer name from the textbox.
 
Hello Dherrera,
in the SetDefaultPrinter function the IF statement just automatically goes to the ELSE portion

Your not sending a valid printer string.

Try This...
Code:
Private Sub Test_Click()

MsgBox GetDefaultPrinter
Debug.Print GetPrinters
'Returned
'   [My Printers]   [ 1st Network ] [ 2nd Network ]
'Fax;Adobe PDF;Canon;\\P-1\Lexmark;\\PC-01\Acrobat PDFWriter
'Printers are separated by semi-colons!
'Network Printers always begin with \\
'OR
MsgBox GetPrinters
'Fax;Adobe PDF;Canon;\\P-1\Lexmark;\\PC-01\Acrobat PDFWriter
'You must include the Full Path and Name...
'Example:  "\\servername\printername"

'Now set the Default Value for Text0  "\\P-1\Lexmark"

End Sub
This is your form...
Code:
Private Sub Form_Load()

'MsgBox GetDefaultPrinter
SetDefaultPrinter (Me.Text0)
'MsgBox GetDefaultPrinter


End Sub

If this doesn't work...
Paste the Return string from 'GetPrinters' so I can see what your passing to it.

I can't believe it won't work, My father and I have been using this same code in all kinds of environments...

Once you get it solved, check my earlier post about resetting the 'USERS' default printer. If this is a multi-user form, you'll defiantly want to reset the default printer.

Good Luck

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
thanks AGC,
it works perfect now. what i did was remove the if statement from the SetDefaultPrinter function and that solved the problem. i even added a combo box for remote users to be able to chose their local printer when ever they are not in the office.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top