PDA

View Full Version : Solved: Coding A Specific Printer



GaryB
05-22-2006, 01:18 PM
In April I saw a post about coding in printing margins for a report. What I am curious about, is, can you also code a specific printer. I have a report that works fine on my computer, but, loses margins and the specific printer assigned when opened from another computer. Both are opening the report from the same database on a shared network. As always, any help is greatly appreciated.

Thanks

Gary

OBP
05-22-2006, 02:43 PM
Yes, try these 2.


Private Sub Command60_Click()
'These 2 routines list the printers on the system On Error GoTo Err_Command60_Click


Dim prtLoop As Printer
Dim prtFirst As Printer


Set prtFirst = Application.Printers(0) default printer

With prtFirst
MsgBox "Device name: " & .DeviceName & vbCr _ msgbox
with printer data & "Driver name: " & .DriverName & vbCr _
& "Port: " & .Port
End With


For Each prtLoop In Application.Printers
'A sort of for/next loop using the word "each" which represents
'database 'Objects not records With prtLoop a "with/end with"
'statement like an If/Then but uses database objects not records or' fields
MsgBox " Device name: " & .DeviceName & vbCr _
& "Driver name: " & .DriverName & vbCr _
& "Port: " & .Port
End With
Next prtLoop


Exit_Command60_Click:
Exit Sub


Err_Command60_Click:
MsgBox Err.Description
Resume Exit_Command60_Click


End Sub

Edited 2-Jun 2006 by geekgirlau. Reason: insert vba tags

GaryB
05-25-2006, 02:30 PM
Thanks for the response. Here's the code I tried for this using a command button to print. I messed this up - can you please advise me on this.

Thanks

Gary


Private Sub JOB_FOLDER_Click()
'These 2 routines list the printers on the system On Error GoTo Err_Command60_Click
Dim prtLoop As Printer
Dim prtFirst As Printer

Set prtFirst = Application.Printers(0) default printer
With prtFirst
MsgBox "Device name: " & .DeviceName & vbCr _ msgbox
with printer data & "Driver name: " & .DriverName & vbCr _
& "LPT1: " & .LPT1
End With

For Each prtLoop In Application.Printers
'A sort of for/next loop using the word "each" which represents database
'Objects not records With prtLoop a "with/end with"
'statement like an If/Then but uses database objects not records or
'fields
MsgBox " Epson Stylus Photo 1270: " & .Epson Stylus Photo 1270 & vbCr _
& "Epson Stylus Photo 1270: " & .Epson Stylus Photo 1270 & vbCr _
& "Lpt1: " & .Lpt1
End With
Next prtLoop

Exit_Command60_Click:
Exit Sub

Err_Command60_Click:
MsgBox Err.Description
Resume Exit_Command60_Click

End Sub

Edited 2-Jun 2006 by geekgirlau. Reason: insert vba tags

OBP
05-26-2006, 01:51 AM
Gary, I am sorry, that is my fault. The VB and Remarks got mixed up when I posted it.
This is just the doing that I used, it work OK on my stand alone computer/printer set up.

Private Sub Command60_Click()
On Error GoTo Err_Command60_Click
Dim prtLoop As Printer
Dim prtFirst As Printer
Set prtFirst = Application.Printers(0)
With prtFirst MsgBox "Device name
" & .DeviceName & vbCr _ & "Driver name: " & .DriverName & vbCr _ & "Port: " & .Port
End With
For Each prtLoop In Application.Printers
With prtLoop
MsgBox " Device name: " & .DeviceName & vbCr _ & _
"Driver name: " & .DriverName & vbCr _ & "Port: " & .Port
End With
Next prtLoop
Exit_Command60_Click:
Exit Sub
Err_Command60_Click:
MsgBox Err.Description
Resume Exit_Command60_Click
End Sub

Edited 2-Jun 2006 by geekgirlau. Reason: insert vba tags

GaryB
05-26-2006, 04:05 PM
Hi OBP,
When I load this code into my visual basic editor this part of the code

With prtFirst MsgBox "Device name
" & .DeviceName & vbCr _ & "Driver name: " & .DriverName & vbCr _ & "Port: " & .Port
turns red and a message box pops up saying:
Expected: end of statement

I would appreciate any insight you might have on this. I'll keep plugging away at this.

Thanks again,

Gary:banghead:
Edited 2-Jun 2006 by geekgirlau. Reason: insert vba tags

GaryB
05-26-2006, 04:10 PM
Here's what I have done so far. When it is set as and
Private Sub JOB_FOLDER_Click()
On Error GoTo Err_Command60_Click
Dim prtLoop As Printer
Dim prtFirst As Printer
Set prtFirst = Application.Printers(0)
With prtFirst MsgBox "epson stylus photo 1270
" & .epson stylus photo 1270 & vbCr _ & _
"epson stylus photo 1270: " & .epson stylus photo 1270 & vbCr _ & _
"Lpt1: " & .Lpt1
End With
For Each prtLoop In Application.Printers
With prtLoop
MsgBox " epson stylus photo 1270: " & .epson stylus photo 1270 & vbCr _ & _
"Driver name: " & .DriverName & vbCr _ & _
"Lpt1: " & .Lpt1
End With
Next prtLoop
Exit_Command60_Click:
Exit Sub
Err_Command60_Click:
MsgBox Err.Description
Resume Exit_Command60_Click
End Sub

