Duplex printing with macros - Google Groups

文章推薦指數: 80 %
投票人數:10人

I recorded a macro, and part of that macro is supposed to duplex my document when i print it. However, it does not. I got some code from Microsoft's website ... Groupsmicrosoft.public.word.vba.generalConversationsAboutDuplexprintingwithmacros941viewsSkiptofirstunreadmessageKeithunread,Jul13,2004,12:42:08AM7/13/04ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoIrecordedamacro,andpartofthatmacroissupposedtoduplexmydocumentwheniprintit.However,itdoesnot.IgotsomecodefromMicrosoft'swebsitetohelpmewiththis,butthereissomethingwrongwithitsomewhere.Ibelivethattheproblemexistswhenitasksmehowiwanttoduplexit.TheonlyproblemisIdontknowwheretoputtheoptioninat.Iwillpastthecodebelow.anyhelpwouldbegreatful!OptionExplicitPublicTypePRINTER_DEFAULTSpDatatypeAsLongpDevmodeAsLongDesiredAccessAsLongEndTypePublicTypePRINTER_INFO_2pServerNameAsLongpPrinterNameAsLongpShareNameAsLongpPortNameAsLongpDriverNameAsLongpCommentAsLongpLocationAsLongpDevmodeAsLong'PointertoDEVMODEpSepFileAsLongpPrintProcessorAsLongpDatatypeAsLongpParametersAsLongpSecurityDescriptorAsLong'PointertoSECURITY_DESCRIPTORAttributesAsLongPriorityAsLongDefaultPriorityAsLongStartTimeAsLongUntilTimeAsLongStatusAsLongcJobsAsLongAveragePPMAsLongEndTypePublicTypeDEVMODEdmDeviceNameAsString*32dmSpecVersionAsIntegerdmDriverVersionAsIntegerdmSizeAsIntegerdmDriverExtraAsIntegerdmFieldsAsLongdmOrientationAsIntegerdmPaperSizeAsIntegerdmPaperLengthAsIntegerdmPaperWidthAsIntegerdmScaleAsIntegerdmCopiesAsIntegerdmDefaultSourceAsIntegerdmPrintQualityAsIntegerdmColorAsIntegerdmDuplexAsIntegerdmYResolutionAsIntegerdmTTOptionAsIntegerdmCollateAsIntegerdmFormNameAsString*32dmUnusedPaddingAsIntegerdmBitsPerPelAsIntegerdmPelsWidthAsLongdmPelsHeightAsLongdmDisplayFlagsAsLongdmDisplayFrequencyAsLongdmICMMethodAsLongdmICMIntentAsLongdmMediaTypeAsLongdmDitherTypeAsLongdmReserved1AsLongdmReserved2AsLongEndTypePublicConstDM_DUPLEX=&H1000&PublicConstDM_IN_BUFFER=8PublicConstDM_OUT_BUFFER=2PublicConstPRINTER_ACCESS_ADMINISTER=&H4PublicConstPRINTER_ACCESS_USE=&H8PublicConstSTANDARD_RIGHTS_REQUIRED=&HF0000PublicConstPRINTER_ALL_ACCESS=(STANDARD_RIGHTS_REQUIREDOr_PRINTER_ACCESS_ADMINISTEROrPRINTER_ACCESS_USE)PublicDeclareFunctionClosePrinterLib"winspool.drv"_(ByValhPrinterAsLong)AsLongPublicDeclareFunctionDocumentPropertiesLib"winspool.drv"_Alias"DocumentPropertiesA"(ByValhwndAsLong,_ByValhPrinterAsLong,ByValpDeviceNameAsString,_ByValpDevModeOutputAsLong,ByValpDevModeInputAsLong,_ByValfModeAsLong)AsLongPublicDeclareFunctionGetPrinterLib"winspool.drv"Alias_"GetPrinterA"(ByValhPrinterAsLong,ByValLevelAsLong,_pPrinterAsByte,ByValcbBufAsLong,pcbNeededAsLong)AsLongPublicDeclareFunctionOpenPrinterLib"winspool.drv"Alias_"OpenPrinterA"(ByValpPrinterNameAsString,phPrinterAsLong,_pDefaultAsPRINTER_DEFAULTS)AsLongPublicDeclareFunctionSetPrinterLib"winspool.drv"Alias_"SetPrinterA"(ByValhPrinterAsLong,ByValLevelAsLong,_pPrinterAsByte,ByValCommandAsLong)AsLongPublicDeclareSubCopyMemoryLib"kernel32"Alias"RtlMoveMemory"_(pDestAsAny,pSourceAsAny,ByValcbLengthAsLong)'=================================================================='SetPrinterDuplex''ProgrammaticallysettheDuplexflagforthespecifiedprinter'driver'sdefaultproperties.''Returns:Trueonsuccess,Falseonerror.(Anerrorwillalso'displayamessagebox.Thisisdoneforinformationalvalue'only.Youshouldmodifythecodetosupportbettererror'handlinginyourproductionapplication.)''Parameters:'sPrinterName-Thenameoftheprintertobeused.''nDuplexSetting-Oneofthefollowingstandardsettings:'1=None'2=Duplexonlongedge(book)'3=Duplexonshortedge(legal)''==================================================================PublicFunctionSetPrinterDuplex(ByVals2ndFloorBinderAsString,ByValnDuplexSettingAsLong)AsBooleanDimhPrinterAsLongDimpdAsPRINTER_DEFAULTSDimpinfoAsPRINTER_INFO_2DimdmAsDEVMODEDimyDevModeData()AsByteDimyPInfoMemory()AsByteDimnBytesNeededAsLongDimnRetAsLong,nJunkAsLongOnErrorGoTocleanupIf(nDuplexSetting<1)Or(nDuplexSetting>3)ThenMsgBox"Error:dwDuplexSettingisincorrect."ExitFunctionEndIfpd.DesiredAccess=PRINTER_ALL_ACCESSnRet=OpenPrinter(s2ndFloorBinder,hPrinter,pd)If(nRet=0)Or(hPrinter=0)ThenIfErr.LastDllError=5ThenMsgBox"Accessdenied--Seethearticleformoreinfo."ElseMsgBox"Cannotopentheprinterspecified"&_"(makesuretheprinternameiscorrect)."EndIfExitFunctionEndIfnRet=DocumentProperties(0,hPrinter,s2ndFloorBinder,0,0,0)If(nRet<0)ThenMsgBox"CannotgetthesizeoftheDEVMODEstructure."GoTocleanupEndIfReDimyDevModeData(nRet+100)AsBytenRet=DocumentProperties(0,hPrinter,s2ndFloorBinder,_VarPtr(yDevModeData(0)),0,DM_OUT_BUFFER)If(nRet<0)ThenMsgBox"CannotgettheDEVMODEstructure."GoTocleanupEndIfCallCopyMemory(dm,yDevModeData(0),Len(dm))IfNotCBool(dm.dmFieldsAndDM_DUPLEX)ThenMsgBox"Youcannotmodifytheduplexflagforthisprinter"&_"becauseitdoesnotsupportduplexorthedriver"&_"doesnotsupportsettingitfromtheWindowsAPI."GoTocleanupEndIfdm.dmDuplex=nDuplexSettingCallCopyMemory(yDevModeData(0),dm,Len(dm))nRet=DocumentProperties(0,hPrinter,s2ndFloorBinder,_VarPtr(yDevModeData(0)),VarPtr(yDevModeData(0)),_DM_IN_BUFFEROrDM_OUT_BUFFER)If(nRet<0)ThenMsgBox"Unabletosetduplexsettingtothisprinter."GoTocleanupEndIfCallGetPrinter(hPrinter,2,0,0,nBytesNeeded)If(nBytesNeeded=0)ThenGoTocleanupReDimyPInfoMemory(nBytesNeeded+100)AsBytenRet=GetPrinter(hPrinter,2,yPInfoMemory(0),nBytesNeeded,nJunk)If(nRet=0)ThenMsgBox"Unabletogetsharedprintersettings."GoTocleanupEndIfCallCopyMemory(pinfo,yPInfoMemory(0),Len(pinfo))pinfo.pDevmode=VarPtr(yDevModeData(0))pinfo.pSecurityDescriptor=0CallCopyMemory(yPInfoMemory(0),pinfo,Len(pinfo))nRet=SetPrinter(hPrinter,2,yPInfoMemory(0),0)If(nRet=0)ThenMsgBox"Unabletosetsharedprintersettings."EndIfSetPrinterDuplex=CBool(nRet)cleanup:If(hPrinter<>0)ThenCallClosePrinter(hPrinter)EndFunctionSubbinder()''binderMacro'Macrorecorded7/12/2004byKeithR.Sylvester'WithActiveDocument.PageSetup.LineNumbering.Active=False.Orientation=wdOrientLandscape.TopMargin=InchesToPoints(0.92).BottomMargin=InchesToPoints(0.92).LeftMargin=InchesToPoints(0.38).RightMargin=InchesToPoints(1).Gutter=InchesToPoints(0).HeaderDistance=InchesToPoints(0.5).FooterDistance=InchesToPoints(0.5).PageWidth=InchesToPoints(11).PageHeight=InchesToPoints(8.5).FirstPageTray=wdPrinterDefaultBin.OtherPagesTray=wdPrinterDefaultBin.SectionStart=wdSectionNewPage.OddAndEvenPagesHeaderFooter=False.DifferentFirstPageHeaderFooter=False.VerticalAlignment=wdAlignVerticalTop.SuppressEndnotes=False.MirrorMargins=False.TwoPagesOnOne=False.GutterPos=wdGutterPosLeftEndWithSelection.WholeStorySelection.Font.Size=8ActivePrinter="2ndFloorBinder"Application.PrintOutFileName:="",Range:=wdPrintAllDocument,Item:=_wdPrintDocumentContent,Copies:=1,Pages:="",PageType:=wdPrintAllPages,_Collate:=True,Background:=True,PrintToFile:=False,PrintZoomColumn:=0,_PrintZoomRow:=0,PrintZoomPaperWidth:=0,PrintZoomPaperHeight:=0EndSubAlexIvanovunread,Jul13,2004,1:57:45AM7/13/04ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoYouneedtocallSetPrinterDuplexwithfullyqualifiedprinternamesomewherebeforePrintOut,ieSubPrintDuplex()DimMyPrinterasString'Youmightneedtostripextracharacterslike"onNE03:"'appendedtotheActivePrinterpropertyMyPrinter=Left(Word.ActivePrinter,InStr(1,Word.ActivePrinter,"")-1)SetPrinterDuplexMyPrinter,2ActiveDocument.PrintOutEndSub--PleasereplytoNGonly.Theemailaddressisnotmonitored.Alex."Keith"wroteinmessagenews:[email protected]...Keithunread,Jul13,2004,3:42:01AM7/13/04ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoAlex,Thanksforthehelp.ThatworkedthewayIneededitto.TheonlyproblemIhavenow,isIneedthebindingtobechanged.Ineedittobechangedtolegalbinding,thatwaywheniflipthepagesup,icanreadthenwithouthavingtoturnthefolder.Belowiswhereittellsmetodothat,Ijustcannotfigureoutwhereorwhattoputinthecode.Thanks,KeithAlexIvanovunread,Jul13,2004,4:06:44AM7/13/04ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoDoesnotthiswork?SetPrinterDuplexMyPrinter,3--PleasereplytoNGonly.Theemailaddressisnotmonitored.Alex."Keith"wroteinmessagenews:[email protected]...Keithunread,Jul13,2004,4:18:01AM7/13/04ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoThatworkedgreat!ThanksAlex!KeithKeithWunread,Jan31,2006,2:57:29AM1/31/06ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoAlexIhavebeenreadingyourrepliesherewithgreatinterest.IhavefoundyouradviseveryusefulandIhavegotthesimilarsituationtoKeith.However,IamworkingwithanewKyoceraFS8008printerandWhenIfeedaparameterof2or3(longorshortedgebinding)itworksfinebutaparameterof1doesnothing!Thefrustratingthingisthatthesettingsresetthe'default'whenallIwanttodoistoeffectthecurrentdocument.Anytipsyoucangivemewillbeappreciated.Justforconfusion,mynameisalsoKeith!Vidhyaunread,Jan21,2010,3:21:01PM1/21/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoVidhyaunread,Jan21,2010,3:25:02PM1/21/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoIhavethesamequeryasKeith.Iworkedoverthecoding.Buttheprintingdoesnttakeanyeffectonduplex.Iworkonthenetworkoverasharedprinter.Itgivesthemessate'Unabletosetsharedprintersettings'.Whyisthat?Isntthesamelikechangingthe'Duplex'valueto2/3inthePrintingpreferences?thanksinadvanceJonathanWestunread,Jan21,2010,7:43:04PM1/21/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessageto"Vidhya"wroteinmessagenews:[email protected]...You'llneedalocally-installerprinterdriverforthatcodetowork.--RegardsJonathanWestVidhyaunread,Jan22,2010,9:26:01AM1/22/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoButIneedthetemplatetoworkontheclient'sPC.Theapplicationworksforaround200peopleaccessing2000documentswith1templatedocumentacrossnetwork.Willitnotbeatasktoinstallalocalprinterforeachandeveryuseraccessingthetemplate?Isthereaworkaroundorsomething?"JonathanWest"wrote:>.>Vidhyaunread,Jan22,2010,11:46:01AM1/22/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoHi,Theprinteraccesswaschanged.ImodifiedthedesiredaccesstoPRINTER_ALL_ACCESS.Nowthereisnoerror.Butduplexprintingdoesntoccur.shouldIchangeanythingelse?-Vidhya"JonathanWest"wrote:>>You'llneedalocally-installerprinterdriverforthatcodetowork.>>>-->Regards>JonathanWest>>.>JonathanWestunread,Jan22,2010,8:06:03PM1/22/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessageto"Vidhya"wroteinmessagenews:[email protected]...>Hi,>>Theprinteraccesswaschanged.Imodifiedthedesiredaccessto>PRINTER_ALL_ACCESS.Nowthereisnoerror.Butduplexprintingdoesnt>occur.>shouldIchangeanythingelse?Yes,you'llneedtoinstallalocaldriver,asIsaid.Ioughttoknow,Iwrotethatcode.http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=116--RegardsJonathanWestVidhyaunread,Jan26,2010,9:24:06AM1/26/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoHi,HowcanIinstallthelocal-networkprinteronalltheuser'smachine?Itsnotafeasiblesolutionsinceitinvolvesmanyusers.Thecodeisfine.Imeanttoaskwhetheranytimedelayisrequiredbetweenprintandprinterreset.-Vidha"JonathanWest"wrote:>.>JonathanWestunread,Jan26,2010,10:30:07PM1/26/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessageto"Vidhya"wroteinmessagenews:[email protected]...>Hi,>>HowcanIinstallthelocal-networkprinteronalltheuser'smachine?>Its>notafeasiblesolutionsinceitinvolvesmanyusers.Thecodeisfine.Well,ifitonlyworksinacircumstancewhichdoesn'tapplytoyou,thenit's*not*fine.>I>meanttoaskwhetheranytimedelayisrequiredbetweenprintandprinter>reset.>Don'ttryanddotheresetwhiletheprintjobisstillbeingsenttotheprintspooler-thereforedon'tattempttousebackgroundprintinginWord.ThecodeinmyarticledoesthrowinafewDoEventscommandstogivetimeforeverythingtosortitselfoutaftertheprintjobhasgonethroughbeforeyouthenresettheprinterbacktoitsdefaultstate.iworkedoutbytrial&errorwhatwasenoughonthemechineIwasdevelopingon,andthenaddedafewextraonesforabitofmargintocopewithvaryingsituations.--RegardsJonathanWestVidhyaunread,Jan27,2010,9:49:01AM1/27/10ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoThanks.Itworkednow.Ineedtonowcheckwhetheritworkswithotherusers.Butmyenditworksfine.thanksagain.-Vidhya"JonathanWest"wrote:>.>ReplyallReplytoauthorForward0newmessages



請為這篇文章評分?