UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Selecting Default Printer, Access 2010    
 
   
DBaker7777
post Nov 9 2017, 03:10 PM
Post#1



Posts: 11
Joined: 10-July 13



Using Access 2010
Found this code for changing the default printer. When I run it, it does not seem to be setting the default printer based on the selection in the combobox. The code seems a bit outdated and was wondering if that is why it is not working. Thank you for any help


I have this in a module
CODE
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A

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
   strBuffer = Space(1024)
   lngbuf = GetProfileString("PrinterPorts", strPrinterName, "", strBuffer, Len(strBuffer))
  
  If lngbuf > 0 Then
     strDeviceLine = strPrinterName & "," & _
                     fstrDField(strBuffer, Chr(0), 1) & "," & _
                     fstrDField(strBuffer, Chr(0), 2)
     Call WriteProfileString("windows", "Device", strDeviceLine)
     SetDefaultPrinter = True
        
  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

   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


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

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
Go to the top of the page
 
ADezii
post Nov 10 2017, 10:08 AM
Post#2



Posts: 1,847
Joined: 4-February 07
From: USA, Florida, Delray Beach


Why not programmatically change the Default Printer at Run Time, from your Combo Box?
CODE
'To change the Default Printer
Set Application.Printer = Application.Printers.Item(Me.cboPrinters.Value)

'processing here, then switch back to another Printer
Go to the top of the page
 
DBaker7777
post Nov 10 2017, 10:52 AM
Post#3



Posts: 11
Joined: 10-July 13



On the form I have this:

CODE
Option Explicit
Dim gblMyDefaultPrinter as String

Private Sub Form_Load()
' Add unbound combobox to form and populate with available printers
   Me.Combo0.RowSource = GetPrinters
   Me.Combo0 = GetDefaultPrinter
'Set the users Default Printer
   gblMyDefaultPrinter = GetDefaultPrinter
End Sub

Private Sub Combo0_AfterUpdate()
   SetDefaultPrinter (Me.Combo0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Reset the printer back to the user's default.
SetDefaultPrinter (gblMyDefaultPrinter)
End Sub
Go to the top of the page
 
DBaker7777
post Nov 14 2017, 09:04 AM
Post#4



Posts: 11
Joined: 10-July 13



Ok I went with the

CODE
Set Application.Printer = Application.Printers.Item(Me.cboPrinters.Value)



The printer switches however now when it prints on my printer it is fine, when another user selects a printer to print on the formatting is all off and it comes out on 18 pages. Is there anyway to keep the formatting or not have it change once it prints no matter what PC it is on? It is a label printer (Dymo), but each user has a different Dymo printer, but the label size is the same.
Go to the top of the page
 
ADezii
post Nov 14 2017, 09:37 AM
Post#5



Posts: 1,847
Joined: 4-February 07
From: USA, Florida, Delray Beach


The specific Demo Printer would have to be selected on each PC and made the Default.
Go to the top of the page
 
DBaker7777
post Nov 14 2017, 11:15 AM
Post#6



Posts: 11
Joined: 10-July 13



It is selected through the Combobox, but instead of printing on 1 label as it does on my PC it breaks it up onto 18 labels when Dymo is selected on another PC.
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    22nd November 2017 - 11:27 AM