VBA creating pdf files and saving them in the same folder ...

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

I am trying to create individual pdf file for three active sheets (Report1, Report2 and Report3) and want to save them in the same folder as ... 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. VBAcreatingpdffilesandsavingtheminthesamefolderwheretheworkbookissaved Threadstarter hyunee Startdate Dec28,2016 H hyunee NewMember Joined Sep21,2016 Messages 12 Helloeveryone Iamtryingtocreateindividualpdffileforthreeactivesheets(Report1,Report2andReport3)andwanttosavetheminthesamefolderaswherethecurrentexcelworkbookissaved.ihavecodesbelowandthepdffilesarecreatedokaybuttheyarebeingsavedindifferentfolder.icantfigureitoutwhyandhowtofixit.canyoupleasehelp? Ihavesavedtheworkbookandclosedandre-openeditandthepdffilesarestillsavedinthewrongplace.... Thankyouinadvance! *herearesomebackgroundwhichmighthelpyoutohelpme... inormallycreatethefollowingmonthworkbookbychangingfilenameofpreviousmonthexcelworkbookandcreatedanewfolderforthemonthiamworkingandsavedthem.iwantthepdffilestobesavedintheTHATfoldericreatednotpreviousfolder! Codesthatihaveatthemomentare... Sheets("Report1").Select Dpath="Report1"&Format(Range("h7"),"mmmmyyyy") ActiveSheet.ExportAsFixedFormatType:=xlTypePDF,Filename:=_ Dpath,Quality:=xlQualityStandard,IncludeDocProperties:=True,IgnorePrintAreas_ :=False,OpenAfterPublish:=True Sheets("report2").Select Dpath="Report2"&Format(Range("h7"),"mmmmyyyy") ActiveSheet.ExportAsFixedFormatType:=xlTypePDF,Filename:=_ Dpath,Quality:=xlQualityStandard,IncludeDocProperties:=True,IgnorePrintAreas_ :=False,OpenAfterPublish:=True Sheets("report3").Select Dpath="Report3"&Format(Range("h7"),"mmmmyyyy") ActiveSheet.ExportAsFixedFormatType:=xlTypePDF,Filename:=_ Dpath,Quality:=xlQualityStandard,IncludeDocProperties:=True,IgnorePrintAreas_ :=False,OpenAfterPublish:=True   ExcelFacts Whentheysaid... Clickheretorevealanswer Whentheysaidyouaregoingto"Excelatlife",theymeantyou"willbedoingExcelyourwholelife". 1 2 Next 1of2 Next Last Macropod RetiredModerator Joined Aug27,2007 Messages 3,628 Youwouldgetbetterresultsifyouactuallysuppliedthepath.Try: Code: SubPDF_Save() DimiAsLong,StrPathAsString,StrFlNmAsString StrPath=ActiveWorkbook.Path&"\" Fori=1To3 WithSheets("Report"&i) StrFlNm="Report"&i&Format(.Range("H7"),"mmmmyyyy") .ExportAsFixedFormatType:=xlTypePDF,Filename:=StrPath&StrFlNm,_ Quality:=xlQualityStandard,IncludeDocProperties:=True,_ IgnorePrintAreas:=False,OpenAfterPublish:=True EndWith Next EndSub   H hyunee NewMember Joined Sep21,2016 Messages 12 Thankyou!! Theproblemisthetabname(worksheetname)arenotactuallyReport1,2and3.ijustchangeditto1,2,3foryoutounderstandeasy.TheworksheetnamesareproductnameslikeCola,Pepsi,andFantasoyourcodingbelowwontwork....sorrycanyoupleasepleasegivemedifferentcodes?   Macropod RetiredModerator Joined Aug27,2007 Messages 3,628 Itwouldhavebeenhelpfulhadyousaidtheyweren'ttherealworksheetnamesinsteadofhavingmewastetimeprovidingastreamlinedsolutionthatdoesn'tapply...Asitis,theonlypartofmycodingthatwon'tworkistheloopYouwillinsteadneedtoreplacethereferencesto: "Report"&I withwhatevertherealworksheetnamesareandhaveasequenceofsuchWith-EndWithconstructs-oryoucouldadaptthecodeIposted,thus: Code: SubPDF_Save() DimiAsLong,StrPathAsString,StrFlNmAsString,ArrNames ArrNames=Array("Cola","Pepsi","Fanta") StrPath=ActiveWorkbook.Path&"\" Fori=0ToUBound(ArrNames) WithSheets(ArrNames(i)) StrFlNm=Sheets(ArrNames(i))&Format(.Range("H7"),"mmmmyyyy") .ExportAsFixedFormatType:=xlTypePDF,Filename:=StrPath&StrFlNm,_ Quality:=xlQualityStandard,IncludeDocProperties:=True,_ IgnorePrintAreas:=False,OpenAfterPublish:=True EndWith Next EndSub   Macropod RetiredModerator Joined Aug27,2007 Messages 3,628 ADVERTISEMENT Nowcross-postedat:VBASavingPDFfilesinthesamefolderwheretheexcelworkbookissavedwithouteventhecourtesyofacknowledgingtheadvicealreadygiven. PleasereadourpolicyonCross-Postinginrule#13:http://www.mrexcel.com/forum/board-announcements/99490-forum-rules.html   H hyunee NewMember Joined Sep21,2016 Messages 12 Thankyou!andsorryfornottellingyouthesheetnamewerenotrealnamebefore. Ihavechangedthecodesasbelowanditstillnotworking.ItcomesupasComplieerror.SorryiamnewtoVBAandonlystartedlearningrecently.Ifyoucouldhelponemoretimeplease... alsoiamnotsurewhythepdfisnotbeingsavedautomaticallyinthesamefolderwheretheexcelbookissavedwhenidonthaveanyspecificpathinthecoding.. SubPDF_Save() DimiAsLong,StrPathAsString,StrFlNmAsString StrPath=ActiveWorkbook.Path&"" Fori=1To3 WithSheets("Cola","Pepsi","Fanta") StrFlNm="Cola"&Format(.Range("H7"),"mmmmyyyy") .ExportAsFixedFormatType:=xlTypePDF,Filename:=StrPath&StrFlNm,_ Quality:=xlQualityStandard,IncludeDocProperties:=True,_ IgnorePrintAreas:=False,OpenAfterPublish:=True EndWith Next EndSub   Lastedited:Dec28,2016 Macropod RetiredModerator Joined Aug27,2007 Messages 3,628 ADVERTISEMENT Isthereareasonyoudidn'tusethecodeIposted???   H hyunee NewMember Joined Sep21,2016 Messages 12 Ihave,butitdidntworkeither.itkeepssaying"Objectdoesn'tsupportthispropertyormethod. Isthereanywayicouldsendyoutheexcelworkingswiththecodesihave?iliterallycopiedandpastedyourcodesandnotsurewhyit'snotworking..   Macropod RetiredModerator Joined Aug27,2007 Messages 3,628 Typo.Try: Code: SubPDF_Save() DimiAsLong,StrPathAsString,StrFlNmAsString,ArrNames ArrNames=Array("Cola","Pepsi","Fanta") StrPath=ActiveWorkbook.Path&"\" Fori=0ToUBound(ArrNames) WithSheets(ArrNames(i)) StrFlNm=ArrNames(i)&Format(.Range("H7"),"mmmmyyyy") .ExportAsFixedFormatType:=xlTypePDF,Filename:=StrPath&StrFlNm,_ Quality:=xlQualityStandard,IncludeDocProperties:=True,_ IgnorePrintAreas:=False,OpenAfterPublish:=True EndWith Next EndSub   H hyunee NewMember Joined Sep21,2016 Messages 12 It'snowworking!Thankyousomuch!ireallyappreciateyourhelp!! CantwaittogetbetteratVBA!   1 2 Next 1of2 Next Last Youmustloginorregistertoreplyhere. Share: Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link Excelcontainsover450functions,withmoreaddedeveryyear.That’sahugenumber,sowhereshouldyoustart?Rightherewiththisbundle. Forumstatistics Threads 1,167,381 Messages 5,853,484 Members 431,590 Latestmember qaagh6 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



請為這篇文章評分?