vba to set duplex printing

jerry12302

Active Member
Joined
Apr 18, 2005
Messages
449
Office Version
  1. 2010
Platform
  1. Windows
I have spent hours searching for code that will work to set the printer to duplex printing before printing a job. I have narrowed it down to this code from a Microsoft site, but it crashes right away. "Printer" is highlighted, and the error says "variable not defined."


<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> SetPrinterToDuplex()<br>    SetPrinterDuplex Printer.DeviceName, 2<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br>   <SPAN style="color:#007F00">' ==================================================================</SPAN><br>   <SPAN style="color:#007F00">' SetPrinterDuplex</SPAN><br>   <SPAN style="color:#007F00">'</SPAN><br>   <SPAN style="color:#007F00">'  Programmatically set the Duplex flag for the specified printer</SPAN><br>   <SPAN style="color:#007F00">'  driver's default properties.</SPAN><br>   <SPAN style="color:#007F00">'</SPAN><br>   <SPAN style="color:#007F00">'  Returns: True on success, False on error. (An error will also</SPAN><br>   <SPAN style="color:#007F00">'  display a message box. This is done for informational value</SPAN><br>   <SPAN style="color:#007F00">'  only. You should modify the code to support better error</SPAN><br>   <SPAN style="color:#007F00">'  handling in your production application.)</SPAN><br>   <SPAN style="color:#007F00">'</SPAN><br>   <SPAN style="color:#007F00">'  Parameters:</SPAN><br>   <SPAN style="color:#007F00">'    sPrinterName - The name of the printer to be used.</SPAN><br>   <SPAN style="color:#007F00">'</SPAN><br>   <SPAN style="color:#007F00">'    nDuplexSetting - One of the following standard settings:</SPAN><br>   <SPAN style="color:#007F00">'       1 = None</SPAN><br>   <SPAN style="color:#007F00">'       2 = Duplex on long edge (book)</SPAN><br>   <SPAN style="color:#007F00">'       3 = Duplex on short edge (legal)</SPAN><br>   <SPAN style="color:#007F00">'</SPAN><br>   <SPAN style="color:#007F00">' ==================================================================</SPAN><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetPrinterDuplex(<SPAN style="color:#00007F">ByVal</SPAN> sPrinterName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _<br>      <SPAN style="color:#00007F">ByVal</SPAN> nDuplexSetting <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>      <SPAN style="color:#00007F">Dim</SPAN> hPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>      <SPAN style="color:#00007F">Dim</SPAN> pd <SPAN style="color:#00007F">As</SPAN> PRINTER_DEFAULTS<br>      <SPAN style="color:#00007F">Dim</SPAN> pinfo <SPAN style="color:#00007F">As</SPAN> PRINTER_INFO_2<br>      <SPAN style="color:#00007F">Dim</SPAN> dm <SPAN style="color:#00007F">As</SPAN> DEVMODE<br>      <SPAN style="color:#00007F">Dim</SPAN> yDevModeData() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN><br>      <SPAN style="color:#00007F">Dim</SPAN> yPInfoMemory() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN><br>      <SPAN style="color:#00007F">Dim</SPAN> nBytesNeeded <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>      <SPAN style="color:#00007F">Dim</SPAN> nRet <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, nJunk <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <br>      <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> cleanup<br>   <br>      <SPAN style="color:#00007F">If</SPAN> (nDuplexSetting < 1) <SPAN style="color:#00007F">Or</SPAN> (nDuplexSetting > 3) <SPAN style="color:#00007F">Then</SPAN><br>         MsgBox "Error: dwDuplexSetting is incorrect."<br>         <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>      <br>      pd.DesiredAccess = PRINTER_ALL_ACCESS<br>      nRet = OpenPrinter(sPrinterName, hPrinter, pd)<br>      <SPAN style="color:#00007F">If</SPAN> (nRet = 0) <SPAN style="color:#00007F">Or</SPAN> (hPrinter = 0) <SPAN style="color:#00007F">Then</SPAN><br>         <SPAN style="color:#00007F">If</SPAN> Err.LastDllError = 5 <SPAN style="color:#00007F">Then</SPAN><br>            MsgBox "Access denied -- See the article for more info."<br>         <SPAN style="color:#00007F">Else</SPAN><br>            MsgBox "Cannot open the printer specified " & _<br>              "(make sure the printer name is correct)."<br>         <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>         <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>   <br>      nRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)<br>      <SPAN style="color:#00007F">If</SPAN> (nRet < 0) <SPAN style="color:#00007F">Then</SPAN><br>         MsgBox "Cannot get the size of the DEVMODE structure."<br>         <SPAN style="color:#00007F">GoTo</SPAN> cleanup<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>   <br>      <SPAN style="color:#00007F">ReDim</SPAN> yDevModeData(nRet + 100) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN><br>      nRet = DocumentProperties(0, hPrinter, sPrinterName, _<br>                  VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)<br>      <SPAN style="color:#00007F">If</SPAN> (nRet < 0) <SPAN style="color:#00007F">Then</SPAN><br>         MsgBox "Cannot get the DEVMODE structure."<br>         <SPAN style="color:#00007F">GoTo</SPAN> cleanup<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>   <br>      <SPAN style="color:#00007F">Call</SPAN> CopyMemory(dm, yDevModeData(0), Len(dm))<br>   <br>      <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> <SPAN style="color:#00007F">CBool</SPAN>(dm.dmFields And DM_DUPLEX) <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox "You cannot modify the duplex flag for this printer " & _<br>               "because it does not support duplex or the driver " & _<br>               "does not support setting it from the Windows API."<br>         <SPAN style="color:#00007F">GoTo</SPAN> cleanup<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>   <br>      dm.dmDuplex = nDuplexSetting<br>      <SPAN style="color:#00007F">Call</SPAN> CopyMemory(yDevModeData(0), dm, Len(dm))<br>   <br>      nRet = DocumentProperties(0, hPrinter, sPrinterName, _<br>        VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _<br>        DM_IN_BUFFER <SPAN style="color:#00007F">Or</SPAN> DM_OUT_BUFFER)<br><br>      <SPAN style="color:#00007F">If</SPAN> (nRet < 0) <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox "Unable to set duplex setting to this printer."<br>        <SPAN style="color:#00007F">GoTo</SPAN> cleanup<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>   <br>      <SPAN style="color:#00007F">Call</SPAN> GetPrinter(hPrinter, 2, 0, 0, nBytesNeeded)<br>      <SPAN style="color:#00007F">If</SPAN> (nBytesNeeded = 0) <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> cleanup<br>   <br>      <SPAN style="color:#00007F">ReDim</SPAN> yPInfoMemory(nBytesNeeded + 100) <SPAN style="color:#00007F">As</SPAN> Byte<br><br>      nRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), nBytesNeeded, nJunk)<br>      <SPAN style="color:#00007F">If</SPAN> (nRet = 0) <SPAN style="color:#00007F">Then</SPAN><br>         MsgBox "Unable to get shared printer settings."<br>         <SPAN style="color:#00007F">GoTo</SPAN> cleanup<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>   <br>      <SPAN style="color:#00007F">Call</SPAN> CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))<br>      pinfo.pDevmode = VarPtr(yDevModeData(0))<br>      pinfo.pSecurityDescriptor = 0<br>      <SPAN style="color:#00007F">Call</SPAN> CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))<br>   <br>      nRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)<br>      <SPAN style="color:#00007F">If</SPAN> (nRet = 0) <SPAN style="color:#00007F">Then</SPAN><br>         MsgBox "Unable to set shared printer settings."<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>   <br>      SetPrinterDuplex = <SPAN style="color:#00007F">CBool</SPAN>(nRet)<br><br>cleanup:<br>      <SPAN style="color:#00007F">If</SPAN> (hPrinter <> 0) <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Call</SPAN> ClosePrinter(hPrinter)<br><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br></FONT>

This is all at the top of the module also...

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Type</SPAN> PRINTER_DEFAULTS<br>       pDatatype <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pDevmode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       DesiredAccess <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN><br><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Type</SPAN> PRINTER_INFO_2<br>       pServerName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pPrinterName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pShareName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pPortName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pDriverName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pComment <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pLocation <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pDevmode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>       <SPAN style="color:#007F00">' Pointer to DEVMODE</SPAN><br>       pSepFile <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pPrintProcessor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pDatatype <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pParameters <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       pSecurityDescriptor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>  <SPAN style="color:#007F00">' Pointer to SECURITY_DESCRIPTOR</SPAN><br>       Attributes <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       Priority <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       DefaultPriority <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       StartTime <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       UntilTime <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       Status <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       cJobs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       AveragePPM <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN><br><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Type</SPAN> DEVMODE<br>       dmDeviceName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> * 32<br>       dmSpecVersion <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmDriverVersion <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmSize <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmDriverExtra <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmFields <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmOrientation <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmPaperSize <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmPaperLength <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmPaperWidth <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmScale <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmCopies <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmDefaultSource <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmPrintQuality <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmColor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmDuplex <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmYResolution <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmTTOption <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmCollate <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmFormName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> * 32<br>       dmUnusedPadding <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmBitsPerPel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>       dmPelsWidth <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmPelsHeight <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmDisplayFlags <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmDisplayFrequency <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmICMMethod <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmICMIntent <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmMediaType <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmDither<SPAN style="color:#00007F">Type</SPAN> <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmReserved1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>       dmReserved2 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">End</SPAN> Type<br><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> DM_DUPLEX = &H1000&<br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> DM_IN_BUFFER = 8<br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> DM_OUT_BUFFER = 2<br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> PRINTER_ACCESS_ADMINISTER = &H4<br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> PRINTER_ACCESS_USE = &H8<br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> STANDARD_RIGHTS_REQUIRED = &HF0000<br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _<br>             PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)<br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> ClosePrinter <SPAN style="color:#00007F">Lib</SPAN> "winspool.drv" _<br>    (<SPAN style="color:#00007F">ByVal</SPAN> hPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> DocumentProperties <SPAN style="color:#00007F">Lib</SPAN> "winspool.drv" _<br>     Alias "DocumentPropertiesA" (<SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>     <SPAN style="color:#00007F">ByVal</SPAN> hPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> pDeviceName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _<br>     <SPAN style="color:#00007F">ByVal</SPAN> pDevModeOutput <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> pDevModeInput <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>     <SPAN style="color:#00007F">ByVal</SPAN> fMode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetPrinter <SPAN style="color:#00007F">Lib</SPAN> "winspool.drv" Alias _<br>     "GetPrinterA" (<SPAN style="color:#00007F">ByVal</SPAN> hPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Level <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>     pPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> cbBuf <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, pcbNeeded <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> OpenPrinter <SPAN style="color:#00007F">Lib</SPAN> "winspool.drv" Alias _<br>     "OpenPrinterA" (<SPAN style="color:#00007F">ByVal</SPAN> pPrinterName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, phPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>     pDefault <SPAN style="color:#00007F">As</SPAN> PRINTER_DEFAULTS) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetPrinter <SPAN style="color:#00007F">Lib</SPAN> "winspool.drv" Alias _<br>     "SetPrinterA" (<SPAN style="color:#00007F">ByVal</SPAN> hPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Level <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>     pPrinter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Command <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>   <SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CopyMemory <SPAN style="color:#00007F">Lib</SPAN> "kernel32" Alias "RtlMoveMemory" _<br>    (pDest <SPAN style="color:#00007F">As</SPAN> Any, pSource <SPAN style="color:#00007F">As</SPAN> Any, <SPAN style="color:#00007F">ByVal</SPAN> cbLength <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>)</FONT>

Any help would be appreciated, including other ways, if any, to set the printer to duplex. I have also tried sendkeys methods, but they have failed.

Thanks!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
The code is for Visual Basic not Visual Basic for Applications. You may still, however, get it to work. The problem (so far) is that VBA does not recognise the Printer object,however in the code this is only used to supply the name to the SetPrinterDuplex() function.

I have not gone through the other details of the Function in detail but it does seem to be based on API calls common to both applications. To get the printer name with VBA we use Application.ActivePrinter. The problem then is that the string content result is different to that required by the function.

For my printer I get :-
With VBA Application.ActivePrinter .... "Brother HL-1440 series on LPT1:"
With Visual Basic 6 Printer.DeviceName ... "Brother HL-1440 series"

I suggest that, to begin with, you hard code the printer name and see what happens - If anything - at the next step. So the code will be, for my printer :-
Code:
Sub SetPrinterToDuplex()
    SetPrinterDuplex "Brother HL-1440 series", 2
End Sub
 
Upvote 0
Thanks Brian,

I entered the printer name (which is on a network here) and now it doesn't crash anymore, but I get the error message "access denied", as shown in this part of the code:

nRet = OpenPrinter(sPrinterName, hPrinter, pd)
If (nRet = 0) Or (hPrinter = 0) Then
If Err.LastDllError = 5 Then
MsgBox "Access denied -- See the article for more info."

The article it refers to suggests that, when remote access to a network printer's properties is denied, you should add a local printer driver for a network printer and set the default to duplex, then just change the printer as necessary. Here's the article:

http://support.microsoft.com/kb/230743

However, this is for Windows NT or 2000, I have Windows XP.

I tried going through the steps anyway, but they're different, and I don't get an option to assign a different name to the printer, so it just recreates the same printer, no help.
 
Upvote 0
Actually the advice makes very good sense.
When using Add Printer you can call the printer anything you like. This is the name that shows in Start/Settings/Printers and appears in applications when you select file/print. Windows doesn't "know" what is actually on the end of the cable anyway.

My experience with XP or any other Windows version is that if we install the same printer twice we get something like PrinterName(2). We can then right click the name to get the Rename option. At this stage you, presumably, can also set the duplex option via .. Properties.
 
Upvote 0
Here's the problem, and maybe it's because the way the IT guys set up our network, but when I select Add a Printer I get the Printer Wizard and hit Next.

My options are to select a local printer, or a network printer. (The printer I print to is on the network, so I select Network Printer).

Then, I get 3 options, Find Printer, Connect to a Printer (type in the name in a field), and Connect to a Printer on the Internet...

I've tried both Find Printer and Connect to a Printer specifying a name. Either way, the only way I can add the printer is to select from a list of printers, even if I type in a name, it ignores it, I can only select my printer that's already in the list. When it recognizes a valid printer it asks me if I want it to be the default printer, I say no, and then the only option is to finish.

I have added printers before on other computers where I get printer(1), printer(2), etc. for the same printer, but on this network, it's not allowing that to happen.

I don't know that much about networks, if anyone knows a way around this, I would appreciate the advice.
 
Upvote 0
We can try another method. You mention that you tried SendKeys. This is fiddly to set up, but should be possible. There are a couple of tricks to this. One is the addition of Wait statements to delay code while things happen on screen, another is to add DoEvents lines to make sure the keystrokes happen. Long Wait is better than short to begin with. Can tweak things later on. It's simple really, but requires a lot of trial & Error.

First do the job manually to make notes of the keystrokes required. Then write the code including the Waits. Have a look at the message here that contains "MACRO TO WRITE EXTENDED FILE PROPERTIES INFO ..." and follow the message dialog to its final, successful, conclusion. I have done this sort of thing on numerous occasions and the result has always been satisfaction, despite the necessary slowness and occasional failure. Bear in mind that things connected to servers have very variable delays depending on the traffic at the time. Allow extra time for this.

http://www.mrexcel.com/forum/showthread.php?t=269104
 
Upvote 0
Generally we can expect to copy paste old code, and for it to run without problem because they make it backward compatible. Although they do drop old functions from time to time. I don't know about this Word code, but consider it should work.

Your do not state which version of MS Office you are using. You have not said what error messages you get, if any. You could try stepping the code in the VB Editor using the F8 key, which often gives better error messages.

I have used VBA since the beginning, and still use macros written for Office 2000 32 bit without problems in Office 2010 64 bit.

I am sorry that I cannot help further with this and suggest that you post a new message with more information. You will see that my suggested solution is to set up as if for a new, or different, printer and use that.
 
Upvote 0

Forum statistics

Threads
1,215,145
Messages
6,123,289
Members
449,094
Latest member
GoToLeep

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top