Set Default Printer by Visual basic Code 
Author Message
 Set Default Printer by Visual basic Code

How could we change the default printer in windows by Visual Basic code?
Thanks


Sat, 20 Jul 2002 03:00:00 GMT  
 Set Default Printer by Visual basic Code
Try this

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

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

Sub SetDefaultPrinter(strPrinterName As String

    Dim strBuffer As String
    Dim i As Long
    Dim strPrinter() As String

    strBuffer = Space(1024)
    i = GetProfileString("Devices", strPrinterName, "", strBuffer,
Len(strBuffer))
    strPrinter = Split(strBuffer, ",", -1, vbTextCompare)
    i = WriteProfileString("windows", "Device", strPrinterName & "," &
strPrinter(0) & "," & strPrinter(1))
    i = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows

End Sub

Function GetDefaultPrinter() As String
    Dim strBuffer As String
    Dim i As Long
    Dim strPrinter() As String
    strBuffer = Space(1024)
    i = GetProfileString("windows", "Device", "", strBuffer, Len(strBuffer))
    strPrinter = Split(strBuffer, ",", -1, vbTextCompare)
    GetDefaultPrinter = strPrinter(0)
End Function

Tony Edgecombe


Quote:
> How could we change the default printer in windows by visual basic code?
> Thanks



Sat, 20 Jul 2002 03:00:00 GMT  
 Set Default Printer by Visual basic Code
try this :

    dim def_printer as string
    dim pr as printer
    def_printer = "Epson..."
    for each pr in printers
        if pr.devicename = def_printer then
            set printer = pr
        end if
    next


Quote:
> How could we change the default printer in windows by visual basic code?
> Thanks



Mon, 22 Jul 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Set Default Printer by Visual basic Code

2. Setting the System Default Printer in Visual Basic

3. Setting the default printer from Visual Basic

4. visual basic code for default printer

5. Code to set default printer

6. How to set default printer in VBA code??

7. How to set default printer in VBA code?

8. Set default printer by code

9. setting/changing default printer via code (VBA)

10. Set default printer by code

11. Setting the default printer in code

12. Get/Set Default printer - Looking for code samples or Tips

 

 
Powered by phpBB® Forum Software