Hi everyone
I have workbook I would like to auto save to PDF copy file in different location every time the original file is save maybe some VBA code is this possible ???
Thanks in advance
Shane
Hi everyone
I have workbook I would like to auto save to PDF copy file in different location every time the original file is save maybe some VBA code is this possible ???
Thanks in advance
Shane
Last edited by sspreyer; 08-16-2013 at 04:17 PM.
Hello Shane,
Add this macro to your ThisWorkbook module. Change the file path and name to what you will be using.
Private Sub Workbook_AfterSave(ByVal Success As Boolean) Dim Filename As String Dim Filepath As String Filepath = "C:\Users\Owner\Documents\" Filename = Filepath & ThisWorkbook.Name If Success Then With ThisWorkbook.ExportAsFixedFormat _ (Type:=xlTypePDF, _ Filename:=Filename, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=True, _ OpenAfterPublish:=False) End With End If End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Star below the post.3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
hi thanks for your help i seem to be get error with this code i have highlighted in the code showing were it say's im getting compile error also having trouble entering a file name to work
cheers
p.s sorry for being a pain
Private Sub Workbook_AfterSave(ByVal Success As Boolean) Dim Filename As String Dim Filepath As String Filepath = "C:\Users\Shane\Documents\" Filename = Filepath & ThisWorkbook.Name If Success Then With ThisWorkbook.ExportAsFixedFormat _ ' this line highlights and i get error message ("compile error expected file or variable") (Type:=xlTypePDF, _ Filename:=Filename, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=True, _ OpenAfterPublish:=False) End With End If End Sub
Last edited by jeffreybrown; 08-18-2013 at 09:58 AM. Reason: As per Forum Rule #12, please don't quote whole post unless necessary -- it's just clutter.
Hi,
It works for me if set out as
i.e. without the parentheses either side of the .Export......code and with the .Export.... as a new lineWith ThisWorkbook .ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=Filename1, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=True, _ OpenAfterPublish:=False End With
Richard Buttrey
RIP - d. 06/10/2022
If any of the responses have helped then please consider rating them by clicking the small star icon below the post.
Should be:
Besides, if you set a print area, make sure to set "IgnorePrintAreas" to "False".With ThisWorkbook .ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=Filename1, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=True, _ OpenAfterPublish:=False End With
Last, but not least, I combined some code I found elsewhere with the code in this forum. That way my PDF file saves in the same location as the Excel file without having to set the path, manually. Next to that, the PDF file only contains the print area of the active worksheet, instead of the whole Excel file :
Private Sub Workbook_AfterSave(ByVal Success As Boolean) Dim ws As Worksheet Dim strPath As String Dim myFile As Variant Dim strFile As String Set ws = ActiveSheet 'enter name and select folder for file ' start in current workbook folder strFile = Replace(ThisWorkbook.Name, ".xlsm", "") _ & ".pdf" strFile = ThisWorkbook.Path & "\" & strFile If Success Then ws.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=strFile, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End If exitHandler: Exit Sub End Sub
Last edited by ledenjes; 07-14-2014 at 06:34 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks