Results 1 to 9 of 9

Thread: [02/03] Add a Printer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    [02/03] Add a Printer

    Hello guys, I'm trying to add a new printer (printers/faxes).
    I'm using the following:
    VB Code:
    1. Module Module1
    2.     Public Declare Auto Function OpenPrinter Lib "winspool.drv" _
    3.                             (ByVal pPrinterName As String, _
    4.                             ByRef phPrinter As IntPtr, _
    5.                             ByVal pDefault As PrinterDefaults) As Int32
    6.     Public Declare Function DeletePrinter Lib "winspool.drv" _
    7.                          (ByVal hPrinter As IntPtr) As Int32
    8.  
    9.     Public Declare Function ClosePrinter Lib "winspool.drv" _
    10.                 (ByVal hPrinter As IntPtr) As Int32
    11.     Private Declare Function AddPrinter Lib "winspool.drv" _
    12.        Alias "AddPrinterA" _
    13.       (ByVal pServerName As String, _
    14.        ByVal Level As Int32, _
    15.     ByVal pPrinter As PRINTER_INFO_2) As Int32
    16.     Structure PRINTER_INFO_2
    17.         Public pServerName As String
    18.         Public pPrinterName As String
    19.         Public pShareName As String
    20.         Public pPortName As String
    21.         Public pDriverName As String
    22.         Public pComment As String
    23.         Public pLocation As String
    24.         Public pDevMode As Int32  'DEVMODE
    25.         Public pSepFile As String
    26.         Public pPrintProcessor As String
    27.         Public pDatatype As String
    28.         Public pParameters As String
    29.         Public pSecurityDescriptor As Int32  'SECURITY_DESCRIPTOR
    30.         Public Attributes As Int32
    31.         Public Priority As Int32
    32.         Public DefaultPriority As Int32
    33.         Public StartTime As Int32
    34.         Public UntilTime As Int32
    35.         Public Status As Int32
    36.         Public cJobs As Int32
    37.         Public AveragePPM As Int32
    38.     End Structure
    39.  
    40.     Structure PrinterDefaults
    41.         Public pDataType As String
    42.         Public pDevMode As Int32
    43.         Public permissions As Int32
    44.     End Structure
    45.  
    46.     Public Const PRINTER_ACCESS_ADMINISTER As Int32 = &H4
    47.     Public Const STANDARD_RIGHTS_REQUIRED As Int32 = &HF0000
    48.     Public Const PRINTER_ACCESS_USE As Int32 = &H8
    49.     Public Const PRINTER_ALL_ACCESS As Int32 = _
    50.                       STANDARD_RIGHTS_REQUIRED Or _
    51.                       PRINTER_ACCESS_USE Or _
    52.                       PRINTER_ACCESS_ADMINISTER
    53.  
    54.     Sub RemovePrinter(ByVal PrinterName As String)
    55.         Dim hPrinter As IntPtr
    56.         Dim PrinterDefs As New PrinterDefaults
    57.         Dim rtn As Int32
    58.         PrinterDefs.permissions = PRINTER_ALL_ACCESS
    59.         rtn = OpenPrinter(PrinterName, hPrinter, PrinterDefs)
    60.         rtn = DeletePrinter(hPrinter)
    61.         rtn = ClosePrinter(hPrinter)
    62.     End Sub
    63.     Sub addprinter1(ByVal PrinterName As String)
    64.         Dim pi2 As PRINTER_INFO_2
    65.         With pi2
    66.             .pPrinterName = PrinterName
    67.             .pPortName = "RPT2:"
    68.             .pDriverName = "Apple LaserWriter 12/640 PS"
    69.             .pPrintProcessor = "WinPrint" '?
    70.         End With
    71.         Dim rtn As Int32
    72.         rtn = AddPrinter("", 2, pi2)
    73.     End Sub
    74.     Sub Main()
    75.         'addprinter1("test1")
    76.         RemovePrinter("test")
    77.     End Sub

    But nothing happens!
    What else must I do?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Add a Printer

    Is this a local printer or a network printer?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] Add a Printer

    Hello, Thanx for the reply!.
    it's just a Local Printer.
    It's one of those "default" printers listed in the list in Printers / faxes

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Add a Printer

    Well my code works for public network printers but maybe you can try it on your local printer?

    http://www.vbforums.com/showthread.php?t=308544
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] Add a Printer

    Hi!
    I've tried it like this:
    VB Code:
    1. Shell("Rundll32 printui.dll,PrintUIEntry /ga /c\\Grimm \'Lexmark 3200 Color Jetprinter'", vbNormalFocus)

    So, I didn't include the servername parameter in there.
    But, it said that the operation cannot be completed because the connection to the server was lost
    Have I done something wrong?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Add a Printer

    I take it that your printer name is "Grimm 'Lexmark 3200 Color Jetprinter'" with the single quotes?

    You need your local computer name or try local and see if that works.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] Add a Printer

    Quote Originally Posted by RobDog888
    I take it that your printer name is "Grimm 'Lexmark 3200 Color Jetprinter'" with the single quotes?

    You need your local computer name or try local and see if that works.
    The computer's name is Grimm, and the printer is Lexmark 3200 Color JetPrinter.
    So I've done something wrong?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Add a Printer

    Just a little bit.

    You need it in this format and see if it works.

    Dont forget to do the netstart and stop to refresh as explained in my comments in my codebank thread.

    VB Code:
    1. Shell "Rundll32 printui.dll,PrintUIEntry /ga /c\\Grimm/n\\Grimm\Lexmark 3200 Color JetPrinter", vbNormalFocus
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [02/03] Add a Printer

    Hello again!
    Thanx for all your effort

    OK, it doesn't throw any errors, but I see nothing in Printers / Faxes
    I have Stopped and started the spooler after I executed the shell command, any other problems?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width