Duplex printing with macros - Google Groups
文章推薦指數: 80 %
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.generalConversationsAboutDuplexprintingwithmacros941viewsSkiptofirstunreadmessageKeithunread,Jul13,2004,12:42:08AM7/13/04ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoIrecordedamacro,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/04ReplytoauthorSignintoreplytoauthorForwardSignintoforwardDeleteLinkReportmessageasabuseSignintoreportmessageasabuseShoworiginalmessageEitheremailaddressesareanonymousforthisgrouporyouneedtheviewmemberemailaddressespermissiontoviewtheoriginalmessagetoYouneedtocallSetPrinterDuplexwithfullyqualifiedprinternamesomewherebeforePrintOut,ieSubPrintDuplex()DimMyPrinterasString'Youmightneedtostripextracharacterslike"onNE03:"'appendedtotheActivePrinterpropertyMyPrinter=Left(Word.ActivePrinter,InStr(1,Word.ActivePrinter,"")-1)SetPrinterDuplexMyPrinter,2ActiveDocument.PrintOutEndSub--PleasereplytoNGonly.Theemailaddressisnotmonitored.Alex."Keith"
延伸文章資訊
- 1Excel VBA double sided printing | Forum post - STL Training
Excel VBA double sided printing. HI, How do i set a double sided printing via VBA? my default on ...
- 2Print double sided - OzGrid Free Excel/VBA Help Forum
Print double sided · Cells.Select · Selection.RowHeight = 32 · With ActiveSheet.PageSetup ·.Print...
- 3How to get Duplex printing working in Excel? - Stack Overflow
Since the duplex option cannot be chosen by VBA directly, you can workaround this by adding the s...
- 4Vba To Set Duplex Printing - I have spent... - Free Excel Help
Vba To Set Duplex Printing - I have spent hours searching for code that will ... Become a master ...
- 5Thread: Excel VBA - Printing Front And Back - VBForums
excel has no provision in vba to print in duplex, even if it is in the printer dialog, and works ...