Tuesday, April 26, 2011 12:42 PM
With the invaluable help of Google and Graham Mayor I wrote a little bit of Code in Word 2007 to print to pre-printed stationary which inlcudes changing the printer and the trays for the printing. This bit of code works really good in Word 2007 for my 30 users.
Sub PrintToStationery() ' select the photocopier (for now) as printer ' go to page settings, set first page to tray 3, other pages to tray 4 ' print document ' set page setup back to it was before change ' set printer back to the one used before ' sources - http://pubs.logicalexpressions.com/pub0009/lpmarticle.asp?id=101 ' sources - http://www.gmayor.com/fax_from_word.htm ' declare variables Dim sCurrentPrinter As String Dim oDoc As Document Dim iFirstPageTray As Integer Dim iOtherPagesTray As Integer ' save current settings Set oDoc = ActiveDocument sCurrentPrinter = ActivePrinter iFirstPageTray = oDoc.PageSetup.FirstPageTray iOtherPagesTray = oDoc.PageSetup.OtherPagesTray ' change printer On Error GoTo NoPrinter: ActivePrinter = \\server\ ' if printer changes name changes too On Error GoTo NoTrays: ' change trays With oDoc.PageSetup .FirstPageTray = wdPrinterLowerBin ' if printer changes this number changes too .OtherPagesTray = 257 ' if printer changes this number changes too End With ' print directly as we don't want user to make changes ' in Printer Dialogue oDoc.PrintOut ' reset settings ' change tray back to normal With oDoc.PageSetup .FirstPageTray = iFirstPageTray .OtherPagesTray = iOtherPagesTray End With ' change printer back to original ActivePrinter = sCurrentPrinter ExitSub: Exit Sub NoTrays: ' we already changed the printer, but we have problems setting the correct tray numbers ' so we have to set printer back to original one, then leave the function ' occurs with bill preview from MYOB MsgBox ("The trays for letterhead printing could not be changed. This letter can't be " & _ "printed on letterhead. Please save your work and restart Word. " & _ "If the error persists, please contact your System Administrator. Thank you!") ActivePrinter = sCurrentPrinter GoTo ExitSub NoPrinter: ' we are here because the Photocopier couldn't be found ' we didn 't do any changes so just leave this sub, but tell the user MsgBox ("The correct letterhead printer could not be located. This letter can't " & _ "printed on letterhead. Please contact your System Administrator. Thank you!") End Sub printer
The function saves the current printer and tray settings, changes to the needed values, and in the end sets everything back to normal.
Now I'm trying to roll out this code to Office 2010 on 64bit Win 7 SP1. The line
sCurrentPrinter = ActivePrinter
shows the current printer set to \\server\printer which is the printer I want. But the lines
' change printer On Error GoTo NoPrinter: ActivePrinter = \\server\printer ' if printer changes name changes toocome up with an error - printer could not be found, although I try to set the printer to exactly the name of the pinter as Word uses it the lines before. I can only set the printer if I write the whole printer name in lower case. And then, in the lines
' change printer back to original ActivePrinter = sCurrentPrinterwhen I try to set the printer back to the original, I get another erro - printer not found.My questions:1. Why does the printer name suddenly have to be lower case?2. Why can't I set the printer back to it's origina value?Funnily enough I have this problem only on the 64bit Win 7 installations. My work machine is a 32 Win7 and has no problems, and I also have no problems on WinXp 32 with Office 2010 - which means generally Office 2010 is working but not something is different with 64bit???Every help, comment and suggestion is welcome. Although development took some time, this bit of code helps to save a LOT of time.Thanks,KPS: Where is the preview button for the post?
Tuesday, April 26, 2011 4:47 PM
Are you certain it's not a print driver issue? Can you open a standard Word document and manually select the printer and it prints?
Different print drivers are required for 64-bit Win 7.
Wednesday, May 04, 2011 3:18 AM
sorry for the delay in answering, I was sick.
I can print to the printer fine if a go via the menu and the print button with the new backstage view. I can change printer around and print and change printer back and all works fine. So problem still is why the printer should a) be case sensetive and b) why can't I set it back to what I saved the printer for?
Monday, March 19, 2012 2:28 AM
Thanks for this thread. I have spent a week trying to get code that worked in my Access ADP application to work on Win 7 64 bit running Office 64 bit
It looks like there is some code somewhere that is testing to see if the printer exists - and this is case sensitive - but not the API call that sets or retrieves the printer names.
Tuesday, March 20, 2012 2:33 AM
in the end it was a case sensitivity issue in Office 2010 - suddenly printer names are case sensitive, and additionally differenent cases on different PC's. Also, the server unc isn't in there anymore.
Use the following function:
Function GetPrinter(strMyCasePrinter As String) As String 'this function will get all installed printer names on the pc 'then, the printername of the wanted printer and installed printer are compared in uppercase ' this will ensure that case sensetivity in office 2010 has no influence on finding correct printer ' source: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_26815440.html#a34878156 Dim WshNetwork As Object Dim Printers As Object Dim printer As Variant Set WshNetwork = CreateObject("WScript.Network") Set Printers = WshNetwork.EnumPrinterConnections GetPrinter = False For Each printer In Printers If UCase$(strMyCasePrinter) = UCase$(printer) Then GetPrinter = printer Exit For End If Next End Function
Change the following line:
' change printer On Error GoTo NoPrinter: ActivePrinter = \\server\printer ' if printer changes name changes too
' change printer On Error GoTo NoPrinter: 'new way with Office 2010 ' in Office 2010, printer name is suddenly case sensetiv ' the call of this new function will search for the right spelling of the printer name ActivePrinter = GetPrinter("printer") ' if printer ever changes, change name here
This in the end did the trick for me.
Additionally, to ensure full functionality, use the same driver and the same way to install the printer on all PC's. In my case, to get the correct trays working, use printer driver provided by manufacturer and NOT the windows included ones. Also, all PC's got the printer installed as local printer on IP port.
Tuesday, March 20, 2012 3:40 AM
Thanks for the continued interest Kerstin.
My situation is slightly different.
I am using a series of API calls to compile a list of local, remote and network printers.
These go into a combobox so that the user can select the destination printer.
I then create a Word Application object, add a document to it, and then set the active printer of the document to the one selected by the user.
This used to work perfectly, but now I get error 5216 "there is a printer error" because it can't find the printer that the API just told me was there.
I will try your code when I get the chance, and report back
Tuesday, March 20, 2012 3:44 AM
Dim WshNetwork As Object
Dim Printers As Object
Set WshNetwork = CreateObject("WScript.Network")
Set Printers = WshNetwork.EnumPrinterConnections
you get all installed printers - network, local, and even printers mapped via remote desktop (at least last time I checked). Try this instead of API calls to get the printer list to pout into selection field :)