Add Duplex command to Print Button (VBA) - Mr. Excel

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

I want pages 1&2, 3&4, 5&6, etc. to print both sides. I have a color printer on the network ... Add Duplex command to Print Button (VBA). Forums What'snew Newposts ExcelArticles MrExcelPublishing Login Register What'snew Search Newposts Searchforums Menu Login Register Installtheapp Install Ifyouwouldliketopost,pleasecheckouttheMrExcelMessageBoardFAQandregisterhere.Ifyouforgotyourpassword,youcanresetyourpassword. Forums QuestionForums ExcelQuestions Youareusinganoutofdatebrowser.Itmaynotdisplaythisorotherwebsitescorrectly.Youshouldupgradeoruseanalternativebrowser. AddDuplexcommandtoPrintButton(VBA) Threadstarter seanjon Startdate Jan22,2018 Tags application.activeprinter button print printer scurrentprinter seanjon NewMember Joined Dec23,2017 Messages 10 Goodday. AworkbookIhavehas22worksheets.Iwantpages1&2,3&4,5&6,etc.toprintbothsides.Ihaveacolorprinteronthenetworkthatisnotmydefaultprinter.Imadeacommandbuttonthatwillsendtheworkbooktothecolorprinter.Ialsowantittoprintitduplex. ThisiswhatIhaveforthebuttonsofar.Itworksandsendstheworkbooktothatprinter. Code: PrivateSubCommandButton1_Click() DimsCurrentPrinterAsString ConstMyPrinterAsString="\\vm-print\MARS_BROTHERMFC-9460CDNonNe06:" sCurrentPrinter=Application.ActivePrinter Application.ActivePrinter=MyPrinter ActiveWorkbook.PrintOutFrom:=1,To:=22,Copies:=1,Collate:=True,_ IgnorePrintAreas:=False Application.ActivePrinter=sCurrentPrinter EndSub Throughsearching,Iknowthatthereisacodetosetittoduplex: Code: SubSetPrinterToDuplex() SetPrinterDuplex"\\vm-print\MARS_BROTHERMFC-9460CDNonNe06:",2 EndSub WhereshouldIputthisinthecodeformycommandbutton,oristheresomethingdifferentIneedtoputinthesub? Thanksforyourtime. Sean   ExcelFacts Differencebetweentwodates Clickheretorevealanswer Secretfunction!Use=DATEDIF(A2,B2,"Y")&"years"&=DATEDIF(A2,B2,"YM")&"months"&=DATEDIF(A2,B2,"MD")&"days" PaulKed ActiveMember Joined Jun4,2015 Messages 442 Youmaybeabletosendcontrolcodestotheprintertotellittoprintinduplexmode,butyouwouldhavetolookthroughtheprinterdocumentationtoseeifthisispossible. OtherwiseitisprobablypossibleusingthedarkartofAPIcalls,whichIcanonlycopyfromtheWizardsthatwritethem!   PaulKed ActiveMember Joined Jun4,2015 Messages 442 BTW Code: SubSetPrinterToDuplex() SetPrinterDuplex"\\vm-print\MARS_BROTHERMFC-9460CDNonNe06:",2 EndSub Won'tworkwithoutthefunctionSetPrinterDuplex   Lastedited:Jan22,2018 seanjon NewMember Joined Dec23,2017 Messages 10 PaulKedsaid: BTW Code: SubSetPrinterToDuplex() SetPrinterDuplex"\\vm-print\MARS_BROTHERMFC-9460CDNonNe06:",2 EndSub Won'tworkwithoutthefunctionSetPrinterDuplex Clicktoexpand... Iamnotunderstandingwhatyoumeanbythis.Thecodehassetprinterduplex.WhatamImissingthatyouaretellingme?Thanks   PaulKed ActiveMember Joined Jun4,2015 Messages 442 Aquicksearchturnedupthisfunction: Code: PublicFunctionSetPrinterDuplex(ByValsPrinterNameAsString,_ ByValnDuplexSettingAsLong)AsBoolean DimhPrinterAsLong DimpdAsPRINTER_DEFAULTS DimpinfoAsPRINTER_INFO_2 DimdmAsDEVMODE DimyDevModeData()AsByte DimyPInfoMemory()AsByte DimnBytesNeededAsLong DimnRetAsLong,nJunkAsLong OnErrorGoTocleanup If(nDuplexSetting<1)Or(nDuplexSetting>3)Then MsgBox"Error:dwDuplexSettingisincorrect." ExitFunction EndIf pd.DesiredAccess=PRINTER_ALL_ACCESS nRet=OpenPrinter(sPrinterName,hPrinter,pd) If(nRet=0)Or(hPrinter=0)Then IfErr.LastDllError=5Then MsgBox"Accessdenied--Seethearticleformoreinfo." Else MsgBox"Cannotopentheprinterspecified"&_ "(makesuretheprinternameiscorrect)." EndIf ExitFunction EndIf nRet=DocumentProperties(0,hPrinter,sPrinterName,0,0,0) If(nRet<0)Then MsgBox"CannotgetthesizeoftheDEVMODEstructure." GoTocleanup EndIf ReDimyDevModeData(nRet+100)AsByte nRet=DocumentProperties(0,hPrinter,sPrinterName,_ VarPtr(yDevModeData(0)),0,DM_OUT_BUFFER) If(nRet<0)Then MsgBox"CannotgettheDEVMODEstructure." GoTocleanup EndIf CallCopyMemory(dm,yDevModeData(0),Len(dm)) IfNotCBool(dm.dmFieldsAndDM_DUPLEX)Then MsgBox"Youcannotmodifytheduplexflagforthisprinter"&_ "becauseitdoesnotsupportduplexorthedriver"&_ "doesnotsupportsettingitfromtheWindowsAPI." GoTocleanup EndIf dm.dmDuplex=nDuplexSetting CallCopyMemory(yDevModeData(0),dm,Len(dm)) nRet=DocumentProperties(0,hPrinter,sPrinterName,_ VarPtr(yDevModeData(0)),VarPtr(yDevModeData(0)),_ DM_IN_BUFFEROrDM_OUT_BUFFER) If(nRet<0)Then MsgBox"Unabletosetduplexsettingtothisprinter." GoTocleanup EndIf CallGetPrinter(hPrinter,2,0,0,nBytesNeeded) If(nBytesNeeded=0)ThenGoTocleanup ReDimyPInfoMemory(nBytesNeeded+100)AsByte nRet=GetPrinter(hPrinter,2,yPInfoMemory(0),nBytesNeeded,nJunk) If(nRet=0)Then MsgBox"Unabletogetsharedprintersettings." GoTocleanup EndIf CallCopyMemory(pinfo,yPInfoMemory(0),Len(pinfo)) pinfo.pDevmode=VarPtr(yDevModeData(0)) pinfo.pSecurityDescriptor=0 CallCopyMemory(yPInfoMemory(0),pinfo,Len(pinfo)) nRet=SetPrinter(hPrinter,2,yPInfoMemory(0),0) If(nRet=0)Then MsgBox"Unabletosetsharedprintersettings." EndIf SetPrinterDuplex=CBool(nRet) cleanup: If(hPrinter<>0)ThenCallClosePrinter(hPrinter) EndFunction   PaulKed ActiveMember Joined Jun4,2015 Messages 442 Therestofthecodeandinstructions(forWord,butitwillbeverysimilarforExcel)arehere: https://support.microsoft.com/en-us...rinting-for-microsoft-word-automation-clients   Youmustloginorregistertoreplyhere. Share: Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link Excelcontainsover450functions,withmoreaddedeveryyear.That’sahugenumber,sowhereshouldyoustart?Rightherewiththisbundle. Forumstatistics Threads 1,167,285 Messages 5,853,079 Members 431,546 Latestmember GeneBF Sharethispage Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link Forums QuestionForums ExcelQuestions We'vedetectedthatyouareusinganadblocker.WehaveagreatcommunityofpeopleprovidingExcelhelphere,butthehostingcostsareenormous.YoucanhelpkeepthissiterunningbyallowingadsonMrExcel.com.AllowAdsatMrExcelWhichadblockerareyouusing?DisableAdBlockFollowtheseeasystepstodisableAdBlock1)Clickontheiconinthebrowser’stoolbar.2)Clickontheiconinthebrowser’stoolbar.2)Clickonthe"Pauseonthissite"option.GobackDisableAdBlockPlusFollowtheseeasystepstodisableAdBlockPlus1)Clickontheiconinthebrowser’stoolbar.2)Clickonthetoggletodisableitfor"mrexcel.com".GobackDisableuBlockOriginFollowtheseeasystepstodisableuBlockOrigin1)Clickontheiconinthebrowser’stoolbar.2)Clickonthe"Power"button.3)Clickonthe"Refresh"button.GobackDisableuBlockFollowtheseeasystepstodisableuBlock1)Clickontheiconinthebrowser’stoolbar.2)Clickonthe"Power"button.3)Clickonthe"Refresh"button.GobackContinuewithoutadsI'vedisabledmyadblock Top



請為這篇文章評分?