PDA

View Full Version : use SendKeys to print to different printer



daniels012
02-14-2008, 09:04 AM
[[USING Office 2000]]

Is there a way to print one of my reports to a different printer using SendKeys?

I have used send keys in Excel before, but I have no idea how to do this in Access.

I know in 2000 I have to change the default printer, then print the report, then change back the default printer. I have looked up ways of doing this creating new forms, and opening and closing them, and all different workarounds. I just thought if there was a way to use send keys, it may be easier.

Any suggestions are greatly appreciated!!
Michael:bug:

daniels012
02-15-2008, 09:27 AM
I use this in Excel:
Application.Dialogs(xlDialogPrinterSetup).Show

Is there something in Access like this?
Michael

rconverse
02-15-2008, 11:00 AM
I do not think Access 2000 has that capability. I had been working with Access 2003 for a few months before I realized that Access 2003 does have this capability.

Edit: When I say capability, I mean the ability to display dialogs. There may be another way in VB to change the default printer and then change it back.

Carl A
02-18-2008, 07:36 AM
I use this in Excel:
Application.Dialogs(xlDialogPrinterSetup).Show

Is there something in Access like this?
Michael

I use a combobox to retrieve my printers. Here is an example on this page download a replacement for Chapter10 (Access 97 Developer's Handbook)

http://www.mcwtech.com/downloads.aspx

HTH

Oorang
02-18-2008, 04:50 PM
Actually printers are pretty easy in Access. To change the active printer you just set Application.Printer. The trick is that the Application.Printer property does not accept a string property, it accepts a Printer Object.

Well how do you get a printer object? The easiest way (imo) is to just specify on of the printers in the Access.Printers collection. You can either specify it with an Index Number:
Access.Application.Printer = Access.Printers(1)Or the device name:
Access.Application.Printer = Access.Printers("\\PRINT1\WIKON867")

daniels012
02-19-2008, 07:21 AM
Aaron,
How/Where would I set the report to print on this printer then switch back to the default printer?

Michael

Oorang
02-20-2008, 11:03 AM
Personally I would make the button that prints the report swap it over and back like this:
Option Explicit

Private Sub btnPrintReport_Click()
On Error GoTo Err_Hnd
Const lngUserCancelled_c As Long = 2501
Const strReportName_c As String = "deleteme"
Const strPrinterName_c As String = "Microsoft XPS Document Writer"
Dim strOrgPrinter As String
Access.DoCmd.Hourglass True
strOrgPrinter = Access.Application.Printer.DeviceName
Access.Application.Printer = Access.Application.Printers(strPrinterName_c)
Access.DoCmd.OpenReport strReportName_c, acNormal
Exit_Proc:
On Error Resume Next
Access.Application.Printer = Access.Application.Printers(strOrgPrinter)
Access.DoCmd.Hourglass False
Exit Sub
Err_Hnd:
If Err.Number <> lngUserCancelled_c Then
MsgBox Err.Description
End If
Resume Exit_Proc
End Sub

daniels012
02-20-2008, 01:13 PM
Ok,
I am going to attempt this. The only problem I have is I have a macro in the spot of the On Click??
Can I put your code elsewhere?

Michael

Qubit
02-20-2008, 03:06 PM
I try not to use SendKeys, as it tends to cause probs.

Check this out (http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnaraccessdev/html/ODC_MicrosoftOfficeDeveloperForumControllingYourPrinterinMicrosoftAccess.as p):

and this:
http://support.microsoft.com/kb/208840


Basically, you create a dummy report with the printer destination set correctly - do one for each printer you'll be switching from/to.

Then when you want to redirect, you grab the settings from the correct report and setting the prtDevNames and prtDevMode properties. See above links.

All in all, it's a pretty messy business. Might be easier to clone the report, change the printer, then open whichever one you need.

.q


Edit: Emedded over-long URL to prevent side scrolling ~Oorang

Oorang
02-20-2008, 04:18 PM
Ok,
I am going to attempt this. The only problem I have is I have a macro in the spot of the On Click??
Can I put your code elsewhere?
Michael
I'm not sure if I understand your question. You can make any button do what you want it to. If you want to preserve the function of the macro then of course add another button that does this function. I think however you are asking me how to hook the code to the button instead of the macro. In that case you would want to go to the button properties (right click on the button and click properties), then select the events tab. Finally select the onclick field. Notice that on the right you will have a dropdown button and an ellipsis (three dots or "...") click the ellipsis ("...") and select "Code Builder" when prompted. The name of the sub has to be whatever the code builder generated. Just copy/paste the code I gave you into that sub. (Obviously omit my sub name.)

daniels012
02-21-2008, 07:08 AM
Aaron,
I am sorry, I am not very clear when I explain things.
Actually, I have a Macro for the OnClick of this button.
I wanted to add your code and have the macro.
I would convert the Macro to code but I am affraid I
don't know the code to do that! I tried to print out the macro
which in turn gave me 3 pages. It would be a lot of typing for me to
retype all of it.

Does this make more sense? i appreciate your patience, I am trying
to learn as much as I can.

Michael

Oorang
02-22-2008, 01:30 PM
It's actually not too painful to convert a macro to code. Just right click on the macro and select "Save As", change the drop down menu from Macro to Module, and it will save a code version of your macro. If you want you can post that code back here and we might be able to help you blend the functions together.

daniels012
02-22-2008, 01:49 PM
This won't convert all my macros to modules? Will It?

Michael

Oorang
02-25-2008, 11:44 AM
lol Well you could have just tried it on a copy of the db. But yes it will convert everything. But it leaves the original intact. You won't loose anything. It just makes a code version of the original without altering the original.

daniels012
02-25-2008, 12:08 PM
This is the converted macro to code:


Option Compare Database
'------------------------------------------------------------
' PrintRequestReportMacro
'
'------------------------------------------------------------
Function PrintRequestReportMacro()
On Error GoTo PrintRequestReportMacro_Err
DoCmd.Echo False, ""
DoCmd.GoToRecord acForm, "FrmRequestForWO", acNext
DoCmd.GoToRecord acForm, "FrmRequestForWO", acPrevious
DoCmd.OpenReport "RequestforWORpt", acPreview, "", "[RequestWOQry]![RequestID]=[Forms]![FrmRequestForWO]![RequestID]"
If (Forms!FrmRequestForWO!NoChargeWO = True) Then
Reports!RequestforWORpt!NoChargeWO.Visible = True
End If
If (Forms!FrmRequestForWO!NoChargeWO = True) Then
Reports!RequestforWORpt!RequestNoChargeLabel.Visible = True
End If
If (Forms!FrmRequestForWO!NoChargeWO = False) Then
Beep
MsgBox "Put a green sheetof paper in the printer then hit OK", vbInformation, ""
End If
If (Forms!FrmRequestForWO!NoChargeWO = True) Then
Beep
MsgBox "Put a Purple sheet of paper in the printer then hit OK", vbInformation, ""
End If
DoCmd.PrintOut acPrintAll, 1, 1, acHigh, 1, True
DoCmd.Close acReport, "RequestforWORpt"
DoCmd.GoToRecord acForm, "FrmRequestForWO", acNewRec
Exit Function

PrintRequestReportMacro_Exit:
Exit Function
PrintRequestReportMacro_Err:
MsgBox Error$
Resume PrintRequestReportMacro_Exit
End Function



Michael

daniels012
03-26-2008, 06:58 AM
Aaron,
any ideas on adding your code to mine?
Michael

Trevor
03-26-2008, 09:21 PM
Danials012, if you simply want to know how to use send keys for access

Sendkeys "{"A"}{Tab}" ' will send A+Tab to the current window
'if you want to do somthing like Alt + m then
SendKeys "%m" ' if you want to send a space
SendKeys "{ }"
% = Alt
^ = Ctrl
+ + shift

daniels012
04-18-2008, 07:12 AM
Any help with adding your code to mine Aaron?

Michael

daniels012
04-23-2008, 07:15 AM
OOrang,
Is there a way to combine your code with mine?

Oorang
04-23-2008, 08:07 AM
Sorry for the delay, try this:
Function PrintRequestReportMacro()
Const strPrinterName_c As String = "Microsoft XPS Document Writer"
Const strRptName_c As String = "RequestforWORpt"
On Error GoTo PrintRequestReportMacro_Err
Dim rpt As Access.Report
Dim chkNoCharge As Access.CheckBox
Dim lblNoCharge As Access.Label
Dim blnNoCharge As Boolean
Dim strOrgPrinter As String
DoCmd.Echo False, vbNullString
'Why do this at all?
'DoCmd.GoToRecord acForm, "FrmRequestForWO", acNext
'DoCmd.GoToRecord acForm, "FrmRequestForWO", acPrevious
DoCmd.OpenReport strRptName_c, acPreview, vbNullString, _
"[RequestWOQry]![RequestID]=[Forms]![FrmRequestForWO]![RequestID]"
Set rpt = Access.Reports(strRptName_c)
Set chkNoCharge = rpt.Controls("NoChargeWO")
Set lblNoCharge = rpt.Controls("RequestNoChargeLabel")
blnNoCharge = Nz(chkNoCharge.Value, False)
chkNoCharge.Visible = blnNoCharge
lblNoCharge.Visible = blnNoCharge
If blnNoCharge Then
If MsgBox("Put a Purple sheet of paper in the printer then hit OK", vbInformation + vbOKCancel) = vbCancel Then
Err.Raise vbObjectError, , "Cancelled"
End If
Else
If MsgBox("Put a green sheetof paper in the printer then hit OK", vbInformation + vbOKCancel) = vbCancel Then
Err.Raise vbObjectError, , "Cancelled"
End If
End If
strOrgPrinter = rpt.Printer.DeviceName
rpt.Printer = Access.Printers(strPrinterName_c)
DoCmd.PrintOut acPrintAll, 1, 1, acHigh, 1, True
PrintRequestReportMacro_Exit:
On Error Resume Next
If LenB(strOrgPrinter) Then
rpt.Printer = Access.Printers(strOrgPrinter)
End If
DoCmd.Close acReport, "RequestforWORpt"
DoCmd.Echo True, vbNullString
DoCmd.Hourglass False
Set rpt = Nothing
Set chkNoCharge = Nothing
Set lblNoCharge = Nothing
Exit Function
PrintRequestReportMacro_Err:
MsgBox Error$
Resume PrintRequestReportMacro_Exit
End Function

daniels012
04-23-2008, 11:28 AM
Also, How would I add this Function to my button's_Click()?

Michael

CreganTur
04-23-2008, 11:49 AM
Here you go :) :