Edited 2-Jun 2006 by geekgirlau. Reason: insert line breaks

GaryB
05-26-2006, 04:16 PM
Here's what I have done so far. It is set as an Event Procedure on a command button.

Private Sub JOB_FOLDER_Click()
On Error GoTo Err_Command60_Click
Dim prtLoop As Printer
Dim prtFirst As Printer
Set prtFirst = Application.Printers(0)
With prtFirst MsgBox "epson stylus photo 1270
" & .epson stylus photo 1270 & vbCr _ & _
"epson stylus photo 1270: " & .epson stylus photo 1270 & vbCr _ & _
"Lpt1: " & .Lpt1
End With
For Each prtLoop In Application.Printers
With prtLoop
MsgBox " epson stylus photo 1270: " & .epson stylus photo 1270 & vbCr _ & _
"Driver name: " & .DriverName & vbCr _ & "Lpt1: " & .Lpt1
End With
Next prtLoop
Exit_Command60_Click:
Exit Sub
Err_Command60_Click:
MsgBox Err.Description
Resume Exit_Command60_Click
End Sub

When I click on the button, prtLoop As Printer is highlighted and the message box reads " user defined type not defined". Now I've really gotten myself in a pickle. Ha!

thanks

Gary
Edited 2-Jun 2006 by geekgirlau. Reason: insert line breaks

OBP
05-27-2006, 02:35 AM
Gary, sorry, I missed a concatenated line and the "_" shouldn't be in the lines when it is all on one line. That is the problem with not copying it out of the VB Editor, it was in an Excel sheet where I store old code. It should read as below



Private Sub Command6_Click()

On Error GoTo Err_Command6_Click
Dim prtLoop As Printer
Dim prtFirst As Printer
Set prtFirst = Application.Printers(0)
With prtFirst
MsgBox "Device name" & .DeviceName & vbCr & _
"Driver name: " & .DriverName & vbCr & "Port: " & .Port
End With
For Each prtLoop In Application.Printers
With prtLoop
MsgBox " Device name: " & .DeviceName & vbCr & _
"Driver name: " & .DriverName & vbCr & "Port: " & .Port
End With
Next prtLoop
Exit_Command6_Click:
Exit Sub
Err_Command6_Click:
MsgBox Err.Description
Resume Exit_Command6_Click

End Sub

I have just checked it and it works OK for me.
You don't put the names of the printers in the msgbox the ".DeviceName" does that.

Edited 2-Jun 2006 by geekgirlau. Reason: insert vba tags

boneKrusher
05-27-2006, 04:32 PM
Here is a code that loads all your printers into a combo Box:

Dim prt As Printer

For Each prt In Application.printers
Me!combo2.AddItem Item:=prt.DeviceName
Next prt

Then this command will print to the selected printer:


Dim stDocName As String
Application.Printer = Application.printers(Me!combo2.Value)
stDocName = "Report1"
DoCmd.OpenReport stDocName, acNormal

OBP
05-28-2006, 02:42 AM
boneKrusher, that is a nice piece of code, thanks for the help.

boneKrusher
05-29-2006, 05:15 AM
You're welcome. I use it on all my forms. Access wants to print to your default printer, this is a better solution.

Regards,

GaryB
05-30-2006, 07:48 AM
Hi BoneKrusher,

So, do I create a combo box and attached the code to it? Also, the report I am printing, will always print to a certain printer. What is happening is when other users print from their work stations the specific printer with it's settings and margins are getting lost even though I saved the report with a a spcific printer. Can your code be set for detailing that one printer?

Thanks

Gary

boneKrusher
05-30-2006, 03:23 PM
Gary,

Yes, attach the first part of the code to your combo box. In the code I posted, combo2 is the name of the combo box. The code:


For Each prt In Application.printers
Me!combo2.AddItem Item:=prt.DeviceName
Next prt loads the printers that are for the current users. So its unique to each users.

As for detailing that one printer, I am not sure.

Sorry

GaryB
05-30-2006, 04:12 PM
BoneKrusher,
This is the code I attached to a combo box. I set it as event proceedure in on enter. I get an error message " compile error" method or data not found. I'm doing something way wrong here. Any ideas?

Thanks
Gary
Private Sub Combo448_Enter()
For Each prt In Application.printers
Me!Combo448.AddItem Item:=prt.DeviceName
Next prt
End Sub

GaryB
05-30-2006, 04:29 PM
I found the problem with method or data message. Printers is not listed. Would it help to know I am using Access 2000?

Thanks again and again,

Gary

boneKrusher
05-30-2006, 06:17 PM
You did not declare the PRT as a printer

In the properties for the combo box, make sure that the Row Source Type is set to "Value List"


Dim prt as printer
For Each prt In Application.printers
Me!Combo448.AddItem Item:=prt.DeviceName
Next prt

