VBA creating pdf files and saving them in the same folder ...
文章推薦指數: 80 %
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
延伸文章資訊
- 1Excel VBA program save a copy of file to same directory as ...
I want the code to save the copy in the same directory as the original document a folder called "...
- 2How to Save a PDF File with a Specific User-Defined Path in ...
A simple VBA macro program script will be able to open or display a dialog window, asking the use...
- 3Excel VBA Save As PDF: Step-By-Step Guide And 10 Examples
If you're creating a macro to save Excel files as PDF, and those PDF files must always be saved i...
- 4Saving all worksheets to seperate PDF files but excluding ...
I am a bit new to VBA coding but have managed to create a macro ... through all worksheets and sa...
- 5Excel VBA Macro to Save PDF in Specific Folder (7 Ideal ...
This tutorial will demonstrate different examples to use Excel VBA macro to save as PDF in a spec...