Open your form in design view
select the button you want the function assigned to
on your properties sheet look for the On Click Event (under Events tab)
Click the build button (looks like "...")
Select Code window if askedHere you'll see the code skeleton for:
Private Sub ButtonName_Click()
End Sub

Just sandwich your procedure between this skeleton and you'll have a delicious function activated by the Click event on the chosen button.

HTH:hi:

daniels012
04-23-2008, 11:56 AM
Like:

Private Sub ButtonName_Click()
PrintRequestReportMacro
End Sub

Or do I enter all of the Function in the middle?

Michael

Oorang
04-23-2008, 12:22 PM
Either will work. But if you go with the second method, only paste the code. Don't paste the sub/end sub lines it will throw an error:)

daniels012
04-28-2008, 06:19 AM
Aaron,
I get a debug:
"Compile error
Method or Data member not found."
Is this because I am using Access 2000?
Michael

Oorang
04-28-2008, 10:43 AM
What did it highlight/select when it gave that error?

daniels012
04-28-2008, 11:59 AM
I said it debugged, but actually I get the error message and then it. just freezes the screen.

Michael

Oorang
04-28-2008, 06:05 PM
If you go to the Debug Menu and select "Compile" it should take you to the problem line. If it compiles without an error, remark out your error handlers and all the "Echo" statements. Then run the code and when you get the error click "Debug" and it should drop you right to the line.

daniels012
04-29-2008, 10:35 AM
This line:
rpt.Printer = Access.Printers(strPrinterName_c)

It highlights Printers.

Michael

Oorang
04-29-2008, 01:53 PM
I was afraid of that. I did a little digging and it turns out the "Printers" object didn't come around until 2003 (I went from 97 to 2003, so I skipped 2000). Really sorry about that. I did a little more research and I found this: http://www.mvps.org/access/reports/rpt0009.htm that should help you. See if you can work it out, if not I will have to reinstall my version of 97 and we'll see what I can come up with.