also change the event to ON OPEN for the form.

if that doesnt work, post your DB for me to look at.

Good luck
Regards,

GaryB
05-31-2006, 08:51 AM
I created a combo box, double checked the source type is "Value List" set the form on open and copied the code and placed it as follows:
Private Sub Form_Open(Cancel As Integer)
Dim prt As printer
For Each prt In Application.printers
Me!Combo448.AddItem Item:=prt.DeviceName
Next prt
End Sub

Still get an error message "compile error" " user-defined type not defined" in VB Editor. I can't post the DB it's over 50 MB. Sorry to be a pain.
Thanks,

Gary

GaryB
05-31-2006, 09:02 AM
I think I see the problem. In VB Editor, when I type Dim Prt as, a list box pops up and Printer is not on the list. So that's why I'm getting the compile error. Looks like I'm limited on what I can and can't do.

Thanks for trying,

Gary

mdmackillop
05-31-2006, 12:21 PM
Hi Gary,
I've simplified the previous listing code as follows. BTW, which version of Access are you using?
Regards
MD


Private Sub Command60_Click()
Dim Prt As Printer

msg = Application.Printers.Count & " printers found." & vbCr & vbCr
For i = 0 To Application.Printers.Count - 1
Set Prt = Application.Printers(i)
With Prt
msg = msg & .DeviceName & ". Driver name: " & .DriverName & _
". Port: " & .Port & vbCr
End With
Next
MsgBox msg
End Sub

GaryB
05-31-2006, 12:29 PM
Hi MD,
I'm using Access 2000. The problem I am encountering is the
Dim Prt As Printer
won't work. I get an error message - please review the post right above yours. I have a feeling this is a limitation in Acess 2000.

Thanks

Gary

mdmackillop
05-31-2006, 12:34 PM
I do have some code for selecting and changing the active printer running in my office on 2000. It's hellish complicated, reading/changing all sorts of registry settings, and I can't remember where I pinched it from. I'll fetch it tomorrow, if I can't find it on the web.

mdmackillop
05-31-2006, 12:53 PM
Something similar here
http://www.members.shaw.ca/AlbertKallal/msaccess/printch2k.zip

GaryB
05-31-2006, 01:06 PM
MD,

That is a very cool piece of work and I do have a use for it, however, it doesn't address the problem I initially started with. What I need to do is set code for a report to print to preview and then print to a specific printer with specific margins. You can save a report with all of this, but, every now and again the report loses it's settings. What is odd about it is on my computer it will work fine on someone else computer the settings are gone and I have to take the db off-line, re-save the report and start it back up again. I'm not sure if I'm getting stating what my problem is clearly?

Thanks,

Gary

mdmackillop
05-31-2006, 01:15 PM
Hi Gary,
I don't think I can help with the second bit. I'd try keeping a "spare" copy of the report. I wonder if they would both lose your settings.
Regards
MD

GaryB
05-31-2006, 01:28 PM
Thanks for the try. And thank you to everyone else who responded . I'm going to close the book on this one. If I do come up with something I will post it.

Thanks again,

Gary

boneKrusher
06-01-2006, 07:47 AM
Gary,

I've attached a sample in Access 2000. See if it works.

Bones

GaryB
06-01-2006, 12:20 PM
hmmmm,
Tried it and still had the same problem with printer not being a method. This worked for you in access 2000? If so, I have a really goofy problem going on.

Gary

boneKrusher
06-01-2006, 01:15 PM
Hmmm do you have the lastest service Jet pack 4.0 installed? Maybe its that.

GaryB
06-01-2006, 04:17 PM
Here's how it breaks down:
vers 9.0.2720
vba vers 6.0
Not sure on the Jet Pack Version.

Gary

geekgirlau
06-01-2006, 08:30 PM
By the way gentlemen, don't forget to use the VBA tags when posting code, and insert line breaks in the code - makes it a lot easier for all to read! :reading:

boneKrusher
06-03-2006, 06:08 AM
Gary,

All I can think is try to get the lasted updates for office. Maybe upgrade to access 2003...

Sorry

GaryB
06-05-2006, 06:30 AM
Not a problem, thanks for all the help


Gary

boneKrusher
06-10-2006, 05:58 AM
Gary,

I did some research. To answer your original question, how to set the default of the printer, I found this code:


Dim ChangePrinter As Object

Set ChangePrinter = CreateObject("WScript.Network")
ChangePrinter.SetDefaultPrinter "Adobe PDF"
Set ChangePrinter = Nothing

GaryB
06-13-2006, 09:35 AM
Hi BoneKrusher,

This did it. Since I preview the report before printing, I added the code, as an event proceedure to - on activate- and when the report opens for preview, it changes the default printer to the one I named in the code. It seems logical that if I use the same code and change the printer to the normal printer I usually have set as default I can add it as an event proceedure to - on close - and have it automatically reset my computer back to the original defaut. Just did it and it worked perfect On close it reset the default printer. Thanks for the determination and a whole lot of effort. I will mark this solved.

Thanks again
Gary